How do I disable JFrame close button?

To disable the close operation on a JFrame; when user click the close icon on the JFrame; we can set the value of the default close operation to JFrame.DO_NOTHING_ON_CLOSE.

package org.kodejava.example.swing;

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

public class DisableCloseButtonDemo extends JFrame {
    public DisableCloseButtonDemo() throws HeadlessException {

    private void initialize() {
        // JFrame.DO_NOTHING_ON_CLOSE tell JFrame instance to do nothing
        // when a window closing event occurs.

        JButton button = new JButton("Close");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

        this.setLayout(new FlowLayout(FlowLayout.CENTER));
        this.setSize(new Dimension(100, 100));

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new DisableCloseButtonDemo().setVisible(true);


Programmer, runner, recreational diver, live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. Support me, buy me ☕ or 🍵

Leave a Reply