-3

I want to be able to detect when all instances of JInternalFrame in a JDesktopPane are closed. I've been looking through the events fired by desktop pane and I don't see one that is applicable. I've tried adding a JInternalFrameListener to each one added to the desktop pane and listening for a closed event but that doesn't seem to work.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user3877599
  • 47
  • 1
  • 7
  • 1
    Show the code where you're implementing it, then maybe someone can be more able to help you. – Frakcool Sep 22 '15 at 17:40
  • 1
    See [`JInternalFrame.addInternalFrameListener(InternalFrameListener)`](http://docs.oracle.com/javase/8/docs/api/javax/swing/JInternalFrame.html#addInternalFrameListener-javax.swing.event.InternalFrameListener-) for the closing of one window. Keep a list of the internal frames and remove them from the list when closed. *"but that doesn't seem to work."* For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Sep 22 '15 at 17:50

1 Answers1

0

Here a working example:

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

public class InternalFrameDemo extends JFrame {
    JDesktopPane desktop;
    InternalFrameAdapter adapter;
    int frameCount = 0;

    public InternalFrameDemo() {
        super("InternalFrameDemo");

        adapter = new InternalFrameAdapter() {
            public void internalFrameClosed(InternalFrameEvent e) {
                frameCount--;
                if (frameCount == 0) {
                    JOptionPane.showMessageDialog(InternalFrameDemo.this, "All internal frames closed.");
                }
            }
        };

        setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);

        desktop = new JDesktopPane();
        createFrame();
        createFrame();
        createFrame();
        setContentPane(desktop);
    }

    protected void createFrame() {
        JInternalFrame frame = new JInternalFrame("title", true, true);
        frame.setSize(300,300);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        frameCount++;
        frame.addInternalFrameListener(adapter);
        desktop.add(frame);
    }

    private static void createAndShowGUI() {
        InternalFrameDemo frame = new InternalFrameDemo();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } 
        catch (UnsupportedLookAndFeelException e) {
           // handle exception
        }
        catch (ClassNotFoundException e) {
           // handle exception
        }
        catch (InstantiationException e) {
           // handle exception
        }
        catch (IllegalAccessException e) {
           // handle exception
        }
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

You basically add a listener on each internal frame and keep track of opened and closed frames (with an int variable).

Henri Benoit
  • 705
  • 3
  • 10