
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Disable Maximize Button of a JFrame in Java
In this article, we will learn to disable the maximize button of a JFrame in Java. When creating Swing GUIs in Java, we might occasionally want to prevent users from maximizing certain windows. Removing the maximize button will do the trick for dialog windows, tool windows, or any window where a fixed size needs to be kept.
What is a JFrame?
A JFrame is a class from javax. swing package and it can extend java.awt.frame class. It is a top-level window with a border and a title bar. A JFrame class has many methods that can be used to customize it.
After setting the size of a JFrame, we can still change the size by putting the cursor at the corners and dragging it or if we press resize option next to close at the top right corner, it will maximize to the size of a full screen. This happens because resize is set to true by default for the JFrame class.
Different Approaches
The following are the two distinct methods to disable the maximize button of a JFrame in Java:
Using setResizable() Method
This is the simplest and most common method. Setting setResizable(false) prevents the user from resizing the window, which implicitly disables the maximize button.
Syntax
The following is the syntax for setResizable():
JFrame frame = new JFrame("My Frame"); frame.setResizable(false); // Disables resizing and maximize button
Example
Below is an example of disabling the maximize button of a JFrame using the setResizable() method:
import java.awt.*; import javax.swing.*; public class JFrameDemo extends JFrame { String title; public JFrameDemo(String title) { super(title); add(new JLabel("JFrame with maximize button disabled"), BorderLayout.CENTER); setSize(350, 275); setLocationRelativeTo(null); setResizable(false); // maximize button disable setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public static void main(String args[]) { new JFrameDemo("JFrame Demo"); } }
Output
Using setUndecorated() Method
For more fine-grained control, we can set setUndecorated(true) to remove all default window decorations. This leaves us with an empty space to construct our own title bar and window controls.
We would also need to implement our own drag and drop mechanism for the window and maybe even define our own minimize and close buttons.
Syntax
The following is the syntax for setResizable():
JFrame frame = new JFrame(""); frame.setUndecorated(true); // Remove all native window decorations
Example
Below is an example of disabling the maximize button of a JFrame using the setUndecorated() method:
import javax.swing.*; public class UndecoratedFrameExample { public static void main(String[] args) { JFrame frame = new JFrame("JFrame Demo"); frame.setUndecorated(true); // Remove native window decorations frame.setSize(350, 275); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new JLabel("Custom undecorated JFrame", SwingConstants.CENTER)); frame.setVisible(true); } }