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]
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With