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

4
  • 4
    Obligatory comment: The Use of Multiple JFrames, Good/Bad Practice? Commented Apr 5, 2013 at 8: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. Commented Apr 5, 2013 at 8: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); Commented Apr 5, 2013 at 8:20
  • is there each class have one frame or more? Commented Apr 5, 2013 at 8:23

1 Answer 1

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?.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.