Changing a JMenuBar border
Asked Answered
C

2

7

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]

Coss answered 28/8, 2012 at 16:30 Comment(0)
O
11

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

Oman answered 28/8, 2012 at 17:40 Comment(0)
T
2

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.

Tugboat answered 28/8, 2012 at 16:55 Comment(1)
I have already checked the java doc conscientiously, but thanks for the advice ;) I think I'm gonna remove the border of every component of Swing until this border disapear.Coss

© 2022 - 2024 — McMap. All rights reserved.