Reputation: 323
I am trying to set JMenu foreground color in my project. I use UIManager.put("text", Color.RED) it work for all text but in JMenu text color does not change.
I want to set JMenu.setForeground("Color.RED") to work but UIManager.put("Menu.foreground", Color.RED) does not fill color. So please help me for this below code.
import java.awt.Color;
import javax.swing.UIManager;
public class Frame extends javax.swing.JFrame {
public Frame() {
setExtendedState(Frame.MAXIMIZED_BOTH);
setTitle("MENU COLOR");
initComponents();
}
private void initComponents() {
menu_bar = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenu1.setText("File");
jMenu1.setFont(new java.awt.Font("URW Bookman L", 0, 18));
jMenu1.setPreferredSize(new java.awt.Dimension(45, 25));
jMenu1.setForeground(Color.RED);
menu_bar.add(jMenu1);
setJMenuBar(menu_bar);
pack();
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager
.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
UIManager.put("Menu.foreground", Color.RED);
break;
}
}
} catch (Exception ex) {
ex.printStackStrace();
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Frame().setVisible(true);
}
});
}
private javax.swing.JMenu jMenu1;
private javax.swing.JMenuBar menu_bar;
}
Thank You For Reading
Upvotes: 2
Views: 2686
Reputation: 11327
You can replace a separate component UI.
Example:
public class MyMenuUI extends SynthMenuUI {
public static ComponentUI createUI(JComponent aComponent) {
return new MyMenuUI();
}
@Override
public void installUI(JComponent c) {
super.installUI(c);
c.setForeground(UIManager.getColor("Menu.foreground"));
}
}
After initialization of L&F you must simply put your UI into L&F
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager
.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
UIManager.put("Menu.foreground", Color.RED);
UIManager.put("MenuUI", MyMenuUI.class.getName());
break;
}
}
} catch (Exception ex) {
ex.printStackStrace();
}
Disadvantage: you must do it for each supported L&F
Upvotes: 3