I am developing a Java Swing application on macOS using JMenuBar and JMenu. I am trying to change the text color of a menu item to red using the setForeground(Color.RED) method, but it is not working. The menu text always appears in the default color, even after applying the changes.
I suspect this might be related to macOS's native Look and Feel integration, which overrides some customizations. Here is the code I am using:
package university.management.system;
import java.awt.*;
import javax.swing.*;
public class Project extends JFrame {
Project() {
setSize(1440, 900);
ImageIcon i1 = new ImageIcon(ClassLoader.getSystemResource("icons/third.jpg"));
Image i2 = i1.getImage().getScaledInstance(2250, 1500, Image.SCALE_DEFAULT);
ImageIcon i3 = new ImageIcon(i2);
JLabel image = new JLabel(i3);
add(image);
JMenuBar mb = new JMenuBar();
JMenu newInfo = new JMenu("New Information");
newInfo.setForeground(Color.RED); // Trying to set text color to red
mb.add(newInfo);
setJMenuBar(mb);
setVisible(true);
}
public static void main(String[] args) {
// Disable macOS native menu bar integration
System.setProperty("apple.laf.useScreenMenuBar", "false");
new Project();
}
}
Used setForeground(Color.RED) on the JMenu object.
Disabled macOS native menu bar integration with System.setProperty("apple.laf.useScreenMenuBar", "false");
Experimented with UIManager properties:
UIManager.put("Menu.foreground", Color.RED);
UIManager.put("Menu.selectionBackground", Color.LIGHT_GRAY);
Tried using a custom Look and Feel (CrossPlatformLookAndFeel
).
Despite these attempts, the menu text color remains unchanged on macOS. The same code works as expected on Windows.
Why does the setForeground method not work on macOS? How can I enforce custom menu text colors in macOS Swing applications? Is there a workaround or alternative approach to achieve this?
You are setting setForeground color on menu instead of menubar, if you set setForeground color on menubar then it will work check below
package university.management.system;
import java.awt.*;
import javax.swing.*;
public class Project extends JFrame {
Project() {
setSize(1440, 900);
ImageIcon i1 = new ImageIcon(ClassLoader.getSystemResource("icons/third.jpg"));
Image i2 = i1.getImage().getScaledInstance(2250, 1500, Image.SCALE_DEFAULT);
ImageIcon i3 = new ImageIcon(i2);
JLabel image = new JLabel(i3);
add(image);
JMenuBar mb = new JMenuBar();
JMenu newInfo = new JMenu("New Information");
//newInfo.setForeground(Color.RED); // Trying to set text color to red
mb.add(newInfo);
mb.setForeground(Color.RED);
setJMenuBar(mb);
setVisible(true);
}
public static void main(String[] args) {
// Disable macOS native menu bar integration
System.setProperty("apple.laf.useScreenMenuBar", "false");
new Project();
}
}