admin管理员组文章数量:1122832
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?
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?
Share Improve this question asked Nov 22, 2024 at 10:39 SrihimamshuSrihimamshu 32 bronze badges1 Answer
Reset to default 0You 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();
}
}
本文标签: javaUnable to Change JMenu Text Color on macOS in a Swing ApplicationStack Overflow
版权声明:本文标题:java - Unable to Change JMenu Text Color on macOS in a Swing Application - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736304458a1932239.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论