18

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

mKorbel
  • 109,525
  • 20
  • 134
  • 319
silverkid
  • 9,291
  • 22
  • 66
  • 92

4 Answers4

15

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);
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
10

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.

Community
  • 1
  • 1
Daniel
  • 1,239
  • 1
  • 13
  • 24
5
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);
        }
    }
}
stacker
  • 68,052
  • 28
  • 140
  • 210
  • Are there any cons to this? – iKlsR Jul 03 '13 at 01:07
  • 4
    What is your answer? You do not explain what you are doing, and I don't see where you are removing the min/max buttons from a JFrame. While I do now see where you're going with this, it took me a minute or two of wondering how this was an answer at all. – Loduwijk Oct 26 '16 at 20:35
  • Beware, this solution makes it quite a bit harder to focus the window. It's a dialog, which means it won't appear as an open program in Windows. If the user focuses another window, such as a web browser, to re-focus the dialog, the user would have to minimize all other programs. – Tom Burris Jan 06 '17 at 06:14
-1

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);
ridvankucuk
  • 2,407
  • 1
  • 23
  • 41
Eason Xiao
  • 65
  • 2