- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to disable close button on a JFrame in Java?
To disable the close button, let us first create a frame −
JFrame frame = new JFrame("Login!");
Use the setDefaultCloseOperation() to set the status of the close button. Set it to DO_NOTHING_ON_CLOSE to disable it −
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
The following is an example to disable close button on a JFrame in Java −
Example
package my; import java.awt.GridLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.SwingConstants; public class SwingDemo { public static void main(String[] args) throws Exception { JFrame frame = new JFrame("Login!"); JLabel label1, label2, label3; frame.setLayout(new GridLayout(2, 2)); label1 = new JLabel("DeptId", SwingConstants.CENTER); label2 = new JLabel("SSN", SwingConstants.CENTER); label3 = new JLabel("Password", SwingConstants.CENTER); JTextField deptid = new JTextField(20); JTextField ssn = new JTextField(20); JPasswordField passwd = new JPasswordField(); passwd.setEchoChar('*'); frame.add(label1); frame.add(label2); frame.add(label3); frame.add(deptid); frame.add(ssn); frame.add(passwd); frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); frame.setSize(550, 400); frame.setVisible(true); } }
The output is as follows displaying the close button but it won’t work since we disabled it above −
Output
- Related Articles
- How to close JFrame on the click of a Button in Java
- How can we disable the maximize button of a JFrame in Java?
- How to set default button for JFrame in Java?
- How to Disable / Enable a Button in TKinter?
- How to set the location of a button anywhere in JFrame?
- Java Program to draw a line on a JFrame in Java
- Disable a Bootstrap Button
- Can we disable JComboBox arrow button in Java?
- How to close a Tkinter window by pressing a Button?
- Disable a button with Bootstrap
- How to maximize JFrame in Java Swing
- How to create a Titleless and Borderless JFrame in Java?
- How to activate and deactivate JFrame in Java
- How to change JFrame background color in Java
- How to set FlowLayout for JFrame in Java?

Advertisements