Swing/GetComponentsRecursively のバックアップ差分(No.7)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - 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?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`は取得しない -- `JPopupMenu`が`JComponent#setComponentPopupMenu(...)`で設定されていても、その子`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