0

Well, I have several JFrames I put them in different classes. with a JFrame which is the used as a start frame which present a list of buttons Each one can start a frame. If I call the frame using only frame.setVisible (true). I have the frame on top of the first. I want to know how to open the second and I close the first, if you have an example I will be grateful

Howard
  • 38,639
  • 9
  • 64
  • 83
Zizou
  • 1,891
  • 3
  • 15
  • 16
  • 4
    Obligatory comment: [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/577423) – Howard Apr 05 '13 at 08:17
  • 1
    Don't do that. Replace the content of the first frame instead, using a CardLayout for example. That said, calling setVisible(false) on the first frame will hide it. – JB Nizet Apr 05 '13 at 08:18
  • 1
    Really consider the 2 comments above. JFYI: _I want to know how to open the second and I close the first_ --> `frame2.setVisible(true); frame1.setVisible(false);` – Guillaume Polet Apr 05 '13 at 08:20
  • is there each class have one frame or more? – chintan Apr 05 '13 at 08:23

1 Answers1

1

If you really want to have many frames, you can do this:

Firstly, do not launch all of the frames at once, launch only the one which contains the buttons. If a corresponding button is clicked, the start method (the one which will launch the frame) of the corresponding frame should be called. In the frame with the buttons call

this.dispose();

to stop all of the frame's actions (including visibility and presence of icons in the task bar).


Here is an example:

private void ButtonActionPerformed(){
this.dispose();
OtherFrameClass.createWindow();
}

Just to clarify in this example the method is the method that is called when the button is pressed and the createWindow() method is the one that launches the next window


This will work but is not an elegant answer. Refer to The Use of Multiple JFrames, Good/Bad Practice?.

Community
  • 1
  • 1
Oliver-R
  • 163
  • 1
  • 9