Determine When a Frame or Window is Closing in Java



In this article, we will learn to determine when a Frame or Window is closing in Java. We will create a simple window in Java Swing titled "Demo". It displays styled text (italic, black on orange) using a text pane inside a scrollable area. The program also listens for window-closing events, printing a message when you close it. This basic example is how to build a GUI with text styling and event handling.

Steps to determine when a Frame or Window is closing

Following are the steps to determine when a Frame or Window is closing in Java ?

  • Import necessary classes from java.awt, javax.swing, and other packages to create the GUI and handle events.
  • Initialize a JFrame with the title Demo and set its default close operation.
  • Implement a WindowListener using a WindowAdapter to listen for window-closing events and print a message when the window is closed.
  • Get the content pane of the frame and create a JTextPane for displaying text with custom attributes.
  • We will use SimpleAttributeSet to set the text as italic with specific foreground and background colors.
  • Then we will add the JTextPane inside a JScrollPane for scrolling functionality and add it to the frame.
  • Set the frame's size, add the window listener, and make the frame visible.

Java program to determine when a Frame or Window is closing

The following is an example to determine when a Frame or Window is closing in Java ?

package my;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
public class SwingDemo {
   public static void main(String args[]) throws BadLocationException {
      JFrame frame = new JFrame("Demo");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      WindowListener listener = new WindowAdapter() {
         public void windowClosing(WindowEvent evt) {
            Frame frame = (Frame) evt.getSource();
            System.out.println("Closing = "+frame.getTitle());
         }
      };
      Container container = frame.getContentPane();
      JTextPane pane = new JTextPane();
      SimpleAttributeSet attributeSet = new SimpleAttributeSet();
      StyleConstants.setItalic(attributeSet, true);
      StyleConstants.setForeground(attributeSet, Color.black);
      StyleConstants.setBackground(attributeSet, Color.orange);
      pane.setCharacterAttributes(attributeSet, true);
      pane.setText("This is a demo text!");
      JScrollPane scrollPane = new JScrollPane(pane);
      container.add(scrollPane, BorderLayout.CENTER);
      frame.setSize(550, 300);
      frame.addWindowListener(listener); frame.setVisible(true);
   }
}

Output

The output is as follows. The following is visible on the console ?

Code Explanation

This Java program creates a simple interactive GUI using Swing, where you'll see a window titled "Demo". Inside, it displays some text styled in italics with a black color on an orange background. We use a JTextPane for this, wrapped in a JScrollPane so you can scroll if the text gets too long. The window listens for when you close it thanks to a WindowListener and prints a message saying it's closing.

Updated on: 2024-08-30T11:43:36+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements