1

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?

Richard Tingle
  • 16,906
  • 5
  • 52
  • 77
Arc
  • 441
  • 1
  • 9
  • 26
  • 1
    Don't understand what you want to achieve, but you should use containers as `JPanel` Then you can use a `LayoutManager` as `CardLayout` to switch views. [How to use CardLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) – nachokk Sep 17 '13 at 18:42
  • You can help yourself. Just code and run the program. – JNL Sep 17 '13 at 18:42
  • if you do not know what a method does 1) read up on the javadoc 2) give it a go... – chancea Sep 17 '13 at 18:44

1 Answers1

3

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/

kwikness
  • 1,425
  • 4
  • 21
  • 37
  • 3
    Creating multiple frame is NOT a good idea. See: http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice – camickr Sep 17 '13 at 18:54
  • ..which is why I provided an alternative. :) However, there may be certain instances where using multiple JFrame instances is advantageous. You should read the answer by ryvantage on the thread you linked. ;) – kwikness Sep 17 '13 at 18:55