Dowemo
0 0 0 0


Question:

I want to remove only minimize button from JFrame but want maximize and close button in JFrame title bar.

Here I am talking about removing not disabling.


Best Answer:


I don't think removing the minimize button is a good thing. But may be you can use the setUndecorated() method to remove the title bar and window edges. And you'll have to add your own close and maximize buttons to perfom those action.

Here is an example :

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.JButton;
public class Example {
    public Example() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setUndecorated(true);//<---- this will disable the frame decorations
        JPanel panel = new JPanel();
        panel.add(new JLabel("titlebar"));
        //Add button maximize
        JButton button_max=new JButton("Maximize");
        button_max.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                if(frame.getExtendedState() == JFrame.NORMAL) {
                    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
                } else {
                    frame.setExtendedState(JFrame.NORMAL);
                }
            }
        });
        panel.add(button_max);
        //Add button close
        JButton button_close = new JButton(new AbstractAction("Close") {
            private static final long serialVersionUID = -4901571960357967734L;
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        panel.add(button_close);
        frame.add(panel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    public static void main(String[] args) {
         new Example();
    }
}

Edit :

As @peeskillet states in the comment, even with this method the window still can be resized and draged by the user. The ComponentResizer class allows to do that.

Here is a an SO post which gives a good example to use this class with Jframe.




Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs