Swing/GetComponentsRecursively のバックアップの現在との差分(No.14)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/GetComponentsRecursively へ行く。
- 1 (2017-02-06 (月) 16:08:58)
- 2 (2017-02-07 (火) 12:54:52)
- 3 (2017-02-07 (火) 19:03:12)
- 4 (2017-02-20 (月) 19:40:38)
- 5 (2017-02-28 (火) 17:51:53)
- 6 (2017-03-07 (火) 14:31:16)
- 7 (2018-01-07 (日) 18:29:32)
- 8 (2018-02-15 (木) 14:23:42)
- 9 (2019-05-22 (水) 19:35:38)
- 10 (2020-01-02 (木) 19:28:25)
- 11 (2020-05-04 (月) 17:09:12)
- 12 (2021-10-31 (日) 01:08:58)
- 13 (2022-12-30 (金) 01:45:38)
- 14 (2024-02-26 (月) 13:39:27)
- 追加された行はこの色です。
- 削除された行はこの色です。
--- 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?id=1NedWhPhVuMDTwrHRaFdW-YXZjdH-019yuw --- * 概要 [#summary] `Container`の子`Component`を再帰的にすべて取得するメソッドを作成し、`JFileChooser`に配置された`JTable`を取得します。 #download(https://drive.google.com/uc?id=1NedWhPhVuMDTwrHRaFdW-YXZjdH-019yuw) * サンプルコード [#sourcecode] #code(link){{ public static Stream<Component> descendants(Container parent) { return Stream.of(parent.getComponents()) .filter(Container.class::isInstance).map(Container.class::cast) .filter(Container.class::isInstance) .map(Container.class::cast) .flatMap(c -> Stream.concat(Stream.of(c), descendants(c))); } // ... descendants(chooser) .filter(JTable.class::isInstance).map(JTable.class::cast) descendants(fileChooser) .filter(JTable.class::isInstance) .map(JTable.class::cast) .findFirst() .ifPresent(t -> t.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN)); }} * 解説 [#explanation] 上記のサンプルでは、`JFileChooser`の詳細表示で使用されている`JTable`を取得し、その自動サイズ変更モードを変更しています。 - `Stream#flatMap(...)`を使用してすべての子コンポーネントを検索しそのストリームを取得する -- 上記の関数で`JFileChooser`のすべての子コンポーネントのストリームを取得し、さらに`JFileChooser`の詳細表示に使用されている`JTable`を絞り込む -- 詳細表示用`JTable`を発見したらその自動サイズ変更モードを`JTable#setAutoResizeMode(...)`で変更 -- `JPopupMenu`が`JComponent#setComponentPopupMenu(...)`で設定されていてもその子`Component`は取得しない --- [[JFileChooserでの隠しファイルの非表示設定を変更する>Swing/FileHidingEnabled]] - `JPopupMenu`が`JComponent#setComponentPopupMenu(...)`で設定されていてもその子`Component`は取得しない -- [[JFileChooserでの隠しファイルの非表示設定を変更する>Swing/FileHidingEnabled]] - `JDK1.8`で導入された`Stream`を使用 -- 以下のように`reduce`を使用する方法もある - `Stream#reduce(...)`を使用して検索対象のルートコンテナを含めたすべてのコンポーネントを検索しそのストリームを取得する別方法: #code{{ public static Stream<Component> descendantOrSelf(Container parent) { return Stream.of(parent.getComponents()) .filter(Container.class::isInstance) .map(c -> descendantOrSelf((Container) c)) .reduce(Stream.of(parent), Stream::concat); } }} -- 以下のように、`Stream`を使用しない方法もある - `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