event

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:

  • Create a new InternalFrameListener.
  • Override the methods that correspond to the events that you want to monitor in the frame e.g internalFrameActivated, internalFrameClosed, internalFrameClosing, internalFrameDeactivated, internalFrameDeiconified, internalFrameIconified, internalFrameOpened and customize as you wish the handling of the respective events. Now every time an event occurs in the frame, the corresponding method will be executed.
  • Create a new JInternalFrame component and use the addInternalFrameListener to add the InternalFrameListener you’ve created.

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.setOpaque(false);

  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);

  jFrame.setVisible(true);
    }

    static JInternalFrame createLayer(String label) {

  return new InternalFrame(label);
    }

    static class InternalFrame extends JInternalFrame {

  InternalFrameListener listener = new InternalFrameListener() {

@Override

public void internalFrameActivated(InternalFrameEvent event) {

    printInfoOfEvent("Activated", event);

}

@Override

public void internalFrameClosed(InternalFrameEvent event) {

    printInfoOfEvent("Closed", event);

}

@Override

public void internalFrameClosing(InternalFrameEvent event) {

    printInfoOfEvent("Closing", event);

}

@Override

public void internalFrameDeactivated(InternalFrameEvent event) {

    printInfoOfEvent("Deactivated", event);

}

@Override

public void internalFrameDeiconified(InternalFrameEvent event) {

    printInfoOfEvent("Deiconified", event);

}

@Override

public void internalFrameIconified(InternalFrameEvent event) {

    printInfoOfEvent("Iconified", event);

}

@Override

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);

setName(str);

addInternalFrameListener(listener);

setBounds(100, 100, 100, 100);

setResizable(true);

setClosable(true);

setMaximizable(true);

setIconifiable(true);

setTitle(str);

setVisible(true);

  }
    }
}

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

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button