Question

I'm fairly new to Swing, so I've been using windowbuilder to try and put together a basic GUI. The design screen works fine, but when I return to the code, it's written it in a way I'm unfamiliar with and I'm struggling to actually get it to run.

The code it generates is:

public class GUIControls extends JFrame{

public GUIControls() {
    getContentPane().setLayout(new CardLayout(0, 0));

    JPanel panel = new JPanel();
    getContentPane().add(panel, "name_36737116256884");
    panel.setLayout(null);

    JButton InsertionSortButton = new JButton("Insertion Sort");
    InsertionSortButton.setBounds(32, 16, 101, 56);
    panel.add(InsertionSortButton);

    JPanel panel_1 = new JPanel();
    getContentPane().add(panel_1, "name_36737137352442");
    InsertionSortButton.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent arg0) {
            CardLayout cardLayout = (CardLayout) getContentPane().getLayout();
            cardLayout.show(getContentPane(), "name_36737137352442");
        }
    });
}

(With the action taken when the button is mouseclicked being written by me, I haven't tested it because I can't run the thing)

Normally I'd do:

public void runGUI(){
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createGUI();

        }
    });
}

With createGUI being the method I used to create a (completely horrible) GUI without windowbuilder, but I can't use GUIControls in this because it doesn't work with runnable (in fact, I'm not even sure what it is when something doesn't a return value, is it still a method?).

Does anyone know how I go about running it?

Thanks

Was it helpful?

Solution

You need to instantiate an instance of GUIControls and make it visible, for example...

public void runGUI(){
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            GUIControls guiControls = new GUIControls();
            guiControls.pack();
            guiControls.setLocationRelativeTo(null);
            guiControls.setVisible(true);
        }
    });
}

ps- I know Window Builder likes to make use of null layouts, but I would avoid them wherever possible - IMHO

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top