I'm doing this Graphic Interface with Swing. The problem that i'm having is that i can't take the white borders that are arround the JMenuItems, and paint it all of black. Here is an image:
I would like to paint it like this (I have edited the image with paint :D):
Could someone help me? I'll appreciate any help. Thank you!
I just did this quick test, using
UIManager.put("PopupMenu.border", new LineBorder(Color.RED));
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
UIManager.put("PopupMenu.border", new LineBorder(Color.RED));
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Stuff");
menu.add(new JMenuItem("A"));
menu.add(new JMenuItem("BB"));
menu.add(new JMenuItem("CCC"));
menuBar.add(menu);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.setJMenuBar(menuBar);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}
Now, this may not be desirable, as this will effect ALL popup menus in your program
Updated
I had a look at JMenu
and through it's UI delegates and it appears that the popup menu is created within a private
method of JMenu
called ensurePopupMenuCreated
, which would have been an excellent place to inject your custom code.
The method is actually called in a number of different places, but possibly the getPopupMenu
is the most accessible
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Stuff") {
private Border border = new LineBorder(Color.RED);
@Override
public JPopupMenu getPopupMenu() {
JPopupMenu menu = super.getPopupMenu();
menu.setBorder(border);
return menu;
}
};
JMenuItem mi = new JMenuItem("Help", 'H');
mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, KeyEvent.META_MASK));
menu.add(new JMenuItem("A"));
menu.add(new JMenuItem("BB"));
menu.add(new JMenuItem("CCC"));
menu.add(mi);
menuBar.add(menu);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.setJMenuBar(menuBar);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}