How to get list of opened JFrame in Java Swing?

UPDATED: 22 June 2014

Swing
Swing is built on base of Abstract Window Toolkit. Swing is used to create desktop application. Swing is using AWT directly or indirectly to provide you richer user interface. Swing is placed under javax.swing

JFrame can be accessible by its parent.



Understanding the Diagram
As you can see I created boundary around Parent JFrame, Child JFrame 1 and Child JFrame 2. It means you can access list of JFrame initiated by ParentJFrame. You can't access Standalone JFrame in Parent JFrame.


Frame.getFrames()
This method will return list of all Frame Object created by its parent and parent it self.


Parent JFrame Source Code
import java.awt.Frame;

public class ParentJFrame extends javax.swing.JFrame {

    /* Creates new form ParentJFrame */
    public ParentJFrame() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // //GEN-BEGIN:initComponents
    private void initComponents() {

        lblInfo = new javax.swing.JLabel();
        btnOpenChildJFrame = new javax.swing.JButton();
        btnListJFrames = new javax.swing.JButton();
        scrollPane = new javax.swing.JScrollPane();
        textArea = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Parent JFrame");

        lblInfo.setText("Parent JFrame");

        btnOpenChildJFrame.setText("Open Child JFrame");
        btnOpenChildJFrame.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOpenChildJFrameActionPerformed(evt);
            }
        });

        btnListJFrames.setText("List JFrame");
        btnListJFrames.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnListJFramesActionPerformed(evt);
            }
        });

        textArea.setColumns(20);
        textArea.setRows(5);
        scrollPane.setViewportView(textArea);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
                    .addComponent(lblInfo)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnOpenChildJFrame)
                        .addGap(18, 18, 18)
                        .addComponent(btnListJFrames)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblInfo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnOpenChildJFrame)
                    .addComponent(btnListJFrames))
                .addGap(18, 18, 18)
                .addComponent(scrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// //GEN-END:initComponents

    /**
     * Open Child JFrame button click event
     */
    private void btnOpenChildJFrameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOpenChildJFrameActionPerformed
        /* Create an object of childJFrame */
        childJFrame child = new childJFrame();
        /* Make childJFrame visible */
        child.setVisible(true);
    }//GEN-LAST:event_btnOpenChildJFrameActionPerformed

    /**
     * List JFrame button click event
     */
    private void btnListJFramesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnListJFramesActionPerformed
        /* Get previous text of JFrame */
        String previousText = textArea.getText();
        String strNewFrames = "";
        /* It will give you list of JFrame created/Initiated by ParentJFrame */
        for(Frame frame: Frame.getFrames()){
            strNewFrames += frame.getTitle() + "\n";
        }
        /* Set text of JTextArea */
        textArea.setText(previousText+strNewFrames);
    }//GEN-LAST:event_btnListJFramesActionPerformed

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

            public void run() {
                new ParentJFrame().setVisible(true);
            }
        });
    }

    private javax.swing.JButton btnListJFrames;
    private javax.swing.JButton btnOpenChildJFrame;
    private javax.swing.JLabel lblInfo;
    private javax.swing.JScrollPane scrollPane;
    private javax.swing.JTextArea textArea;
}

Child JFrame Source Code
public class childJFrame extends javax.swing.JFrame {

    /** Creates new form childJFrame */
    public childJFrame() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // //GEN-BEGIN:initComponents
    private void initComponents() {

        lblInfo = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Child JFrame");

        lblInfo.setText("Child JFrame");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblInfo)
                .addContainerGap(188, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblInfo)
                .addContainerGap(91, Short.MAX_VALUE))
        );

        pack();
    }// //GEN-END:initComponents

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

            public void run() {
                new childJFrame().setVisible(true);
            }
        });
    }
    private javax.swing.JLabel lblInfo;
}
I used NetBeans to create sample code. I explained code that relate the title of article. Major code done only in Parent JFrame. Child JFrame only supports example.

0 comments :