37

I have a JFrame and want to remove the maximize button from that.

I wrote the code below, but it removed maximize, minimize, and close from my JFrame.

JFrame frame = new JFrame();
frame.add(kart);
frame.setUndecorated(true);
frame.setVisible(true);
frame.setSize(400, 400);

I want to only remove the maximize button from the JFrame.

jzd
  • 23,473
  • 9
  • 54
  • 76
Mahdi_Nine
  • 14,205
  • 26
  • 82
  • 117
  • 1
    this *might* help: http://geekycoder.wordpress.com/2009/07/17/java-tips-disabling-the-maximize-button-of-jframe/ – MByD Apr 11 '11 at 18:05

9 Answers9

74

Make it not resizable:

frame.setResizable(false);

You will still have the minimize and close buttons.

sjr
  • 9,769
  • 1
  • 25
  • 36
8

You can't remove the button from a JFrame. Use a JDialog instead. It doesn't have a maximize button.

jzd
  • 23,473
  • 9
  • 54
  • 76
3

In JFrame properties -> maximumSize = minimumSize. And resizable = false. Done! The button is disabled.

Matthieu
  • 2,736
  • 4
  • 57
  • 87
  • This is not a solution for special cases where one wants to be still able resizing the jFrame in one direction only, like let's say vertically (that is changing jFrame height - my case)., cos this way it would be disabled completely as whole. – fafa Jun 14 '23 at 10:57
2
import java.awt.event.WindowAdapter; 
import java.awt.event.WindowEvent;    
import javax.swing.JDialog; import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test extends JDialog {
    public Test(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 {
            Test myFrame = new Test(new JFrame(), "Removing maximize button");
            JPanel panel = new JPanel();
            panel.setSize(100, 100);
            myFrame.add(panel);
            myFrame.setSize(100, 100);
            myFrame.setVisible(true);
        } catch (IllegalArgumentException e) {
            System.exit(0);
        }
    } }
Bartzilla
  • 2,768
  • 4
  • 28
  • 37
  • Can you explain a bit more on what it does? – Matthieu Aug 17 '17 at 10:22
  • 2
    You used a JDialog instead of a JFrame. JDialog and JFrame have different behaviour, so just using a JDialog is not an universally applicable solution for the question. – leftbit May 04 '20 at 10:55
1
/**
 * Removes the buttons from the JDialog title frame. This is a work around
 * to removing the close button
 * 
 * This is confirmed to work with the Metal L&F
 */
public void removeAllTitleFrameButtons() {

    /* Get the components of the dialog */
    Component[] comps = this.getRootPane().getComponents();
    
    /*
     * Go through the components and find the title 
     * pane and remove the buttons.  
     */
    outerloop:
    for(Component comp : comps) {
        if(comp.getClass().getName().indexOf("JLayeredPane") >0) {
            for(Component jcomp : ((JLayeredPane)comp).getComponents()) {
                if(jcomp.getClass().getName().indexOf("Title") > 0) {
                    
                    /* Get the XXXXTitlePane Components */
                    Component[] titlePaneComps = ((JComponent)jcomp).getComponents();
                    
                    for(Component tpComp : titlePaneComps) {
                        if(tpComp instanceof JButton) {
                            ((JButton)tpComp).setVisible(false);                        
                        }
                    }
                    /* No need to continue processing */
                    break outerloop;
                }
            }
        }
    }
}
fafa
  • 77
  • 8
Gino
  • 11
  • 1
  • --- just remove the "break" statement to remove and update the "if" if necessary. I use this to remove the close button. – Gino Apr 13 '12 at 12:56
  • 1
    possible only if the frame is decorated by the LAF – kleopatra Oct 26 '12 at 10:27
  • @kleopatra Exactly: this does not work without the LAF as there is no component which class name would contain word "Title" (at least in that specific loop) in case "this" points to main JFrame. – fafa Jun 14 '23 at 11:15
1

Go to JFrame's property and set resizeable unchecked.

unbuntry
  • 11
  • 2
0

change type property to utility. It will only show the close button.

  • What exactly do you mean by this? Just stating something without any example provided is useless for many... – fafa Jun 14 '23 at 10:54
-1

If you are using Netbean then just unselect the resizable option in properties. It will only disable Minimize/Maximize Button.

-1

There is described how to implement a "JFrame" without maximize and minimize buttons. You need just "incapsulate" a JFrame in JDialog :

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

public class RemoveMaxAndMinButton extends JDialog{
  public RemoveMaxAndMinButton(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{
      RemoveMaxAndMinButton frame = new RemoveMaxAndMinButton(new JFrame(),
            "Remove the Minimize and Maximize button from the Title Bar");
      JPanel panel = new JPanel();
      panel.setSize(200,200);
      JLabel lbl = new JLabel("RoseIndia.Net");
      panel.add(lbl);
      frame.add(panel);
      frame.setSize(400, 400);
      frame.setVisible(true);
    }
    catch(IllegalArgumentException e){
      System.exit(0);
    }
  } 

}

StKiller
  • 7,631
  • 10
  • 43
  • 56
  • 2
    This is not a `JFrame` without minimize/maximize buttons. It’s just a regular `JDialog`. – Martin Sep 15 '16 at 14:55