InternalFrameListener example

In this example we are going to see how to use InternalFrameListener in Java. When you are developing an application with a vivid Graphical environment it is very possible that you will use JInternalFrame components to enhance the end user’s experience. As you might image the monitoring an handling of the several events that occur in the life cycle of the internal frame is very important in this case.

In short, to use an InternalFrameListener, one should follow these steps:

Let’s take a closer look at the code snippet that follows:

package com.javacodegeeks.snippets.desktop;
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameListener;
public class InternalFrameListenerExample {
    public static void main(String args[]) {
  JFrame jFrame = new JFrame();
  Container cPane = jFrame.getContentPane();
  JLayeredPane layerPane = new JDesktopPane();
  layerPane.add(createLayer("First"), JLayeredPane.POPUP_LAYER);
  layerPane.add(createLayer("Seond"), JLayeredPane.DEFAULT_LAYER);
  layerPane.add(createLayer("Third"), JLayeredPane.PALETTE_LAYER);
  cPane.add(layerPane, BorderLayout.CENTER);
  jFrame.setSize(800, 600);
    static JInternalFrame createLayer(String label) {
  return new InternalFrame(label);
    static class InternalFrame extends JInternalFrame {
  InternalFrameListener listener = new InternalFrameListener() {
public void internalFrameActivated(InternalFrameEvent event) {
    printInfoOfEvent("Activated", event);
public void internalFrameClosed(InternalFrameEvent event) {
    printInfoOfEvent("Closed", event);
public void internalFrameClosing(InternalFrameEvent event) {
    printInfoOfEvent("Closing", event);
public void internalFrameDeactivated(InternalFrameEvent event) {
    printInfoOfEvent("Deactivated", event);
public void internalFrameDeiconified(InternalFrameEvent event) {
    printInfoOfEvent("Deiconified", event);
public void internalFrameIconified(InternalFrameEvent event) {
    printInfoOfEvent("Iconified", event);
public void internalFrameOpened(InternalFrameEvent event) {
    printInfoOfEvent("Opened", event);
private void printInfoOfEvent(String str, InternalFrameEvent event) {
    System.out.println("Source: " + event.getInternalFrame().getName()
+ " : " + str);
  public InternalFrame(String str) {
getContentPane().add(new JLabel(str, JLabel.CENTER),BorderLayout.CENTER);
setBounds(100, 100, 100, 100);

This was an example on how to use InternalFrameListener in Java.

Exit mobile version