How do I create an undecorated JFrame?

This code give you an example of how to create a frame without the title bar, and the frame icons such as maximize, minimize and close.

package org.kodejava.swing;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

public class UndecoratedFrame {
    private static Point point = new Point();

    public static void main(String[] args) {
        final JFrame frame = new JFrame();

        // Disables or enables decorations for this frame. By setting undecorated
        // to true will remove the frame's title bar including the maximize,
        // minimize and the close icon.
        frame.setUndecorated(true);

        // As the the frame's title bar removed we need to close out frame for
        // instance using our own button.
        JButton button = new JButton("Close Me");
        button.addActionListener(e -> System.exit(0));

        // The mouse listener and mouse motion listener we add here is to simply
        // make our frame draggable.
        frame.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                point.x = e.getX();
                point.y = e.getY();
            }
        });

        frame.addMouseMotionListener(new MouseMotionAdapter() {
            public void mouseDragged(MouseEvent e) {
                Point p = frame.getLocation();
                frame.setLocation(p.x + e.getX() - point.x, p.y + e.getY() - point.y);
            }
        });

        frame.setSize(500, 500);
        frame.setLocation(200, 200);
        frame.setLayout(new BorderLayout());

        frame.getContentPane().add(button, BorderLayout.NORTH);
        frame.getContentPane().add(new JLabel("Drag Me", JLabel.CENTER), 
            BorderLayout.CENTER);
        frame.setVisible(true);
    }
}
An Undecorated JFrame

An Undecorated JFrame

How do I change JFrame image icon?

This code snippet demonstrates how to change a JFrame image icon using the setIconImage() method.

package org.kodejava.swing;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

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

        // Set the window size and its title
        frame.setSize(new Dimension(500, 500));
        frame.setTitle("Frame Icon Example");

        // Read the image that will be used as the application icon.
        // Using "/" in front of the image file name will locate the
        // image at the root folder of our application. If you don't
        // use a "/" then the image file should be on the same folder
        // with your class file.
        try {
            URL resource = frame.getClass().getResource("/logo.png");
            BufferedImage image = ImageIO.read(resource);
            frame.setIconImage(image);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Display the form
        frame.setVisible(true);
    }
}
Change JFrame Image Icon

Change JFrame Image Icon

How do I handle a window closing event in Swing?

Here you will see how to handle the window closing event of a JFrame. What you need to do is to implement a java.awt.event.WindowListener interface and call the frame addWindowListener() method to add the listener to the frame instance. To handle the closing event implements the windowClosing() method of the interface.

Instead of implementing the java.awt.event.WindowListener interface which require us to implement the entire methods defined in the interface, we can create an instance of WindowAdapter object and override only the method we need, which is the windowsClosing() method. Let’s see the code snippet below.

package org.kodejava.swing;

import javax.swing.JFrame;
import java.awt.Button;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class WindowClosingDemo extends JFrame {
    public static void main(String[] args) {
        WindowClosingDemo frame = new WindowClosingDemo();
        frame.setSize(new Dimension(500, 500));
        frame.add(new Button("Hello World"));

        // Add window listener by implementing WindowAdapter class to
        // the frame instance. To handle the close event we just need
        // to implement the windowClosing() method.
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("WindowClosingDemo.windowClosing");
                System.exit(0);
            }
        });

        // Show the frame
        frame.setVisible(true);
    }
}

How do I close a JFrame application?

Closing a JFrame application can be done by setting the default close operation of the frame. There are some defined constants for the default operation. These constants defined in the javax.swing.WindowConstants interface include EXIT_ON_CLOSE, HIDE_ON_CLOSE, DO_NOTHING_ON_CLOSE and DISPOSE_ON_CLOSE.

To exit an application we can set the default close operation to EXIT_ON_CLOSE, this option will call the System.exit() method when user initiate a close operation on the frame.

package org.kodejava.swing;

import javax.swing.JFrame;
import javax.swing.WindowConstants;

public class MainFrameClose extends JFrame {
    public static void main(String[] args) {
        MainFrameClose frame = new MainFrameClose();
        frame.setSize(500, 500);

        // Be defining the default close operation of a JFrame to
        // EXIT_ON_CLOSE the application will be exited by calling
        // System.exit() when user initiate a close event on the
        // frame.
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

How do I change the cursor shape in Swing?

Using the following code snippet you can change the shape of mouse cursor in your Java Swing desktop application. The cursor is represented by the java.awt.Cursor class. Create an instance of Cursor using the new operator and pass the cursor type to the Cursor class constructor.

We can change Swing’s objects (JLabel, JTextArea, JButton, etc) cursor using the setCursor() method. In the snippet below, for demonstration, we change the cursor of the JFrame. Your mouse pointer or cursor shape will be changed if you positioned inside the frame.

A collections of cursor shape defined in the java.awt.Cursor class, such as:

  • Cursor.DEFAULT_CURSOR
  • Cursor.HAND_CURSOR
  • Cursor.CROSSHAIR_CURSOR
  • Cursor.WAIT_CURSOR
  • etc
package org.kodejava.swing;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import java.awt.Cursor;

public class ChangeCursor extends JFrame {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            ChangeCursor frame = new ChangeCursor();
            frame.setTitle("Change Cursor");
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            frame.setSize(500, 500);

            // Here we create a hand shaped cursor!
            Cursor cursor = new Cursor(Cursor.HAND_CURSOR);
            frame.setCursor(cursor);
            frame.setVisible(true);
        });
    }
}