How do i find if a window is opened on swing
Asked Answered
A

3

9

I have a problem with my application where the user will open more than one window at a time. And i have added dispose() method to call on closing the window. Now i should keep at-least one window open all the time so that the application does not hides without closed fully. If you don't understand read the following scenario:

I have window A and window B opened at the same time. Now i can close either window A or Window B but not both. In other words window B should be allowed to close only if window A is opened and vice versa. How do i do this in swing ??

Auxochrome answered 4/5, 2011 at 22:34 Comment(0)
S
16

A simple kind-of windowManger is not really tricky, all you need is

  • WindowListener which keeps tracks of the Windows it's listening to
  • a defined place to create the windows and register the the listener
  • make the windows do-nothing-on-close and make the listener responsible for the decision of whether to close or not (will do so for all except the last)

Some snippet:

    // the listener (aka: WindowManager)
    WindowListener l = new WindowAdapter() {
        List<Window> windows = new ArrayList<Window>();

        @Override
        public void windowOpened(WindowEvent e) {
            windows.add(e.getWindow());
        }

        @Override
        public void windowClosing(WindowEvent e) {
            if (windows.size() > 1) {
                windows.remove(e.getWindow());
                e.getWindow().dispose();
            }
        }
    };
    // create the first frame
    JFrame frame = createFrame(l);
    frame.setVisible(true);


// a method to create a new window, config and add the listener
    int counter = 0;
    private JFrame createFrame(final WindowListener l) {
        Action action = new AbstractAction("open new frame: " + counter) {

            @Override
            public void actionPerformed(ActionEvent e) {
                JFrame frame = createFrame(l);
                frame.setVisible(true);

            }
        };
        JFrame frame = new JFrame("someFrame " + counter++);
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        frame.add(new JButton(action));
        frame.addWindowListener(l);
        frame.pack();
        frame.setLocation(counter * 20, counter * 10);
        return frame;
    }
Shiner answered 4/5, 2011 at 23:54 Comment(2)
+1 for Action.Moltke
If you're trying to keep track of multiple windows, a WindowManager class implemented with the Singleton pattern might be useful.Wirehaired
U
4

Just a possible approach...

Create a class, call it WindowManager, that manages creation and disposal of windows.

It could for example retain the count of the windows currently open, and allow a dispose operation only if there are more than one windows "alive", otherwise show a confirm message with JOptionPane telling the user "Really close? That would terminate the application." or something like that.

The "tricky" part is that you have to do this kind of window-related operations throughout the WindowManager, otherwise everything would screw up.

Dunno if Swing has something like this built-in, I've never seen such a scenario.

Ullage answered 4/5, 2011 at 22:42 Comment(3)
The "tricky" part is that you have to do any window-related operation throughout the WindowManager, otherwise everything would screw up. What do you mean by this ? I understood tat window-related operations as opening and closing.. Do i have to worry about resize, move etc ?Auxochrome
No no, I was just pointing at the fact that when the user clicks the 'x' button of the window, you have to handle this from within the window class, and then delegate to the WindowManager the actual logic of the operation. This is somewhat tricky, but works. And more, when a new has to be created, this has to be done by the manager or at least notified to the manager. Kinda boring. [edited the answer]Ullage
Let me see if i can find some built-in features... if no other go i should implement your idea.. notify me if you find something relatively easier.. thanks!!Auxochrome
L
1

simply check if the other window is open before closing with window.isVisible();

Leopard answered 4/5, 2011 at 23:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.