10

I am developing a tool for my laptop. I want to disable minimize button in the JFrame. I have already disabled maximize and close button.

Here is the code to disable maximize and close button:

JFrame frame = new JFrame();  
frame.setResizable(false); //Disable the Resize Button  
// Disable the Close button
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 

Please, tell me how to disable minimize button.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Pankaj Kumar Thapa
  • 131
  • 1
  • 2
  • 6

5 Answers5

10

Generally, you can't, what you can do is use a JDialog instead of JFrame

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
10

As @MadProgrammer said (+1 to him), this is definitely not a good idea you'd rather want to

  • use a JDialog and call setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); to make sure it cannot be closed.

  • You could also use a JWindow (+1 to @M. M.) or call setUndecorated(true); on your JFrame instance.

Alternatively you may want to add your own WindowAdapater to make the JFrame un-minimizable etc by overriding windowIconified(..) and calling setState(JFrame.NORMAL); from within the method:

//necessary imports
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class Test {

    /**
     * Default constructor for Test.class
     */
    public Test() {
        initComponents();
    }

    public static void main(String[] args) {

        /**
         * Create GUI and components on Event-Dispatch-Thread
         */
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Test test = new Test();
            }
        });
    }
    private final JFrame frame = new JFrame();

    /**
     * Initialize GUI and components (including ActionListeners etc)
     */
    private void initComponents() {
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        frame.setResizable(false);
        frame.addWindowListener(getWindowAdapter());

        //pack frame (size JFrame to match preferred sizes of added components and set visible
        frame.pack();
        frame.setVisible(true);
    }

    private WindowAdapter getWindowAdapter() {
        return new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent we) {//overrode to show message
                super.windowClosing(we);

                JOptionPane.showMessageDialog(frame, "Cant Exit");
            }

            @Override
            public void windowIconified(WindowEvent we) {
                frame.setState(JFrame.NORMAL);
                JOptionPane.showMessageDialog(frame, "Cant Minimize");
            }
        };
    }
}
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • On Mac windowIconified is working in strangeway. Jframe is getting minimized for a second and then getting back on screen and then showing the messageDialog. – Manish Feb 24 '22 at 14:45
8

If you don't want to allow any user action use JWindow.

Mordechai
  • 15,437
  • 2
  • 41
  • 82
  • See also the [Full-Screen Exclusive Mode API](http://docs.oracle.com/javase/tutorial/extra/fullscreen/index.html). If making an app. that paints every pixel on-screen, it can be very handy. – Andrew Thompson Oct 28 '12 at 08:26
6

You may try to change your JFrame type to UTILITY. Then you will not see both minimize btn and maximize btn in your program.

2

I would recommend you to use jframe.setUndecorated(true) as you are not using any of the window events and do not want the application to be resized. Use the MotionPanel that I've made, if you would like to move the panel.

Sorter
  • 9,704
  • 6
  • 64
  • 74