How do I change JFrame state programmatically?

In the following code snippet you’ll learn how to programmatically change the frame state of a JFrame component in a Swing application. The JFrame states are represented as a bitwise masks. You can minimize, maximize or make the JFrame state to normal using the JFrame.setExtendedState() method.

You can pass the following values as the parameter to the method:

  • Frame.NORMAL
  • Frame.ICONIFIED
  • Frame.MAXIMIZED_HORIZ
  • Frame.MAXIMIZED_VERT
  • Frame.MAXIMIZED_BOTH
package org.kodejava.example.swing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SwingFrameState extends JFrame {
    public SwingFrameState() throws HeadlessException {
        initUI();
    }

    private void initUI() {
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());

        final JButton minimize = new JButton("Minimize");
        final JButton maximize = new JButton("Maximize");
        final JButton normal = new JButton("Normal");

        add(normal);
        add(minimize);
        add(maximize);

        pack();
        setSize(200, 200);

        ActionListener listener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == normal) {
                    setExtendedState(Frame.NORMAL);                    
                } else if (e.getSource() == minimize) {
                    setExtendedState(Frame.ICONIFIED);
                } else if (e.getSource() == maximize) {
                    setExtendedState(Frame.MAXIMIZED_BOTH);
                }
            }
        };

        minimize.addActionListener(listener);
        maximize.addActionListener(listener);
        normal.addActionListener(listener);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new SwingFrameState().setVisible(true);
            }
        });
    }
}

How do I change JFrame image icon?

This example demonstrate how to change a JFrame image icon using the setIconImage() method.

package org.kodejava.example.swing;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;

public class FrameIconExample extends JFrame {
    public static void main(String[] args) {
        FrameIconExample frame = new FrameIconExample();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        // Set the window size and its title
        frame.setSize(new Dimension(300, 200));
        frame.setTitle("Frame Icon Example");

        // Read the image that will be used as the application icon.
        // Using "/" in front of the image file name will locate the 
        // image at the root folder of our application. If you don't 
        // use a "/" then the image file should be on the same folder 
        // with your class file.
        BufferedImage image = null;
        try {
            image = ImageIO.read(
                    frame.getClass().getResource("/icon.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        frame.setIconImage(image);

        // Display the form
        frame.setVisible(true);
    }
}

How do I change the cursor shape?

Using the following example you can change the shape of cursor in your Swing desktop application. Just create a Cursor object and assign it to your JFrame instance. A collections of cursor shapes defined in the java.awt.Cursor class.

package org.kodejava.example.swing;

import javax.swing.*;
import java.awt.*;

public class CursorExample extends JFrame {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                CursorExample form = new CursorExample();
                form.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                form.setSize(250, 250);

                // Here we create a hand shaped cursor!
                Cursor cursor = new Cursor(Cursor.HAND_CURSOR);
                form.setCursor(cursor);

                form.pack();
                form.setVisible(true);
            }
        });
    }
}

How do I make a centered JFrame?

If you have a JFrame and you want to center the position in the screen you can use the following formula. Let’s say you have a class called CenteredJFrame.

package org.kodejava.example.swing;

import javax.swing.*;
import java.awt.*;

public class CenteredJFrame extends JFrame {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                // Get the size of our screen
                Dimension screenSize =
                        Toolkit.getDefaultToolkit().getScreenSize();

                CenteredJFrame frame = new CenteredJFrame();
                frame.setTitle("Centered JFrame");
                frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

                frame.setSize(250, 250);

                // Calculates the position where the CenteredJFrame
                // should be paced on the screen.
                int x = (screenSize.width - frame.getWidth()) / 2;
                int y = (screenSize.height - frame.getHeight()) / 2;
                frame.setLocation(x, y);

                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}