0

Which Method can returns, how can I find number of opened Top-Level Containers

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SuperConstructor extends JFrame {

    private static final long serialVersionUID = 1L;

    public SuperConstructor() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setPreferredSize(new Dimension(300, 300));
        setTitle("Super constructor");
        Container cp = getContentPane();
        JButton b = new JButton("Show dialog");
        b.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                FirstDialog firstDialog = new FirstDialog(SuperConstructor.this);
            }
        });
        cp.add(b, BorderLayout.SOUTH);
        pack();
        setVisible(true);
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                SuperConstructor superConstructor = new SuperConstructor();
            }
        });
    }

    private class FirstDialog extends JDialog {

        private static final long serialVersionUID = 1L;

        FirstDialog(final Frame parent) {
            super(parent, "FirstDialog");
            setPreferredSize(new Dimension(200, 200));
            setLocationRelativeTo(parent);
            setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
            JButton bNext = new JButton("Show next dialog");
            bNext.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent evt) {
                    SecondDialog secondDialog = new SecondDialog(parent, false);
                }
            });
            add(bNext, BorderLayout.SOUTH);
            pack();
            setVisible(true);
        }
    }
    private int i;

    private class SecondDialog extends JDialog {

        private static final long serialVersionUID = 1L;

        SecondDialog(final Frame parent, boolean modal) {
            //super(parent); // Makes this dialog unfocusable as long as FirstDialog is visible
            setPreferredSize(new Dimension(200, 200));
            setLocation(300, 50);
            setModal(modal);
            setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            setTitle("SecondDialog " + (i++));
            JButton bClose = new JButton("Close");
            bClose.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent evt) {
                    setVisible(false);
                }
            });
            add(bClose, BorderLayout.SOUTH);
            pack();
            setVisible(true);
        }
    }
}
tshepang
  • 12,111
  • 21
  • 91
  • 136
mKorbel
  • 109,525
  • 20
  • 134
  • 319

2 Answers2

2

Window[] allWindows = Window.getWindows(); ?

StanislavL
  • 56,971
  • 9
  • 68
  • 98
1

Since J2EE uses the word "container" in a very specific meaning of the word, you are better off saying "how many top level dialogs" in your example. Note that in Swing "top" has a very specific meaning too, so there can only be one "top" item, the item that is (or would be) drawn on top of all other items. Since the answer "one" is likely not the answer you need, I'm going to guess you really meant "How many dialogs are opened?"

The way to count open dialogs is to add to your SuperConstructor class a member which can hold the "count" of opened dialogs. In your button's action listener, you create a new dialog. You can either put the code to increment the count in the class representing the dialog, or in the action listener that created the dialog. Either technique is fine, but if you need a preference, putting it in the action listener (embedded in the SuperConstructor class) is my preference.

If you need the count to be more than just a count of dialogs opened, you need to listen for dialog closing events and decrement the count as dialogs are closed.

Note that setting a dialog to be Visible is not the same as removing the dialog, so be careful to either look at visibility or existence (depending on your desired need) but don't write code which increments on existence but decrements on visibility (or vice versa).

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138