Arc
Arc

Reputation: 449

JFrame changing screens

I'm wondering how to change screens in a JFrame. For example, changing from the starting screen to a different screen. So you have an assortment of buttons, labels, trees, etc on one screen, as the user clicks a button a different layout appears.

Would the 'setVisible(false) and setVisible(true)' do the trick?

Upvotes: 1

Views: 20157

Answers (1)

kwikness
kwikness

Reputation: 1445

You've got it! Create separate JFrame instances for each of your frames:

JFrame frame1 = new JFrame();
JFrame frame2 = new JFrame();

//populate your frames with stuff

frame1.setVisible(false);
frame2.setVisible(true);

On a side note, you'll want to make sure to use setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE) on any secondary frames to prevent your application from terminating if a user closes a secondary frame.

All that being said, you can also use multiple JPanel instances inside of the same JFrame instead of creating multiple JFrame instances. This way, all the action of your application will take place in one window.

I would strongly recommend giving this a read through: http://docs.oracle.com/javase/tutorial/uiswing/

Upvotes: 3

Related Questions