• category: swing folder: FileViewFullRowSelection title: JFileChooserのDetails Viewで行全体を選択可能にする tags: [JFileChooser, JTable, UIManager, LookAndFeel] author: aterai pubdate: 2020-05-04T17:55:19+09:00 description: JFileChooserの詳細表示を行うJTableで行全体の選択状態表示とマウスによる行選択を可能に変更します。 image: https://drive.google.com/uc?id=1Kmm_cFFBn4Ox_HtiEZwK3-cmZbWG7BkD

概要

JFileChooserの詳細表示を行うJTableで行全体の選択状態表示とマウスによる行選択を可能に変更します。

サンプルコード

String key = "FileView.fullRowSelection";
System.out.println(UIManager.getLookAndFeelDefaults().getBoolean(key));
JCheckBox check = new JCheckBox(key) {
  @Override public void updateUI() {
    super.updateUI();
    setSelected(UIManager.getLookAndFeelDefaults().getBoolean(key));
  }
};
JButton button = new JButton("open");
button.addActionListener(e -> {
  Boolean flg = check.isSelected();
  UIManager.put("FileView.fullRowSelection", flg);
  JFileChooser chooser = new JFileChooser();
  // https://ateraimemo.com/Swing/DetailsViewFileChooser.html
  Optional.ofNullable(chooser.getActionMap().get("viewTypeDetails"))
      .ifPresent(a -> a.actionPerformed(new ActionEvent(
          e.getSource(), e.getID(), "viewTypeDetails")));

  // https://ateraimemo.com/Swing/GetComponentsRecursively.html
  stream(chooser)
      .filter(JTable.class::isInstance).map(JTable.class::cast)
      .findFirst()
      .ifPresent(t -> t.putClientProperty("Table.isFileList", !flg));
  int retValue = chooser.showOpenDialog(getRootPane());
  if (retValue == JFileChooser.APPROVE_OPTION) {
    log.setText(chooser.getSelectedFile().getAbsolutePath());
  }
});
View in GitHub: Java, Kotlin

解説

  • FileView.fullRowSelection: false
    • WindowsLookAndFeelのデフォルト
    • 詳細表示(Details View)用のJTableでファイル名を表示する0列目のセルの文字列部分のみマウスクリックに反応して選択可能で選択状態表示もファイル名のみ
  • FileView.fullRowSelection: true
    • NimbusLookAndFeelのデフォルト
    • 詳細表示(Details View)用のJTableで行全体に選択状態が表示される
    • デフォルトではFileView.fullRowSelectionの設定にかかわらずJTable#putClientProperty("Table.isFileList", Boolean.TRUE)が設定されているため、ファイル名を表示する0列目のセルの文字列部分のみマウスクリックに反応する
    • 上記のサンプルではFileView.fullRowSelectiontrueの場合はJTable#putClientProperty("Table.isFileList", Boolean.FALSE)を設定して行全体でマウスクリックによる選択を可能に変更している

参考リンク

コメント