• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JTreeのノードを展開・折り畳み
#navi(../)
#tags()
#tags(JTree, TreeNode, TreePath)
RIGHT:Posted by &author(aterai); at 2007-05-07
*JTreeのノードを展開・折り畳み [#qe99cde9]
JTreeのすべてのノードに対して、展開、折り畳みを行います。
* JTreeのノードを展開・折り畳み [#qe99cde9]
`JTree`のすべてのノードに対して、展開、折り畳みを行います。

-&jnlp;
-&jar;
-&zip;
#download
#ref(https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTMS3T9nvI/AAAAAAAAAY8/ooi4QMbp6fA/s800/ExpandAllNodes.png)

//#screenshot
#ref(http://lh5.ggpht.com/_9Z4BYR88imo/TQTMS3T9nvI/AAAAAAAAAY8/ooi4QMbp6fA/s800/ExpandAllNodes.png)

**サンプルコード [#ia645451]
** サンプルコード [#ia645451]
#code(link){{
//@see http://www.rgagnon.com/javadetails/java-0210.html
//Expand or collapse a JTree - Real's Java How-to
private void expandAll(JTree tree) {
  int row = 0;
  while(row<tree.getRowCount()) {
    tree.expandRow(row);
    row++;
  }
}
private void collapseAll(JTree tree) {
  int row = tree.getRowCount()-1;
  while(row>=0) {
    tree.collapseRow(row);
    row--;
  }
}
}}

#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);
}
}}

**解説 [#gee86b85]
- expandAll(A)
-- JTreeをリストとみなしてexpandAllでは先頭から順番にJTree#expandRow(int)メソッドを実行しています。
-- ループは全展開された時のJTreeの行インデックス数だけ繰り返されます。
** 解説 [#gee86b85]
- `expandAll(A)`
-- `JTree`をリストとみなして`expandAll`では先頭から順番に`JTree#expandRow(int)`メソッドを実行しています。
-- ループは全展開された時の`JTree`の行インデックス数だけ繰り返されます。

- collapseAll(A)
-- 末尾から順番にJTree#collapseRow(int)メソッドを実行し、見かけ上すべてのノードを折り畳みます。
- `collapseAll(A)`
-- 末尾から順番に`JTree#collapseRow(int)`メソッドを実行し、見かけ上すべてのノードを折り畳みます。
-- 子ノードは展開されているが、親ノードが折り畳まれている場合、その子ノードは折り畳まれません。

- expandAll(B), collapseAll(B)
-- visitAll(tree, new TreePath(root), true);
-- visitAll(tree, new TreePath(root), false);
-- 再帰的にTreePathを辿って、JTree#expandPath(TreePath)、JTree#pacollapsePath(TreePath)メソッドを実行することで、展開、折り畳みを行っています。
- `expandAll(B)`, `collapseAll(B)`
-- `visitAll(tree, new TreePath(root), true);`
-- `visitAll(tree, new TreePath(root), false);`
-- 再帰的に`TreePath`を辿って、`JTree#expandPath(TreePath)`、`JTree#pacollapsePath(TreePath)`メソッドを実行することで、展開、折り畳みを行っています。

**参考リンク [#je145430]
-%%[http://www.exampledepot.com/egs/javax.swing.tree/ExpandAll.html Expanding or Collapsing All Nodes in a JTree Component (Java Developers Almanac Example)]%%
-[http://www.rgagnon.com/javadetails/java-0210.html Expand or collapse a JTree - Real's Java How-to]
-[[JTreeのノードを検索する>Swing/SearchBox]]
** 参考リンク [#je145430]
- %%[http://www.exampledepot.com/egs/javax.swing.tree/ExpandAll.html Expanding or Collapsing All Nodes in a JTree Component (Java Developers Almanac Example)]%%
- [http://www.rgagnon.com/javadetails/java-0210.html Expand or collapse a JTree - Real's Java How-to]
- [[JTreeのノードを検索する>Swing/SearchBox]]

**コメント [#d120e1b9]
- 解説のラベルAとBが、ソースとは逆になっていたのを修正しました。 -- [[aterai]] &new{2008-02-05 (火) 19:31:53};
** コメント [#d120e1b9]
- 解説のラベル`A`と`B`が、ソースとは逆になっていたのを修正しました。 -- [[aterai]] &new{2008-02-05 (火) 19:31:53};

#comment