0

I'm wondering, I've been using Netbeans as a learning tool for java and for the past programs that I've made I've been using:

this.dispose();
Frame2 frame2 = new frame2();
frame2.setVisible(true);

Is this the best way to do it?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ângelo
  • 71
  • 2
  • 3
  • 12

1 Answers1

4

It looks like you're trying to swap JFrames. If so, the best thing to do is to change your GUI structure to avoid doing this as window swapping can be rather annoying to the user and is usually unnecessary. Rather than swapping full fledged windows, better to have one main GUI window, one JFrame, and swap views which are often JPanels. The CardLayout can help you do this. This is yet another reason most Swing GUI programs should be geared towards creation of JPanels and not JFrames or JApplets. Doing this will greatly increase the flexibility and potential power of your code.

The tutorial can be found here: CardLayout tutorial.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Thank you for the fast and easily understoodable answer. So, it would be better if i created panels? So it would be something like this? panel1.setVisible(false); panel2.setVisible(true); – Ângelo Apr 27 '14 at 23:17
  • 2
    `So, it would be better if i created panels?`, yes. `So it would be something like this?...`, no. You were given a link to a tutorial. I doubt you read the tutorial in 3 minutes. Read the tutorial first and THEN ask a follow up question if there is something you don't understand. – camickr Apr 27 '14 at 23:40