Minimize and Maximize JFrame Programmatically in Java



In this article, we will learn to minimize/maximize a JFrame programmatically in Java. In Swing, programmers often need to resize the window as needed. For example, they can shrink the window when carrying out background tasks or expand the window size for a better full-screen experience.

What is a JFrame?

A JFrame class is a subclass of Frame class and the components added to a frame are referred to as its contents, these are managed by the contentPane. A JFrame contains a window with title, border, (optional) menu bar and user-specific components.

By default, we can minimize a JFrame by clicking on minimize button and maximize a JFrame by clicking on maximize button at the top-right position of the screen.

Minimizing a JFrame

To minimize the JFrame size, we will use the setState(JFrame.ICONIFIED). This action reduces the window to an icon, usually on the operating system's taskbar.

Syntax

The following is the syntax:

setState(JFrame.ICONIFIED);

Maximizing a JFrame

To maximize a window to its full capacity, we shall be using methods like JFrame.MAXIMIZED_BOTH, JFrame.MAXIMIZED_VERT, and JFrame.MAXIMIZED_HORIZ, the most used among them is the JFrame.MAXIMIZED_BOTH.

Syntax

The following is the syntax:

setExtendedState(JFrame.MAXIMIZED_BOTH);

Minimizing and Maximizing a JFrame

We have a class named JFrameIconifiedTest that extends the JFrame class. Two JButton instances are added to the frame within this class: the maximizeButton and the iconifyButton. The JFrame may be minimized or maximized by the user pressing these buttons.

The following are the steps to minimizing and maximizing a JFrame in Java:

  • The JFrameIconifiedTest constructor initializes two buttons and adds them to the frame in the North and South regions, respectively.
  • An ActionListener is registered for each button, linking button actions to the actionPerformed(...) method.
  • When iconifyButton is clicked, setState(JFrame.ICONIFIED) is called, minimizing the window.
  • When maximizeButton is clicked, setExtendedState(JFrame.MAXIMIZED_BOTH) is called, maximizing the window.
  • The main method simply creates an instance of JFrameIconifiedTest, causing the GUI to appear.

Example

Below is an example of minimizing and maximizing a JFrame in Java:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JFrameIconifiedTest extends JFrame implements ActionListener {
   private JButton iconifyButton, maximizeButton;
   public JFrameIconifiedTest() {
      setTitle("JFrameIconified Test");
      iconifyButton = new JButton("JFrame Iconified");
      add(iconifyButton, BorderLayout.NORTH);
      iconifyButton.addActionListener(this);
      maximizeButton = new JButton("JFrame Maximized");
      add(maximizeButton, BorderLayout.SOUTH);
      maximizeButton.addActionListener(this);
      setSize(400, 275);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLocationRelativeTo(null);
      setVisible(true);
   }
   public void actionPerformed(ActionEvent ae) {
      if(ae.getSource().equals(iconifyButton)) {
         setState(JFrame.ICONIFIED); // To minimize a frame
      } else if(ae.getSource().equals(maximizeButton)) {
         setExtendedState(JFrame.MAXIMIZED_BOTH); // To maximize a frame
      }
   }
   public static void main(String args[]) {
      new JFrameIconifiedTest();
   }
}

Output

If we click on "JFrame Iconified" button, the frame is minimized and click on "JFrame Maximized" button, the frame is maximized.

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-04-15T19:14:55+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements