简体   繁体   中英

Java Swing GridBagLayout

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).

One would think that having said (just showing the salient lines here, fuller code below)

GridBagConstraints c = new GridBagConstraints();
...
c.gridx = 0;
c.gridwidth = 2;
...
mainPanel.add(l, c);

followed by:

c.gridx = 2;
c.gridwidth = 4;
...
mainPanel.add(l, c);

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:

GridBagConstraints c = new GridBagConstraints();
...
c.gridx = 0;
c.gridwidth = 1;
...
mainPanel.add(l, c);

followed by:

c.gridx = 1;
c.gridwidth = 3;
...
mainPanel.add(l, c);

But what I get is all lines divided 50-50. I was able to get lines divided 50-50 and then others not divided, which was OK for a preliminary version.

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.

Relevant code follows, the rest of the project was a standard empty NetBeans Java desktop application:

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...

The grid bag layout doesn't force columns to be of equal width, as far as I know. So what you get is probably that your first column is 50% in width, second and third column have a 0 or near-zero width, and the fourth column again is 50%.

Try making a row where you fill each of the four columns separately. Make sure to use the weightx options.

Give GridBagLayout a hint of what a single cell is - simply add an empty area for each single cell as first row.

gbc.gridx = 0;
gbc.gridwidth = 1;
for (int i=0; i<4; i++)
{  
  container.add(Box.createRigidArea(new Dimension()), gbc);
  gbc.gridx ++;
} 

//now add your rows
gbc.gridx = 0;
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM