java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to 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 com.javacoderanch.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);