Search code examples
javaswinglayout-managergridbaglayout

How does GridBagConstraints work in Java


I just can't understand, how GridBagConstraints works, because sometimes it just goes unpredictable.

First of all I will introduce layout i want as matrix:

[l1][ca] Legend:
[jt][ca] l1 - JLabel1; l2 - JLabel2;
[jt][l2] jt - jTable;  jb - JButton; 
[jt][jb] ca - camera (temporary just JButton);

And what I actually receive:

[l1][ca]
[jt][l2]
[jt][l2]
[jt][jb]
[jt][jb]

My code for this frame:

public static void addToPollFramePane(Container pane){
    pane.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.BOTH;

    JLabel label = new JLabel("Chose one party from the list");
    gbc.fill = GridBagConstraints.BOTH;
    gbc.weightx = 0.5;
    gbc.weighty = 0.25;
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    pane.add(label, gbc);

    JTable table = createTable(partiesDoc);
    JScrollPane scrollPane = new JScrollPane(table);
    gbc.fill = GridBagConstraints.BOTH;
    gbc.weightx = 0.5;
    gbc.weighty = 0.75;
    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.gridwidth = 1;
    gbc.gridheight = 3;
    pane.add(scrollPane, gbc);

    JButton button = new JButton("Camera");
    gbc.fill = GridBagConstraints.BOTH;
    gbc.weightx = 0.5;
    gbc.weighty = 0.5;
    gbc.gridx = 1;
    gbc.gridy = 0;
    gbc.gridwidth = 1;
    gbc.gridheight = 2;
    pane.add(button, gbc);

    label = new JLabel("Press button");
    gbc.fill = GridBagConstraints.BOTH;
    gbc.weightx = 0.5;
    gbc.weighty = 0.25;
    gbc.gridx = 1;
    gbc.gridy = 2;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    pane.add(label, gbc);

    button = new JButton("Vote");
    gbc.fill = GridBagConstraints.BOTH;
    gbc.weightx = 0.50;
    gbc.weighty = 0.25;
    gbc.gridx = 1;
    gbc.gridy = 3;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    pane.add(button, gbc);

}


public static void showPollFrame(JFrame rootFrame){
    JFrame pollFrame = new JFrame("Poll");
    pollFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    pollFrame.setResizable(false);
    pollFrame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            rootFrame.setVisible(true);
        }
    });

    Document confDoc = MainFrame.loadDocumnetFromXML("conf.xml");
    conf = new MainFrame.Config(confDoc);
    partiesDoc = MainFrame.loadDocumnetFromXML(conf.pathToParties);
    addToPollFramePane(pollFrame.getContentPane());

    GraphicsEnvironment enviroment = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice device = enviroment.getDefaultScreenDevice();
    device.setFullScreenWindow(pollFrame);
    pollFrame.setVisible(true);
}

An image of frame:screenshot

As you can see it is displayed absolutely wrong, and I just don't understand why... Even fullscreen is not a fullscreen.

Please explain what I did wrong here!

I have read https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html


Solution

  • Something gets messed up due to the weights. If you remove the weights from the first label, it is shown correctly. Maybe this is because different columns span different rows so that weighing the rows doesn't make complete sense always or maybe a bug in Swing. Below shows which weights to comment out / remove:

    JLabel label = new JLabel("Chose one party from the list");
    gbc.fill = GridBagConstraints.BOTH;
    // gbc.weightx = 0.5;
    // gbc.weighty = 0.25;