Search code examples
javaswingborderjmenubar

Changing a JMenuBar border


I'm trying to change the border of a JMenuBar with an other border. Like that (for example) :

From Image 1 http://img11.hostingpics.net/pics/696780Sanstitre.png To Image 2 http://img11.hostingpics.net/pics/900299Sanstitre2.png                                                                                                                        

But I can't find a way to do that. I can change anything I want but that.

[edit] I have already tried :

UIManager.put("Menu.border", BorderFactory.createLineBorder(Color.black, 1));
UIManager.put("MenuBar.border", BorderFactory.createLineBorder(Color.black, 1));
UIManager.put("MenuItem.border", BorderFactory.createLineBorder(Color.black, 1));

and it's doesn't worked :( ...

[/edit]


Solution

  • Finally I found eaxctly what you are looking for :) The right property for the UIManager is PopupMenu.border. To change the border of the whole popup menu to a thickness of 4 pixel and a red color (just a funny example) you need the following line:

    UIManager.put("PopupMenu.border", BorderFactory.createLineBorder(Color.red, 4));
    

    Here is a small example:

    import java.awt.Color;
    import javax.swing.*;
    import javax.swing.border.*;
    
    public class CustomPopupMenuBorder
    {
        public static void main(String[] args)
        {
            UIManager.put("PopupMenu.border", BorderFactory.createLineBorder(Color.black, 1));      
            JDialog myJDialog = new JDialog();
            myJDialog.setSize(450,300);
            JMenuBar bar = new JMenuBar();
            JMenu menu = new JMenu("It's a me");
            JMenuItem item = new JMenuItem("JMenuItem 1");
            JMenuItem item2 = new JMenuItem("JMenuItem 2");
            menu.add(item);
            JSeparator sep = new JSeparator();    
            menu.add(sep);
            menu.add(item2);
            bar.add(menu);
            myJDialog.setJMenuBar(bar);
            myJDialog.setVisible(true);
        }
    }
    

    The best help I had on my journey to get this is the java application UIManager Defaults