3

I would like to apply my own close and minimize buttons. Is there any way to change the JFrame design?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
BDeveloper
  • 1,175
  • 6
  • 24
  • 44

4 Answers4

6

The trick lies in the PLAF and setDefaultLookAndFeelDecorated(true) (Specifying Window Decorations).

E.G.

Metal Windows

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

public class FrameCloseButtonsByLookAndFeel {

    FrameCloseButtonsByLookAndFeel() {
        String[] names = {
                UIManager.getSystemLookAndFeelClassName(), 
                UIManager.getCrossPlatformLookAndFeelClassName()
        };
        for (String name : names) {
            try {
                UIManager.setLookAndFeel(name);
            } catch (Exception e) {
                e.printStackTrace();
            }
            // very important to get the window decorations.
            JFrame.setDefaultLookAndFeelDecorated(true);
            JFrame f = new JFrame(UIManager.getLookAndFeel().getName());
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

            JPanel gui = new JPanel(new BorderLayout());
            f.setContentPane(gui);

            JTree tree = new JTree();
            tree.setVisibleRowCount(4);
            gui.add(tree, BorderLayout.LINE_START);

            gui.add(new JScrollPane(new JTextArea(3,15)));

            JToolBar toolbar = new JToolBar();
            gui.add(toolbar, BorderLayout.PAGE_START);
            for (int ii=1; ii<5; ii++) {
                toolbar.add(new JButton("Button " + ii));
                if (ii%2==0) {
                    toolbar.addSeparator();
                }
            }

            f.pack();

            f.setLocationByPlatform(true);
            f.setVisible(true);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new FrameCloseButtonsByLookAndFeel();
            }
        });
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
3

think you are after a JWindow

http://docs.oracle.com/javase/7/docs/api/javax/swing/JWindow.html

You can then create your own buttons which actions can minimize/close your window

pengibot
  • 1,492
  • 3
  • 15
  • 35
  • that means all custom GUI are JWindows – BDeveloper Jun 06 '12 at 11:45
  • no, custom GUI can be anything you want, you override them and implement them however you want and apply your own look and feel for how you want it to look – pengibot Jun 06 '12 at 11:48
  • if you want to use a JFrame you can still and use JFrame.setDefaultLookAndFeelDecorated(true); recursively to get rid of close/minimize buttons etc. though would be a little more work – pengibot Jun 06 '12 at 11:49
  • You can also recycle the corresponding internal frame icons, as shown [here](http://stackoverflow.com/a/10360374/230513). – trashgod Jun 06 '12 at 16:04
2

The only thing I'm aware that can be done is to add a WindowListener to the JFrame and handle closing events in that listener. You can make virtually anything, like displaying dialogs or even cancelling the closing of the JFrame.

See this tutorial for more details about how to write such listeners.

As for minimizing: as far as I know, there is no way to control or modify such behaviour, it's completely controlled by the operating system.

The only way to change the aspect of the minimize/close/maximize buttons is to use a custom LookAndFeel and setting JFrame.setDefaultLookAndFeelDecorated (true);.

Radu Murzea
  • 10,724
  • 10
  • 47
  • 69
  • i don't wanna change the events handlers.. i want to change it's image, background, the main frame layout of the application?? – BDeveloper Jun 06 '12 at 11:42
0
  1. Set jframe undecorated.
  2. Place a jlabel for each button.
  3. Put own icon for each Btn.
  4. Put mouseListeners for each jlabel and specify code eg, System.exit(0);/set ICONIFIED option
SilverlightFox
  • 32,436
  • 11
  • 76
  • 145
  • this has nothing to do with LaF's.It's a customized way!For more info go to the Vertex Digital Arts Channel on YouTube – IsmailVawda Dec 29 '13 at 12:51