Keselme
Keselme

Reputation: 4279

Is it possible to add new JPanel to JFrame, after JFrame is loaded?

I'm building a minesweeper game in Java. During the GUI design I came to a problem. Here is my game window.enter image description here

The grey area supposed to contain the actual mine-field. What I want to happen is that after the player clicks Start Game, I want to load the JPanel that contains the implementation of the mine field. However, it's seems that I can't add any more JPanels after JFrame has loaded, I'm correct? I tried running the addition of the mine field in a different thread (sleep for a few seconds and then add the JPanel), just to see if it's possible, and got no result.

public GameWindow(){

        super("Minesweeper");
        this.setLayout(new BorderLayout());

        this.gamePanel = new GamePanel(new GridLayout(BOARD_SIZE,BOARD_SIZE,1,1));

        this.timerPanel = new TimerPanel(new BorderLayout(15,15));

        this.timerPanel.initializeFlagLabel(8);

        this.mineField = new Button[BOARD_SIZE][BOARD_SIZE];
        int buttonCounter = 0;

        for(int i = 0; i < BOARD_SIZE; i ++){

            for(int j = 0; j < BOARD_SIZE; j++){

                mineField[i][j] = new Button();
                mineField[i][j].setName(Integer.toString(buttonCounter++));
                mineField[i][j].setBackground(Color.GRAY);
                mineField[i][j].setPreferredSize(new Dimension(10,10));
                mineField[i][j].addMouseListener(this);
                gamePanel.add(mineField[i][j]);
            }
        }
        Container container = getContentPane();

        container.add(timerPanel,BorderLayout.PAGE_START);

        container.add(new MenuPanel(this), BorderLayout.WEST);

        new Thread(new Runnable(){//Here I want to wait for the JFrame to 
                                  //load and then to add the gamePanel.
                 public void run(){
                     try {
                        Thread.sleep(4000);
                        container.add(gamePanel, BorderLayout.CENTER);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                    e.printStackTrace();
                }

             }
        }); 

        this.setSize(800,640);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);
        this.setVisible(true);

    }

To summarize my question is, is it possible to add new JPanels to JFrame during run-time, and if so how can I do it?

Upvotes: 0

Views: 81

Answers (2)

Nameis j
Nameis j

Reputation: 75

In the action listener for start game, you can add a jpanel to the frame that is precreated. You also may need to repaint() or revalidate()

Upvotes: 1

MadProgrammer
MadProgrammer

Reputation: 347332

The short answer is, yes. You need to call revalidate and repaint to trigger a layout and paint pass on the container you've updated.

The longer answer is, probably not the way you're doing it.

Apart from the fact that you never actually start the Thread, Swing is not thread safe and you shouldn't be updating the UI out side of the context of the Event Dispatching Thread.

In you case, a alternative (and safer) solution would be to use a Swing Timer. See How to use Swing Timers for more details

Upvotes: 2

Related Questions