0

I've looked through topics on how to open only one window when a button is clicked but none of the solutions there helped, perhaps because my code was structured a bit differently.

So I have a main window class extending JFrame and one of the buttons is supposed to open a new window when clicked. I have defined the widgets/panels etc for the new window in a separate class. At the moment, every time I click on the button a new window is opened. I want to make it so that if a window is already opened then it would switch to that window once the button is clicked again.

Here is a bit of my code:

public class MainWindow extends JFrame{

    /*
     * create widgets and panels
     */
    Button.addActionListener(new ActionListener() { // the button that opens 
                                                    //a new window

                @Override
                public void actionPerformed(ActionEvent e) {
                    Window2 ww = new Window2(); //creating the new window here

                }
            });
    }

NB. The Window2 class is also extending JFrame, if that's of any help.

Thanks

user3281466
  • 491
  • 1
  • 7
  • 25
  • you have to store your Window2 object in your MainWindow class, you create/show/put in foreground when your action is performed – Athanor Mar 04 '14 at 11:16
  • *"I've looked through topics on how to open only one window.."* What are the links to those topics? – Andrew Thompson Mar 04 '14 at 11:47
  • *"the Window2 class is also extending JFrame,.."* Don't extend frame, and never have two of them. See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) Note that if the 2nd 'window' is actually a modal `JDialog`, it will not be possible for the user to open it twice at the same time. – Andrew Thompson Mar 04 '14 at 11:49

3 Answers3

2

pull out ojbect creation from actionPerformed method beacuse each time you click button it's create new object. below can help you :-

  1. Make a Window2 class singalton for more detail about singalton click here.

2 . add null check as below :-

    ....
Window2 ww = null; // static or instence variable
......
@Override
public void actionPerformed(ActionEvent e) {
if(ww==null)
{
     ww = new Window2(); 
     ww.someMethod();

}
else
{
   ww.someMethod();

}

} });

Anchit Pancholi
  • 1,174
  • 4
  • 14
  • 40
  • I already tried this with exactly the same if statement but I didn't know what to putin the else sstatement so that it switches to the opened window. It kept opening new windows when I clicked the button though, shouldn't it just do nothing? – user3281466 Mar 04 '14 at 15:03
1

Here is a full working example:

Window2.java

public class Window2 extends JFrame {

    private static final long serialVersionUID = 7843480295403205677L;

}

MainWindow.java

public class MainWindow extends JFrame {

    private static final long serialVersionUID = -9170930657273608379L;

    public static void main(String[] args) {
        MainWindow mw = new MainWindow();
        mw.go();
    }

    private void go() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }

    private void createAndShowGUI() {
        JButton button = new JButton("Click me");

        button.addActionListener(new ActionListener() {

            private Window2 ww = null;

            @Override
            public void actionPerformed(ActionEvent e) {
                if (ww==null) {
                    ww = new Window2(); //creating the new window here
                    ww.setDefaultCloseOperation(HIDE_ON_CLOSE);
                    ww.setTitle("Window2 created on " + new Date());
                    ww.setSize(500, 200);
                }

                pack();
                ww.setVisible(true);
            }
        });

        setLayout(new BorderLayout());
        add(button);

        setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        pack();
        setVisible(true);
    }
}
Stephan
  • 41,764
  • 65
  • 238
  • 329
  • I already tried this with exactly the same if statement but I didn't know what to putin the else sstatement so that it switches to the opened window. It kept opening new windows when I clicked the button though, shouldn't it just do nothing? – user3281466 Mar 04 '14 at 15:07
  • 1
    @user3281466 I have updated my answer with a full working example. – Stephan Mar 04 '14 at 15:39
  • Ah thanks man, I had nearly the same code but it seems that it didn't work because I had defined setVisible(true) in the Window2 class and I didn't explicitly invoke it on the ww object in the actionPerformed method. Thank you. – user3281466 Mar 04 '14 at 20:47
0

What you can try is make two windows and put the actionPeformed method in the main class so that when the button is pressed it displays the second window

Luke Melaia
  • 1,470
  • 14
  • 22