Swing/ExpandAllNodes のバックアップの現在との差分(No.1)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/ExpandAllNodes へ行く。
- 1 (2007-05-07 (月) 17:11:21)
- 2 (2008-02-05 (火) 19:31:06)
- 3 (2008-03-30 (日) 03:02:57)
- 4 (2012-01-12 (木) 16:17:44)
- 5 (2012-11-21 (水) 21:48:29)
- 6 (2013-02-06 (水) 02:10:28)
- 7 (2014-05-09 (金) 12:10:32)
- 8 (2014-05-15 (木) 19:09:20)
- 9 (2014-11-07 (金) 03:21:30)
- 10 (2015-02-12 (木) 15:47:44)
- 11 (2016-11-01 (火) 19:52:19)
- 12 (2017-02-07 (火) 20:52:40)
- 13 (2017-12-22 (金) 17:42:05)
- 14 (2018-03-30 (金) 16:10:48)
- 15 (2018-09-25 (火) 15:38:27)
- 16 (2020-09-26 (土) 17:30:11)
- 17 (2022-05-24 (火) 09:57:17)
- 追加された行はこの色です。
- 削除された行はこの色です。
TITLE:JTreeのノードを展開・折り畳み #navi(../) *JTreeのノードを展開・折り畳み [#qe99cde9] >編集者:[[Terai Atsuhiro>terai]]~ 作成日:&date~ 更新日:&lastmod; --- category: swing folder: ExpandAllNodes title: JTreeのノードを展開・折り畳み tags: [JTree, TreeNode, TreePath] author: aterai pubdate: 2007-05-07T17:11:21+09:00 description: JTreeのすべてのノードに対して、展開、折り畳みを行います。 image: https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTMS3T9nvI/AAAAAAAAAY8/ooi4QMbp6fA/s800/ExpandAllNodes.png --- * 概要 [#summary] `JTree`のすべてのノードに対して、展開、折り畳みを行います。 #contents #download(https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTMS3T9nvI/AAAAAAAAAY8/ooi4QMbp6fA/s800/ExpandAllNodes.png) **概要 [#t2b09e6b] JTreeのすべてのノードに対して、展開、折り畳みを行います。 #screenshot **サンプルコード [#ia645451] #code{{ private void visitAll(JTree tree, TreePath parent, boolean expand) { TreeNode node = (TreeNode)parent.getLastPathComponent(); if(!node.isLeaf() && node.getChildCount()>=0) { Enumeration e = node.children(); while(e.hasMoreElements()) { TreeNode n = (TreeNode)e.nextElement(); TreePath path = parent.pathByAddingChild(n); visitAll(tree, path, expand); } } if(expand) tree.expandPath(parent); else tree.collapsePath(parent); } * サンプルコード [#sourcecode] #code(link){{ private void expandAll(JTree tree) { int row = 0; while(row<tree.getRowCount()) { while (row < tree.getRowCount()) { tree.expandRow(row); row++; } } private void collapseAll(JTree tree) { int row = tree.getRowCount()-1; while(row>=0) { int row = tree.getRowCount() - 1; while (row >= 0) { tree.collapseRow(row); row--; } } }} -&jnlp; -&jar; -&zip; **解説 [#gee86b85] - visitAll -- 再帰的にTreePathを辿って、JTree#expandPath(TreePath)、JTree#pacollapsePath(TreePath)メソッドを実行することで、展開、折り畳みを行っています。 private void visitAll(JTree tree, TreePath parent, boolean expand) { TreeNode node = (TreeNode) parent.getLastPathComponent(); // children(node).forEach(n -> visitAll(tree, parent.pathByAddingChild(n), expand)); if (!node.isLeaf()) { // Java 9: Enumeration<TreeNode> e = node.children(); Enumeration<?> e = node.children(); while (e.hasMoreElements()) { visitAll(tree, parent.pathByAddingChild(e.nextElement()), expand); } } if (expand) { tree.expandPath(parent); } else { tree.collapsePath(parent); } } - expandAll -- JTreeをリストとみなしてexpandAllでは先頭から順番にJTree#expandRow(int)メソッドを実行しています。 -- ループは全展開された時のJTreeの行インデックス数だけ繰り返されます。 // private static Stream<TreeNode> children(TreeNode node) { // // Java 9: // // return Collections.list(node.children()).stream(); // // Java 8: // return Collections.list((Enumeration<?>) node.children()) // .stream().filter(TreeNode.class::isInstance).map(TreeNode.class::cast); // } }} - collapseAll -- 末尾から順番にJTree#collapseRow(int)メソッドを実行し、見かけ上すべてのノードを折り畳みます。 -- 子ノードは展開されているが、親ノードが折り畳まれている場合、その子ノードは折り畳まれません。 * 解説 [#explanation] - `expandAll(A)` -- `JTree`をリストとみなして`expandAll`では先頭から順番に`JTree#expandRow(int)`メソッドを実行 -- ループは全展開された時の`JTree`の行数だけ繰り返す - `collapseAll(A)` -- 末尾から順番に`JTree#collapseRow(int)`メソッドを実行し、表示されているすべてのノードを折り畳む -- 子ノードは展開されているが親ノードが折り畳まれている場合、その非表示となっている子ノードに対しては折り畳みを実行しない - `expandAll(B)` -- `visitAll(tree, new TreePath(root), true);` -- 再帰的に`TreePath`を辿って展開(`JTree#expandPath(TreePath)`メソッド)を実行 - `collapseAll(B)` -- `visitAll(tree, new TreePath(root), false);` -- 再帰的に`TreePath`を辿って折り畳み(`JTree#collapsePath(TreePath)`メソッド)を実行 **参考リンク [#je145430] -[[Expanding or Collapsing All Nodes in a JTree Component (Java Developers Almanac Example)>http://www.exampledepot.com/egs/javax.swing.tree/ExpandAll.html]] -[[Expand or collapse a JTree - Real's Java How-to>http://64.18.163.122/rgagnon/javadetails/java-0210.html]] * 参考リンク [#reference] - [https://www.rgagnon.com/javadetails/java-0210.html Expand or collapse a JTree - Real's Java How-to] - [[JTreeのノードを検索する>Swing/SearchBox]] **コメント [#d120e1b9] * コメント [#comment] #comment - 解説中のラベル`A`と`B`が、ソースコードとは逆になっていたので、これをソースコードと同じになるよう修正。 -- &user(aterai); &new{2008-02-05 (火) 19:31:53}; #comment