Java InternalFrameListener handle internal frame event

Description

Java InternalFrameListener handle internal frame event


import java.awt.BorderLayout;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameListener;

public class Main {
   public static void main(final String[] args) {
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      JDesktopPane desktop = new JDesktopPane();
      JInternalFrame internalFrame = new JInternalFrame("Test", true, true, true, true);

      desktop.add(internalFrame);/*  ww w .  j  a v  a 2 s . c  o m*/

      internalFrame.setBounds(25, 25, 200, 100);

      JLabel label = new JLabel(internalFrame.getTitle(), JLabel.CENTER);
      internalFrame.add(label, BorderLayout.CENTER);

      internalFrame.setVisible(true);

      internalFrame.addInternalFrameListener(new InternalFrameListener() {

         public void internalFrameClosing(InternalFrameEvent e) {
            displayMessage("Internal frame closing", e);
         }

         public void internalFrameClosed(InternalFrameEvent e) {
            displayMessage("Internal frame closed", e);
         }

         public void internalFrameOpened(InternalFrameEvent e) {
            displayMessage("Internal frame opened", e);
         }

         public void internalFrameIconified(InternalFrameEvent e) {
            displayMessage("Internal frame iconified", e);
         }

         public void internalFrameDeiconified(InternalFrameEvent e) {
            displayMessage("Internal frame deiconified", e);
         }

         public void internalFrameActivated(InternalFrameEvent e) {
            displayMessage("Internal frame activated", e);
         }

         public void internalFrameDeactivated(InternalFrameEvent e) {
            displayMessage("Internal frame deactivated", e);
         }

         void displayMessage(String prefix, InternalFrameEvent e) {
            System.out.println(prefix + ": " + e.getSource());
         }

      });

      desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);

      frame.add(desktop, BorderLayout.CENTER);
      frame.setSize(500, 300);
      frame.setVisible(true);
   }
}



PreviousNext

Related