I'm working in this program in this the user is asked, for different types of data, but each type of data depends of the previous ones. So I created three frames, in which I place the first quiz, and now what I want is to open a new window, when the user press a button (Next) which at the same time close the first one. What I've tried so far is to change a Boolean variable when the user press that button (Next), however, I don't know how to make reference to this last variable from a new class. I'm putting the trouble in context because may you can give me a better approach tho solve this.
3 Answers
Opening and closing windows isn't complicated and involves nothing more than creating the window instances (such as a JFrame or JDialog) and calling setVisible(true)
or setVisible(false)
on them. But having said that, how many commercial applications do you use where windows are thrown willy nilly at the user? Few because it's a user interface nightmare. Instead do your user's a favor and swap views with a CardLayout, and for the instance where you occasionally need to get information modally ,use a modal dialog such as a JDialog set to modal or a JOptionPane.

- 283,665
- 25
- 256
- 373
I think you can do it by typing these lines in (Next) button's ActionListener method:
dispose();
NewJFrame n = NweJFrame();
n.setVisible(true);
The line dispose();
will close your previous window and your another new window suppose named NewJFrame will be opened by this two line
NewJFrame n = NweJFrame();
n.setVisible(true);
But before that you must have to write the code of NewJFrame as you have written for your previous window.

- 4,944
- 9
- 39
- 49
this code was only half successfull.
Form2 f2 = new Form2();
if (f2 is Form2)
{
f2.Show();
this.Close();
}
else
{
Form1 f1 = new Form1();
f1.Show();
}

- 63,336
- 16
- 89
- 104

- 79
- 1
-
I'm not sure your answer is relevant. Your code looks like C#, as it uses the `is` operator, has `{`s on new lines and has method names beginning with capital letters. However, this is a Java question. – Luke Woodward Nov 24 '12 at 12:28