Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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]

like image 931
user1534422 Avatar asked Aug 28 '12 16:08

user1534422


2 Answers

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

like image 93
halex Avatar answered Nov 04 '22 00:11

halex


I would start by looking at the javax.swing.border.Border class. Every Swing class which extends javax.swing.JComponent has a setBorder() method.

I strongly suggest that you familiarize yourself with the Java API documentation. These are an invaluable tool when you are programming in Java.

like image 26
Code-Apprentice Avatar answered Nov 03 '22 23:11

Code-Apprentice