How to use InternalFrameListener in the Swing system?

Sep 22, 2025|

Hey there! As a supplier of swing systems, I often get asked about how to use InternalFrameListener in the Swing system. It's a pretty cool feature, and I'm gonna break it down for you in this blog post.

First off, let's talk about what the Swing system is. The Swing system is a set of GUI (Graphical User Interface) components for Java. It's used to create all sorts of desktop applications, from simple text editors to complex business software. And as a swing system supplier, we offer a range of products like the Swing Platform for Excavator, Swing System Assembly for Excavator, and Slewing Ring Bearings. But in this post, we're gonna focus on the programming side of things, specifically the InternalFrameListener.

5Swing System Assembly For Excavator

So, what's an InternalFrameListener? Well, in the Swing system, an internal frame is like a window within a window. It's a lightweight container that can be moved, resized, and closed just like a regular window. And an InternalFrameListener is an interface that allows you to listen for events that happen to an internal frame, like when it's opened, closed, iconified, de - iconified, or activated.

Let's start with the basics of implementing an InternalFrameListener. First, you need to create a class that implements the InternalFrameListener interface. This interface has five methods that you have to override:

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

class MyInternalFrameListener implements InternalFrameListener {

    @Override
    public void internalFrameOpened(InternalFrameEvent e) {
        System.out.println("Internal frame opened");
    }

    @Override
    public void internalFrameClosing(InternalFrameEvent e) {
        System.out.println("Internal frame closing");
    }

    @Override
    public void internalFrameClosed(InternalFrameEvent e) {
        System.out.println("Internal frame closed");
    }

    @Override
    public void internalFrameIconified(InternalFrameEvent e) {
        System.out.println("Internal frame iconified");
    }

    @Override
    public void internalFrameDeiconified(InternalFrameEvent e) {
        System.out.println("Internal frame de - iconified");
    }

    @Override
    public void internalFrameActivated(InternalFrameEvent e) {
        System.out.println("Internal frame activated");
    }

    @Override
    public void internalFrameDeactivated(InternalFrameEvent e) {
        System.out.println("Internal frame de - activated");
    }
}

In the code above, we've created a simple class MyInternalFrameListener that implements the InternalFrameListener interface. Each method corresponds to a different event that can happen to an internal frame. For example, the internalFrameOpened method is called when the internal frame is opened, and the internalFrameClosing method is called when the user tries to close the internal frame.

Now, let's see how to use this listener with an actual internal frame. Here's a simple example of creating an internal frame and adding our listener to it:

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

public class InternalFrameExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Internal Frame Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 600);

        JDesktopPane desktopPane = new JDesktopPane();
        frame.setContentPane(desktopPane);

        JInternalFrame internalFrame = new JInternalFrame("My Internal Frame", true, true, true, true);
        internalFrame.setSize(300, 200);
        internalFrame.setVisible(true);

        MyInternalFrameListener listener = new MyInternalFrameListener();
        internalFrame.addInternalFrameListener(listener);

        desktopPane.add(internalFrame);

        frame.setVisible(true);
    }
}

In this code, we first create a main JFrame. Then we create a JDesktopPane, which is a container for internal frames. We create an internal frame, set its properties (like being resizable, closable, iconifiable, and maximizable), and make it visible. We then create an instance of our MyInternalFrameListener and add it to the internal frame using the addInternalFrameListener method. Finally, we add the internal frame to the desktop pane and make the main frame visible.

Now, let's talk about some practical uses of the InternalFrameListener. One common use is to perform certain actions when an internal frame is closed. For example, you might want to save some data or release some resources. Here's how you can modify the internalFrameClosing method to prompt the user to save changes:

@Override
public void internalFrameClosing(InternalFrameEvent e) {
    JInternalFrame frame = (JInternalFrame) e.getSource();
    int option = JOptionPane.showConfirmDialog(frame, "Do you want to save changes?", "Save Changes", JOptionPane.YES_NO_CANCEL_OPTION);
    if (option == JOptionPane.YES_OPTION) {
        // Code to save changes
        System.out.println("Saving changes...");
        frame.dispose();
    } else if (option == JOptionPane.NO_OPTION) {
        frame.dispose();
    }
}

In this code, when the user tries to close the internal frame, a dialog box appears asking if they want to save changes. Depending on their choice, we can either save the changes and close the frame or just close the frame without saving.

Another use case is to manage the state of multiple internal frames. For example, you might want to disable other internal frames when one is activated. You can do this in the internalFrameActivated method:

@Override
public void internalFrameActivated(InternalFrameEvent e) {
    JInternalFrame activatedFrame = (JInternalFrame) e.getSource();
    JDesktopPane desktopPane = (JDesktopPane) activatedFrame.getParent();
    JInternalFrame[] allFrames = desktopPane.getAllFrames();
    for (JInternalFrame frame : allFrames) {
        if (frame != activatedFrame) {
            frame.setEnabled(false);
        }
    }
}

@Override
public void internalFrameDeactivated(InternalFrameEvent e) {
    JInternalFrame deactivatedFrame = (JInternalFrame) e.getSource();
    JDesktopPane desktopPane = (JDesktopPane) deactivatedFrame.getParent();
    JInternalFrame[] allFrames = desktopPane.getAllFrames();
    for (JInternalFrame frame : allFrames) {
        frame.setEnabled(true);
    }
}

In the internalFrameActivated method, we get the activated frame, find all the other frames in the desktop pane, and disable them. In the internalFrameDeactivated method, we enable all the frames again.

As a swing system supplier, we understand that having a good understanding of these programming concepts can help you build better applications that use our products. Whether you're working on an application for an excavator's swing system or something else entirely, the Swing system and its features like the InternalFrameListener can give you a lot of flexibility.

If you're interested in our swing system products such as the Swing Platform for Excavator, Swing System Assembly for Excavator, or Slewing Ring Bearings, and want to discuss your specific requirements, feel free to reach out for a procurement discussion. We're here to help you find the best solutions for your projects.

References:

  • "Java Swing: A Beginner's Guide"
  • Oracle Java Documentation on Swing Components and Event Handling
Send Inquiry