Swing/MenuItemTextAlignment のバックアップ差分(No.3)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/MenuItemTextAlignment へ行く。
- 1 (2016-07-18 (月) 01:56:46)
- 2 (2016-12-16 (金) 20:00:58)
- 3 (2017-04-07 (金) 13:51:51)
- 4 (2017-08-01 (火) 21:55:51)
- 5 (2017-08-15 (火) 14:19:35)
- 6 (2018-08-17 (金) 13:32:45)
- 7 (2019-02-25 (月) 17:43:48)
- 8 (2020-12-10 (木) 12:11:31)
- 9 (2021-05-12 (水) 07:29:38)
- 10 (2022-08-20 (土) 22:15:25)
- 11 (2023-10-05 (木) 11:59:50)
- 12 (2023-10-16 (月) 12:37:08)
- 追加された行はこの色です。
- 削除された行はこの色です。
---
category: swing
folder: MenuItemTextAlignment
title: JMenuに追加したJMenuItemなどのテキスト位置を揃える
tags: [JMenuBar, JMenu, JMenuItem, LookAndFeel]
author: aterai
pubdate: 2016-07-18T01:53:49+09:00
description: JMenuに追加したJMenuItemやJLabelなどのコンポーネントのテキスト位置を揃えて表示します。
image: https://lh3.googleusercontent.com/-S49YgtIvzc8/V4upKl0JAFI/AAAAAAAAOd4/9vWtFW4DvY4xR0bL0sM9iGRBvHz_u7AcQCCo/s800/MenuItemTextAlignment.png
---
* 概要 [#summary]
`JMenu`に追加した`JMenuItem`や`JLabel`などのコンポーネントのテキスト位置を揃えて表示します。
#download(https://lh3.googleusercontent.com/-S49YgtIvzc8/V4upKl0JAFI/AAAAAAAAOd4/9vWtFW4DvY4xR0bL0sM9iGRBvHz_u7AcQCCo/s800/MenuItemTextAlignment.png)
* サンプルコード [#sourcecode]
#code(link){{
JMenuItem item3 = new JMenuItem("\u200B"); //, HSTRUT);
//item3.setLayout(new BorderLayout());
//item3.setBorder(BorderFactory.createEmptyBorder()); //NimbusLookAndFeel
item3.setEnabled(false);
//item3.setDisabledIcon(HSTRUT);
item3.add(new JMenuItem("JMenuItem(disabled) with JMenuItem", HSTRUT) {
@Override public boolean contains(int x, int y) {
return false; //disable mouse events
}
});
}}
* 解説 [#explanation]
`JMenu`や`JPopupuMenu`にクリック不可の項目として`JMenuItem`の代わりに`JLabel`を追加すると、`WindowsLookAndFeel`を使用している場合や他の`JMenuItem`にアイコンが設定されている場合に、テキストの開始位置が揃わないので、これを回避するために以下の方法をテストしています。
- `JMenuItem.setEnabled(false);`
-- `JMenuItem.setEnabled(false);`と`UIManager.put("MenuItem.disabledForeground", Color.BLACK);`を使用
-- `MenuItem.disabledForeground`が使用されるかどうかは`LookAndFeel`に依存
- `JLabel + EmptyBorder`
-- 余白を設定した`JLabel`を使用
-- 余白の幅は`LookAndFeel`に依存(`LookAndFeel`依存の幅を取得する方法がない?)
- `JPanel with JMenuItem`
-- `MenuElement`ではない透明な`JPanel`に、`JComponent#contains()`メソッドをオーバーライドしてマウスクリックを無効にした`JMenuItem`を追加
- `JMenuItem(disabled) with JMenuItem`
-- `JMenuItem.setEnabled(false);`とした空の`JMenuItem`に、`JComponent#contains()`メソッドをオーバーライドしてマウスクリックを無効にした`JMenuItem`を追加
メモ:
- `JMenu`にアイコン用の余白がない`MetalLookAndFeel`などにアイコンが設定された`JMenuItem`が存在する場合、幅のみのアイコンを設定する必要がある
- `MetalLookAndFeel`の場合、文字列もアイコンも存在しない`JMenuItem`は他の`JMenuItem`と高さが異なる(幅ゼロ空白文字`\u200B`で回避)
- `NimbusLookAndFeel`の`JPanel`は、デフォルトでは背景が不透明
* 参考リンク [#reference]
- [http://stackoverflow.com/questions/38360595/jlabel-with-icon-in-jpopupmenu-doesnt-follow-other-jmenuitem-alignment java - JLabel with icon in JPopupMenu doesn't follow other JMenuItem alignment - Stack Overflow]
- [https://bugs.openjdk.java.net/browse/JDK-8171223 JDK-8171223 Double icons with JMenuItem setHorizontalTextPosition on Win 10 - Java Bug System]
* コメント [#comment]
#comment
#comment