Swing/FileChooserFileAndFilterAlignment のバックアップ差分(No.2)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/FileChooserFileAndFilterAlignment へ行く。
- 1 (2017-08-28 (月) 15:53:09)
- 2 (2018-02-15 (木) 14:23:42)
- 3 (2018-09-12 (水) 19:56:46)
- 4 (2020-09-16 (水) 23:41:30)
- 5 (2022-04-22 (金) 06:29:51)
- 追加された行はこの色です。
- 削除された行はこの色です。
--- category: swing folder: FileChooserFileAndFilterAlignment title: JFileChooserのファイル名とフィルタのラベルを右揃えに変更する tags: [JFileChooser, JLabel, BoxLayout] author: aterai pubdate: 2017-08-28T15:46:15+09:00 description: JFileChooserの下部に表示されるファイル名とフィルタのラベルを左揃えから右揃えに変更します。 image: https://drive.google.com/uc?export=view&id= --- * 概要 [#summary] `JFileChooser`の下部に表示されるファイル名とフィルタのラベルを左揃えから右揃えに変更します。 #download(https://drive.google.com/uc?export=view&id=) #download(https://drive.google.com/uc?export=view&id=1U4T22tNO7N5NZJUDujh3ZUh68fe-xyvbPw) * サンプルコード [#sourcecode] #code(link){{ class RightAlignmentMetalFileChooserUI extends MetalFileChooserUI { protected RightAlignmentMetalFileChooserUI(JFileChooser fc) { super(fc); } @Override public void installComponents(JFileChooser fc) { super.installComponents(fc); SwingUtils.stream(getBottomPanel()) .filter(JLabel.class::isInstance) .map(JLabel.class::cast) .forEach(l -> { l.setHorizontalAlignment(SwingConstants.RIGHT); l.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5)); }); } } class RightAlignmentWindowsFileChooserUI extends WindowsFileChooserUI { protected RightAlignmentWindowsFileChooserUI(JFileChooser fc) { super(fc); } @Override public void installComponents(JFileChooser fc) { super.installComponents(fc); SwingUtils.stream(getBottomPanel()) .filter(JLabel.class::isInstance) .map(JLabel.class::cast) .forEach(l -> l.setAlignmentX(1f)); } } }} * 解説 [#explanation] - `MetalLookAndFeel` -- `MetalFileChooserUI#getBottomPanel()`で取得できる`JPanel`のレイアウトは`BoxLayout.Y_AXIS`で、以下の`3`つの`JPanel`を縦配置 --- ファイル名ラベルとファイル名入力欄を配置した`fileNamePanel`(`BoxLayout.LINE_AXIS`) --- ファイルフィルタラベルとファイルフィルタコンボボックスを配置した`filesOfTypePanel`(`BoxLayout.LINE_AXIS`) --- `approveButton`などを配置した`ButtonPanel`(`FlowLayout`風の独自`ButtonAreaLayout`) -- 各ラベルの推奨サイズを文字列の長い方に合わせることで、別パネルに分かれていても位置が揃うように設定されているため、`JLabel#setHorizontalAlignment(SwingConstants.RIGHT)`で右揃えに変更可能 - `WindowsLookAndFeel` -- `WindowsFileChooserUI#getBottomPanel()`で取得できる`JPanel`のレイアウトは`BoxLayout.LINE_AXIS`で、以下の`3`つの`JPanel`を横配置 --- ファイル名ラベルとファイル名入力欄を配置した`fileNamePanel`(`BoxLayout.Y_AXIS`) --- ファイルフィルタラベルとファイルフィルタコンボボックスを配置した`filesOfTypePanel`(`BoxLayout.Y_AXIS`) --- `approveButton`などを配置した`ButtonPanel`(`BoxLayout.Y_AXIS`) -- ラベルは`BoxLayout.Y_AXIS`の`JPanel`にまとめられているので、`JComponent.html#setAlignmentX(1f)`で右揃えに変更可能 -- 各ラベルは`BoxLayout.Y_AXIS`の`JPanel`にまとめられているので、`JComponent.html#setAlignmentX(1f)`で右揃えに変更可能 * 参考リンク [#reference] - [[MetalLookAndFeelでJFileChooserの下部にコンポーネントを追加する>Swing/FileChooserBottomAccessory]] - [[Containerの子Componentを再帰的にすべて取得する>Swing/GetComponentsRecursively]] * コメント [#comment] #comment #comment