• 追加された行はこの色です。
  • 削除された行はこの色です。
---
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=)

* サンプルコード [#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)`で右揃えに変更可能

* 参考リンク [#reference]
- [[MetalLookAndFeelでJFileChooserの下部にコンポーネントを追加する>Swing/FileChooserBottomAccessory]]
- [[Containerの子Componentを再帰的にすべて取得する>Swing/GetComponentsRecursively]]

* コメント [#comment]
#comment
#comment