• 追加された行はこの色です。
  • 削除された行はこの色です。
---
category: swing
folder: GetComponentsRecursively
title: Containerの子Componentを再帰的にすべて取得する
tags: [Container, Component, JFileChooser, JTable]
author: aterai
pubdate: 2017-02-06T14:11:50+09:00
description: Containerの子Componentを再帰的にすべて取得するメソッドを作成し、JFileChooserに配置されたJTableを取得します。
image: https://drive.google.com/uc?export=view&id=1NedWhPhVuMDTwrHRaFdW-YXZjdH-019yuw
---
* 概要 [#summary]
`Container`の子`Component`を再帰的にすべて取得するメソッドを作成し、`JFileChooser`に配置された`JTable`を取得します。

#download(https://drive.google.com/uc?export=view&id=1NedWhPhVuMDTwrHRaFdW-YXZjdH-019yuw)

* サンプルコード [#sourcecode]
#code(link){{
public static Stream<Component> stream(Container parent) {
  return Arrays.stream(parent.getComponents())
    .filter(Container.class::isInstance).map(c -> stream(Container.class.cast(c)))
    .reduce(Stream.of(parent), Stream::concat);
}
//...
stream(chooser)
  .filter(JTable.class::isInstance).map(JTable.class::cast)
  .findFirst()
  .ifPresent(t -> t.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN));
}}

* 解説 [#explanation]
上記のサンプルでは、`JFileChooser`の詳細表示で使用されている`JTable`を取得し、その自動サイズ変更モードを変更しています。

- メモ
-- `JPopupMenu`などの子`Component`は取得しない
--- [[JFileChooserでの隠しファイルの非表示設定を変更する>Swing/FileHidingEnabled]]
-- `JDK1.8`で導入された`Stream`を使用
--- 以下のように`flatMap`を使用する方法もある
#code{{
public static Stream<Component> stream5(Container parent) {
  return Arrays.stream(parent.getComponents())
    .filter(Container.class::isInstance).map(Container.class::cast)
    .flatMap(c -> Stream.concat(Stream.of(c), stream5(c)));
}
public static Stream<Component> stream6(Container parent) {
  return Stream.concat(Stream.of(parent), Arrays.stream(parent.getComponents())
    .filter(Container.class::isInstance).map(Container.class::cast)
    .flatMap(MainPanel::stream6));
}
}}
--- 以下のように、`Stream`を使用しない方法もある
#code{{
public static boolean searchAndResizeMode(Container parent) {
  for (Component c: parent.getComponents()) {
    if (c instanceof JTable) {
      ((JTable) c).setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
      return true;
    } else if (c instanceof Container && searchAndResizeMode((Container) c)) {
      return true;
    }
  }
  return false;
}
}}

* 参考リンク [#reference]
- [[JFileChooserのデフォルトをDetails Viewに設定>Swing/DetailsViewFileChooser]]
- [[JFileChooserでの隠しファイルの非表示設定を変更する>Swing/FileHidingEnabled]]
- [http://www.java2s.com/Code/Java/Swing-JFC/GetAllComponentsinacontainer.htm Get All Components in a container : Container « Swing JFC « Java]

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