Question

I need to remove the maximize and minimize buttons from a JFrame. Please suggest how to do this.

Was it helpful?

Solution

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Dlg extends JDialog {
    public Dlg(JFrame frame, String str) {
        super(frame, str);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
                System.exit(0);
            }
        });
    }

    public static void main(String[] args) {
        try {
            Dlg frame = new Dlg(new JFrame(), "No min max buttons");
            JPanel panel = new JPanel();
            panel.setSize(200, 200);
            JLabel lbl = new JLabel("blah blah");
            panel.add(lbl);
            frame.add(panel);
            frame.setSize(400, 400);
            frame.setVisible(true);
        } catch (IllegalArgumentException e) {
            System.exit(0);
        }
    }
}

OTHER TIPS

Here's a related example using setUndecorated() to disable the frame decorations.

alt text

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class FrameTest implements Runnable {

    public static void main(String[] args) {
        EventQueue.invokeLater(new FrameTest());
    }

    @Override
    public void run() {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setUndecorated(true);
        JPanel panel = new JPanel();
        panel.add(new JLabel("Stackoverflow!"));
        panel.add(new JButton(new AbstractAction("Close") {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        }));
        f.add(panel);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}

Note: I initially edited stacker's answer, but it was suggested that I create a new answer instead.

There are a few ways to customize the window controls available to your users.

Currently the only way to remove the maximize and minimize buttons, while keeping the title bar and close button, is to use a JDialog instead of a JFrame:

import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class DialogDemo {

    public static void main(String[] args) {
        JDialog dialog = new JDialog(new JFrame(), "No min max buttons");
        // necessary as setDefaultCloseOperation(EXIT_ON_CLOSE) is 
        // not available for JDialogs.
        dialog.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
                System.exit(0);
            }
        });

        JLabel label = new JLabel("blah blah");
        JPanel panel = new JPanel();
        panel.setPreferredSize(new Dimension(400, 400));
        panel.add(label);

        dialog.add(panel);
        dialog.pack();
        dialog.setVisible(true);
    }
}

The dialog solution makes it impossible for users to minimize and maximise the window, including through the use of shortcuts, however it does not remove the ability to resize the window.

Using setResizable(false) will remove the maximize button only, at the cost of not being able to resize the window.

Lastly, as mentioned by trashgod, the setUndecorated(true) method will disable the frame decorations, removing the title bar and window edges. This makes it harder for users to drag, resize, and close the window, although not impossible, as these actions can still be performed using shortcut keys.

You can try this:

JFrame loadingDialog = new JFrame();

JLabel label = new JLabel("blah blah");
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(400, 400));
panel.add(label);

loadingDialog.add(panel);
loadingDialog.setUndecorated(true);

loadingDialog.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
                    loadingDialog.pack();

loadingDialog.setVisible(true);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top