2
votes

I have created a simple program that has 1 JLabel that says Would you like to continue and 2 JButtons one that says yes and one that says no.

I'm using GridBaGLayout to organize the JPanel / JFrame. My program compiles and runs just fine but GridBagLayout Centers all my components in the center of the JFrame. Since I'm new to swing can someone show me how its may be possible to align all my components to the top left of the JFrame?

2
Are you using an IDE?Rabbit Guy
Unless you're doing homework, consider JOptionPane, core Swing functionality for common dialogs, e.g. JOptionPane.showConfirmDialog(null, "Would you like to continue", "Continue?", JOptionPane.YES_NO_OPTION);Adam

2 Answers

2
votes

It might not be a real answer, but anyway: stop using GridBagLayout. It has so many pitfalls and is so brittle to use, so please don't use it.

Use a better alternative such as MigLayout, and learn that. Don't bother learning any of Java's default Layout Managers, except if you really, really need to.

An example using MigLayout:

JPanel panel = new JPanel(new MigLayout("","",""));

panel.add(myJButton1, "wrap");
panel.add(myJButton2, "wrap");
panel.add(myJButton3, "wrap");
panel.add(myJButton4, "wrap");
panel.add(myJButton5, "wrap");
panel.add(myJButton6, "wrap");

enter image description here

1
votes

There's a couple of ways you could do it, you could add all the components to another container first and then layout that container in your container, but the basic principle would remain...

Example

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc= new GridBagConstraints();
            gbc.weightx = 1;
            gbc.anchor = GridBagConstraints.WEST;
            gbc.gridwidth = GridBagConstraints.REMAINDER;

            for (int index = 0; index < 10; index++) {
                add(new JButton("Test"), gbc);
            }

            gbc.weighty = 1;
            add(new JLabel(), gbc);
        }

    }

}

Basically, this adds a "hidden" component to the last row, which wants to use up the remaining space of the container, forcing the components to the top/left corner of the container