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);

    TheHandler handler = new TheHandler();

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);

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

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



public static void main(String[] args) {
    MultiForm go = new MultiForm();
    go.setSize(400, 200);


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());

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


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 *