Swing/MenuItemAcceleratorFont のバックアップ(No.3)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- Swing/MenuItemAcceleratorFont へ行く。
- 1 (2023-11-13 (月) 06:20:17)
- 2 (2023-11-13 (月) 11:52:47)
- 3 (2023-11-17 (金) 23:06:27)
- 4 (2023-11-18 (土) 20:09:35)
- 5 (2023-11-24 (金) 16:00:39)
- 6 (2025-01-03 (金) 08:57:02)
- 7 (2025-01-03 (金) 09:01:23)
- 8 (2025-01-03 (金) 09:02:38)
- 9 (2025-01-03 (金) 09:03:21)
- 10 (2025-01-03 (金) 09:04:02)
- 11 (2025-06-19 (木) 12:41:37)
- 12 (2025-06-19 (木) 12:43:47)
- 13 (2025-08-17 (日) 11:50:23)
- category: swing folder: MenuItemAcceleratorFont title: JMenuItemのAccelerator表示を変更する tags: [JMenuItem, UIManager, Font, JCheckBoxMenuItem, JRadioButtonMenuItem] author: aterai pubdate: 2023-11-13T06:14:06+09:00 description: JMenuItemに設定されたAcceleratorの文字サイズや色を変更します。 image: https://drive.google.com/uc?id=1yw38bWwIYd05dmP11IRoVcc-YF740hoh
概要
JMenuItemに設定されたAcceleratorの文字サイズや色を変更します。
Screenshot

Advertisement
サンプルコード
Color color1;
Color color2;
Font font;
List<String> list = Arrays.asList(
"MenuItem", "CheckBoxMenuItem", "RadioButtonMenuItem");
for (String prefix : list) {
String key1 = prefix + ".acceleratorForeground";
String key2 = prefix + ".acceleratorSelectionForeground";
String key3 = prefix + ".acceleratorFont";
if (selected) {
color1 = AFC;
color2 = Color.WHITE;
font = UIManager.getFont(key3).deriveFont(10f);
} else {
UIDefaults def = UIManager.getLookAndFeelDefaults();
color1 = def.getColor(key1);
color2 = def.getColor(key2);
font = def.getFont(key3);
}
UIManager.put(key1, color1);
UIManager.put(key2, color2);
UIManager.put(key3, font);
}
SwingUtilities.updateComponentTreeUI(popup);
View in GitHub: Java, Kotlin解説
UIManager.put("MenuItem.acceleratorForeground", color)JMenuItem#setAccelerator(KeyStroke)で設定したacceleratorの文字色を変更MetalLookAndFeel,WindowsLookAndFeelなどで有効、NimbusLookAndFeelなどでは無効JCheckBoxMenuItem,JRadioButtonMenuItemには影響しないため、別途CheckBoxMenuItem.acceleratorForegroundなどで設定する必要があるBasicLookAndFeelでMenu.acceleratorForeground,Menu.acceleratorSelectionForeground,Menu.acceleratorSelectionFontが設定されているが、JMenuにacceleratorを設定しても実行時にjava.lang.Error: setAccelerator() is not defined for JMenu. Use setMnemonic() instead.とエラーになるので無意味
UIManager.put("MenuItem.acceleratorSelectionForeground", color)acceleratorの選択文字色を変更
UIManager.put("MenuItem.acceleratorSelectionFont", font)acceleratorのFontを変更- このサンプルでは
FontサイズをFont#deriveFont(10f)で縮小している