Disable JFrame minimize button
Asked Answered
D

5

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.

Disown answered 28/10, 2012 at 7:45 Comment(4)
"Please, tell me how to disable minimize button." Please, don't do that.Turkey
What is the actual goal of the GUI here? Is it a kiosk?Turkey
display the battery charge info to the user in laptop.Disown
Thanks for explaining. How are you finding that value in Java?Turkey
A
10

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

Accompaniment answered 28/10, 2012 at 7:47 Comment(3)
I need a maximize button, modality, but no enabled minimize button (that is, I am asked to implement that functionality). I learned how to add modality to a JFrame, but I'm still not sure how I can disable the frame's window control button as it's managed nativelyEvaporation
@Evaporation There isn't any way to do it from within SwingAccompaniment
Ok, can I do it natively? The BA wants itEvaporation
P
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");
            }
        };
    }
}
Pilferage answered 28/10, 2012 at 8:59 Comment(2)
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.Expiratory
@Expiratory same thing on WIndowsEvaporation
A
8

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

Acidophil answered 28/10, 2012 at 7:49 Comment(1)
See also the Full-Screen Exclusive Mode API. If making an app. that paints every pixel on-screen, it can be very handy.Turkey
T
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.

Tolliver answered 13/12, 2015 at 10:19 Comment(0)
T
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.

Torero answered 27/11, 2012 at 14:40 Comment(2)
People can still minimize the JFrame by clicking on the icon in the windows task bar...Journalist
In that case you minimize to system trayTorero

© 2022 - 2024 — McMap. All rights reserved.