GUI, JComboBox and opening a new window

I am new to Java and especially new to GUI and it’s super confusing to me right now.

I’m making a program for class that is supposed to have a menu (JComboBox I’m assuming) that opens a new window when an option is selected. I am just working on the first option where you click “The Matrix” and a new window pops up with two buttons called “Red Pill” & “Blue Pill” and thats where I’ve hit a wall.

I got to the point where I am able to create a new window (not sure if this is even the right route to take for opening the new window) but, When I try to add Buttons to the new pop up window nothing shows up….

Thanks for any help or pointers in the right direction!

public class MultiForm extends JFrame{

    private JComboBox menu;
    private JButton bluePill;
    private JButton redPill;

private static String[] fileName = {"", "The Matrix", "Another Option"};

public MultiForm() {
    super("Multi Form Program");        
    setLayout(new FlowLayout());
    menu = new JComboBox(fileName);
    add(menu);

    TheHandler handler = new TheHandler();
    menu.addActionListener(handler);        
}

private class TheHandler implements ActionListener{
    public void actionPerformed(ActionEvent event) {        
      ********************************************************************  
            //Create a new window when "The Matrix" is clicked in the JCB
            JFrame newFrame = new JFrame();
            JPanel panel = new JPanel();
            newFrame.setLayout(new FlowLayout());
            newFrame.setSize(500, 300);
            newFrame.setDefaultCloseOperation(newFrame.EXIT_ON_CLOSE);

            Icon bp = new ImageIcon(getClass().getResource("Blue Pill.png"));
            bluePill = new JButton("Blue Pill", bp);
            newFrame.add(bluePill); 

            Icon rp = new ImageIcon(getClass().getResource("Red Pill.png"));
            redPill = new JButton("Red Pill", rp);
            newFrame.add(redPill);  

            add(panel);
            newFrame.setVisible(true);

    }
}

public static void main(String[] args) {
    MultiForm go = new MultiForm();
    go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    go.setSize(400, 200);
    go.setVisible(true);
}
}

Answer

I tried doing newFrame.add(BluePill) and it created a button that was the size of the entire window and it would not allow me to add both buttons that way

That’s because the frame uses a BorderLayout by default. Unless you specify otherwise, the component’s will be added to the CENTER position, BUT, BorderLayout will only allow a single component to be managed at each of the it’s five available positions, so you are only seeing the last component you added.

See How to Use BorderLayout for more details

so I figured that wasn’t the correct way

It’s the right approach, you just need to use a layout manager which can accommodate more components or change the position which you are adding the buttons

In this little example, I’ve just use a FlowLayout, but you can use what ever is going to give you the effect you desire

JFrame newFrame = new JFrame();
newFrame.setLayout(new FlowLayout());
newFrame.setDefaultCloseOperation(newFrame.EXIT_ON_CLOSE);

bluePill = new JButton("Blue Pill");
newFrame.add(bluePill);
redPill = new JButton("Red Pill");
newFrame.add(redPill);

newFrame.pack();
newFrame.setVisible(true);

As a general rule of thumb, I don’t like adding components like this directly to a top level container, I prefer to use a intermediate container, like a JPanel, this gives me more possibilities for re-use, but that’s me.

You should also only make the frame visible when it’s actually ready, otherwise you may find that some times, the components won’t show up

See Laying Out Components Within a Container for more details

Leave a Reply

Your email address will not be published. Required fields are marked *