How can we minimize/maximize a JFrame programmatically in Java?

AWTJavaObject Oriented ProgrammingProgrammingSwing

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. We can also do programmatically by using setState(JFrame.ICONIFIED) to minimize a JFrame and setState(JFrame.MAXIMIZED_BOTH) to maximize a JFrame.


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);
      maximizeButton = new JButton("JFrame Maximized");
      add(maximizeButton, BorderLayout.SOUTH);
      setSize(400, 275);
   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();


In the above program, if we click on "JFrame Iconified" button, the frame is minimized and click on "JFrame Maximized" button, the frame is maximized.

Published on 24-Jul-2019 16:07:15