Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Swing GridBagLayout
    text
    copied!<p>I am writing a Java gui to mimic some paperwork, in particular a form which has a number of "lines" only known at run-time, and the lines are either divided in two (50%-50%, a label and an entry) or divided 25% 75%, (let's say a number and a full sentence).</p> <p>One would think that having said (just showing the salient lines here, fuller code below)</p> <pre><code>GridBagConstraints c = new GridBagConstraints(); ... c.gridx = 0; c.gridwidth = 2; ... mainPanel.add(l, c); </code></pre> <p>followed by:</p> <pre><code>c.gridx = 2; c.gridwidth = 4; ... mainPanel.add(l, c); </code></pre> <p>would 'establish' that in the x direction, the panel is divided into 4, giving me my 50-50 which should leave me free to do this to get my 25%-75% version:</p> <pre><code>GridBagConstraints c = new GridBagConstraints(); ... c.gridx = 0; c.gridwidth = 1; ... mainPanel.add(l, c); </code></pre> <p>followed by:</p> <pre><code>c.gridx = 1; c.gridwidth = 3; ... mainPanel.add(l, c); </code></pre> <p>But what I get is all lines divided 50-50. I <em>was</em> able to get lines divided 50-50 and then others not divided, which was OK for a preliminary version. </p> <p>Have I mis-understood the way this works here? I note in the "Similar Questions" sidebar this post (http://stackoverflow.com/questions/7509781/java-gridbaglayout-automated-construction) recommending MiG layout, which I would consider seriously.</p> <p>Relevant code follows, the rest of the project was a standard empty NetBeans Java desktop application:</p> <pre><code>public class GridBagDemoView extends FrameView { public GridBagDemoView(SingleFrameApplication app) { super(app); initComponents(); // Reset the grid position nextGridY = 0; addLine_50_50("50", "50"); addLine_50_50("50", "50"); addLine_50_50("50", "50"); addLine_50_50("50", "50"); addLine_25_75("25", "75"); addLine_25_75("25", "75"); addLine_25_75("25", "75"); addLine_25_75("25", "75"); mainPanel.validate(); mainPanel.revalidate(); mainPanel.repaint(); } private void addLine_50_50(String left, String right) { GridBagConstraints c = new GridBagConstraints(); // "Universal" settings c.fill = GridBagConstraints.HORIZONTAL; c.insets = new Insets(5, 5, 5, 5); c.anchor = GridBagConstraints.NORTHWEST; c.gridheight = 1; c.weightx = 1; c.weighty = 1; // Settings for the labels (LHS of panel) JTextArea l = new JTextArea(); l.setText(left); c.gridx = 0; c.gridy = nextGridY; c.gridwidth = 2; c.weightx = 1; mainPanel.add(l, c); // Settings for the text (RHS of panel) JTextArea ta = new JTextArea(); ta.setText(right); c.gridx = 2; c.gridy = nextGridY; c.gridwidth = 2; c.weightx = 1; mainPanel.add(ta, c); // Increase row number of next line nextGridY++; } private void addLine_25_75(String left, String right) { GridBagConstraints c = new GridBagConstraints(); // "Universal" settings c.fill = GridBagConstraints.HORIZONTAL; c.insets = new Insets(5, 5, 5, 5); c.anchor = GridBagConstraints.NORTHWEST; c.gridheight = 1; c.weighty = 1; // Settings for the labels (LHS of panel) JTextArea l = new JTextArea(); l.setText(left); c.gridx = 0; c.gridy = nextGridY; c.gridwidth = 1; c.weightx = 1; mainPanel.add(l, c); // Settings for the lext (RHS of panel) JTextArea ta = new JTextArea(); ta.setText(right); c.gridx = 1; c.gridy = nextGridY; c.gridwidth = 3; c.weightx = 1; mainPanel.add(ta, c); // Increase row number of next line nextGridY++; } Generated code etc... </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload