1

So I currently have a program that has 2 windows extending JFrame.

The Main window has a button opening a new window (Window 2). Window2 has its own defined class with widgets and listeners. At the moment if I press the button in the Main frame more than once whilst the second window is still open then it would switch to that window, without creating a new one (which is exactly what I want).

The 2nd window also has some widgets, a search field and a table that's populated according to what the user types in the JTextField. The problem is that once I close Window2 and press the button in the Main window to re-open it, the same window appears with the previously entered text in the search field and the populated table.

The thing is, I want to make it so that once Window2 is closed and re-opened then I create a completely new instance of Window2 with an empty search field and table. I thought this would work with JFrame.DISPOSE_ON_CLOSE but it didn't.

A bit of my code might explain this better:

public class MainWindow extends JFrame{

    /*
     * create widgets and panels in Main window
     */
    private Window2 ww = null;
    Button.addActionListener(new ActionListener() { // the button that opens 
                                                    //a new window

                @Override
                public void actionPerformed(ActionEvent e) {
                   if (ww==null) {  //creating the new window here
                       ww = new Window2();
                       ww.setTitle("Window2");
                       ww.setSize(600, 400);

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


/*
 * Window 2
 */

public class Window2 extends JFrame { 

//add widgets and listeners

pack();
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
user3281466
  • 491
  • 1
  • 7
  • 25
  • use CardLayout with JFrame.pack() – mKorbel Mar 05 '14 at 21:51
  • 1
    Don't use multiple JFrames - For more information look here http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice – jrowe08 Mar 05 '14 at 21:52
  • unfortunately I have to, it's a requirement for my school project. – user3281466 Mar 05 '14 at 22:08
  • 1
    What if you add a `WindowListener` (http://docs.oracle.com/javase/tutorial/uiswing/events/windowlistener.html) to `Window2` that calls a method on `Window1` when `Window2` closes. That method could set `ww` to null, which would force the `ActionListener` in `Window1` to create a new instance of `Window2` when the button is clicked. Let me know if this works, and I'll write it up properly, as an answer. – Dawood ibn Kareem Mar 05 '14 at 22:11

1 Answers1

3

There are a number of ways you might achieve this, the simplest would be to check to see if the window is visible or not, for example...

public void actionPerformed(ActionEvent e) {
    if (ww==null || !ww.isVisible()) {
        ww = new Window2();
        ww.setTitle("Window2");
        // You should be using pack
        ww.setSize(600, 400);
    }
    ww.setVisible(true);
}

Now, having said that, it's generally discouraged to use multiple frames in this way, as it's confusing to users, especially when they might already have multiple windows open doing other things, the other frames can become "lost".

You should consider using a modal dialog, forcing the user to complete the work with the dialog and close it when they have finished. This will stop the user from interacting with the parent window until the dialog is closed or make use of a CardLayout or JTabbedPane, allowing you to switch between views based on your needs.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 1
    +1 - this is a much better solution than the idea in my comment, although I had to think about whether there'd be some kind of resource leak (and I'm reasonably sure there wouldn't be). Not quite sure about the advice about modal dialogues - sometimes multiple frames are the best solution. I think we have to trust OP to know what she actually wants to implement. – Dawood ibn Kareem Mar 05 '14 at 23:54
  • @DavidWallace Agreed, a complete lack of context isn't helping, but we should also provide lessons learned from experience. These are just alternative possibilities which might help solver longer term issues or work flows. As developer and user, I know multiple frames are a complete pain in the back end... – MadProgrammer Mar 05 '14 at 23:57
  • Thanks a lot. Could you let me know why DISPOSE_ON_CLOSE wasn't working in this case? – user3281466 Mar 06 '14 at 20:35
  • 1
    `DISPOSE_ON_CLOSE` disposes of the native peer (think window handle) onto which the frame is rendered, it doesn't dispose of the instance of the `JFrame` object that you created - otherwise you would be running to `NullPointerExceptions`, but this isn't how Java works. You still have a strong reference to the `JFrame` through the `ww` variable, meaning that the object can't be garbage collected – MadProgrammer Mar 06 '14 at 20:53