Search code examples
javaswingjpanellayout-managergridbaglayout

Vertical filling of GridBagLayout in Java does not work


I am having troubles getting the layout right. I have a Class which extends JFrame and has a JPanel which has a the GridBagLayout as layout manager. I want 4 buttons which should be layout in this way

Layout

Somehow the vertical filling does not work. I tried various ways but can't figure out how to make this work.

Sorry if this is a noob question, but tried it for more than an hour without any change :/

public class ButtonPanel extends JFrame {

    private static final long serialVersionUID = 1L;

    public ButtonPanel() {
        this.setSize(800, 600);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
        frame();
    }

    JButton objectsBtn = new JButton("Objekte");    //TODO Strings einfügen
    JButton tenantBtn = new JButton("Mieter");              //TODO Strings einfügen
    JButton ownerBtn = new JButton("Eigentümer");               //TODO Strings einfügen
    JButton optionsBtn = new JButton("Einstellungen");          //TODO Strings einfügen

    JPanel panel = new JPanel();

    public void frame(){

        panel.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(5, 5, 5, 5);
        c.fill = GridBagConstraints.BOTH;
        c.gridwidth = GridBagConstraints.RELATIVE;
        c.gridheight = GridBagConstraints.RELATIVE;

        //Elemente
        c.weightx = 1.0;
        c.weighty = 1.0;
        c.gridx = 0;
        c.gridy = 0;
        panel.add(objectsBtn, c);

        c.weightx = 1.0;
        c.weighty = 1.0;
        c.gridx = 1;
        c.gridy = 0;
        panel.add(optionsBtn, c);

        c.weightx = 1.0;
        c.weighty = 1.0;
        c.gridx = 0;
        c.gridy = 1;
        panel.add(ownerBtn, c);

        c.weightx = 1.0;
        c.weighty = 1.0;
        c.gridx = 1;
        c.gridy = 1;
        panel.add(tenantBtn, c);


        this.add(panel);

        objectsBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                new Object();
            }
        });

        ownerBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                //TODO Eigentümer erstellen
            }
        });
    }


}

greets

THE-E

EDIT: Found the bug

I used Seaglass as Look and Feel in the main-method

  //Set Theme
    try {
        UIManager.setLookAndFeel("com.seaglasslookandfeel.SeaGlassLookAndFeel");
    } catch (Exception e) {
        e.printStackTrace();
    }

It works fine with the default Look and Feel Theme.


Solution

  • I have added the answer in the first post. It was a bug caused by the LookAndFeel skin. :/

    greets

    THE-E