Search code examples
javaswingmenutoolbarlook-and-feel

Top Menu(close, minimize, maximize) Java


How to change Look and Feel for the ToolBar, the top menu (where the buttons to close, minimize, maximize)

Is it possible to like something change? (Add, delete button, assign a background) What is import is required to create it?


Solution

  • you can set the background image of a JButton you could have a look at this: Swing Tutorial: JButton which shows the use of the new JButton(String text,ImageIcon imgIco) to create a JButton with an ImageIcon and String.

    To set the colour of the background and text you could use setBackground(Color c) and setForeground(Color c)

    or

    Alternatively just customize Look and Feel color scheme by setting an appropriate supported Look and Feel and then change the color scheme/size etc of its components thier are hundreds of things you can change for every component see this for them all.

    To customize the Exit, Minimize and Maximize Toolbar buttons this can also be using the Look and Feel ( Custom design for Close/Minimize buttons on JFrame ):

    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();
                }
            });
        }
    }