1

A while ago, I read that the best way to switch between different "screens" in an application would be to set up different JFrames and then simply dipose the first and call the second. For example I have Screen A extending JFrame and Screen B extending JFrame, on a button that should switch to the next screen I would write something like that

JButton button = new JButton("show next screen");
button.addActionListener(){
    new ScreenB();
    frameA.dispose(); // assumed that we coded something like frameA = this earlier
}

Now the problem is that between the changeover of the two frames, I see for let's say 1/10 either Screen A or Screen B, which is very ugly. What would be a better solution? Extending the screens as Container and set them as ContentPane for the always the same JFrame?

Valentino Ru
  • 4,964
  • 12
  • 43
  • 78
  • 2
    *"A while ago, I read that the best way to switch between different "screens" in an application would be to set up different JFrames and then simply dipose the first and call the second."* It is wrong. Where did you read that? The OP in [this question](http://stackoverflow.com/q/9554636/418556) was referring to multiple frames on-screen simultaneously, but see my answer in any case for the multitude of alternatives, starting with `CardLayout`. – Andrew Thompson Oct 21 '12 at 05:53

2 Answers2

2

Try attaching a WindowListener to FrameA

In you action listener call frameA.dipsose(), so long as the frame set set to DISPOSE_ON_CLOSE it will trigger a windowClosed event in the WindowListener.

When this occurs, you can now open you new window.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
-3

You can call in another page to a div tag from JQuery like such:

$("#myDiveTag").load("http://mySecondPage.aspx");

and if you need to send variables you can do something like:

var myVar = $("#controlThatHasVariable");
$("#myDivTag").load("http://xxx.aspx?myData=" + myVar);

here is the syntax to the JQuery load method:

load("URI", data, callback)

In essence, you are disposing whats inside the div tag and replacing it with the page you are calling. You dont need all the tags and what not since the JQuery is inserting the page into an already existing page. so you can call a javascript function on your button onClick event and call the Jquery I posted above to repace the "screen" you have existing with what you call in the Jquery.

Argyle Ghost
  • 141
  • 5
  • 14