Swing/ExpandedDescendants のバックアップの現在との差分(No.2)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/ExpandedDescendants へ行く。
- 1 (2013-11-19 (火) 10:23:24)
- 2 (2013-11-19 (火) 14:49:22)
- 3 (2013-11-20 (水) 10:36:44)
- 4 (2014-06-10 (火) 19:03:44)
- 5 (2014-10-17 (金) 15:02:30)
- 6 (2015-11-11 (水) 20:06:10)
- 7 (2016-01-03 (日) 06:03:06)
- 8 (2016-05-30 (月) 18:13:17)
- 9 (2016-06-24 (金) 16:30:06)
- 10 (2017-09-21 (木) 04:41:02)
- 11 (2017-12-25 (月) 21:36:30)
- 12 (2019-05-22 (水) 19:35:38)
- 13 (2019-12-19 (木) 15:54:29)
- 14 (2021-06-22 (火) 17:54:11)
- 追加された行はこの色です。
- 削除された行はこの色です。
TITLE:JTreeの展開状態を記憶・復元する #navi(../) #tags(JTree, TreePath) RIGHT:Posted by &author(aterai); at 2013-11-18 * JTreeの展開状態を記憶・復元する [#fbc1880a] `JTree`でノードの展開状態を記憶、復元します。 --- category: swing folder: ExpandedDescendants title: JTreeの展開状態を記憶・復元する tags: [JTree, TreePath] author: aterai pubdate: 2013-11-18T00:04:12+09:00 description: JTreeのノードが展開されているかどうかを記憶、復元します。 image: https://lh4.googleusercontent.com/-FcYsZkFYSxE/UojBcoMtHwI/AAAAAAAAB6k/A7D221doy2w/s800/ExpandedDescendants.png --- * 概要 [#summary] `JTree`のノードが展開されているかどうかを記憶、復元します。 - &jnlp; - &jar; - &zip; #download(https://lh4.googleusercontent.com/-FcYsZkFYSxE/UojBcoMtHwI/AAAAAAAAB6k/A7D221doy2w/s800/ExpandedDescendants.png) #ref(https://lh4.googleusercontent.com/-FcYsZkFYSxE/UojBcoMtHwI/AAAAAAAAB6k/A7D221doy2w/s800/ExpandedDescendants.png) ** サンプルコード [#ed1c3655] * サンプルコード [#sourcecode] #code(link){{ visitAll(tree, rootPath, false); //Collapse all if(expandedState == null) { return; } while(expandedState.hasMoreElements()) { if (expandedState == null) { return; } while (expandedState.hasMoreElements()) { tree.expandPath(expandedState.nextElement()); } expandedState = tree.getExpandedDescendants(rootPath); }} ** 解説 [#aef5bae1] 上記のサンプルでは、`JTree#getExpandedDescendants(TreePath)`メソッドで展開されているノードの`TreePath`を`Enumeration`で保存しています。復元は一旦すべてのノードを折り畳んでから、`JTree#expandPath(TreePath)`で`Enumeration<TreePath>`から取得したノードを展開しています。 * 解説 [#explanation] 上記のサンプルでは、`JTree#getExpandedDescendants(TreePath)`メソッドを使用して展開されているノードの`TreePath`を`Enumeration`に保存しています。復元は一旦すべてのノードを折り畳んでから`JTree#expandPath(TreePath)`メソッドを使用して`Enumeration<TreePath>`から取得したノードを展開しています。 - 注: -- 親ノードが閉じている場合、その子ノードの展開状態は記憶していない -- [http://docs.oracle.com/javase/jp/7/api/javax/swing/JTree.html#getExpandedDescendants%28javax.swing.tree.TreePath%29 JTree#getExpandedDescendants(TreePath) (Java Platform SE 7)] - 親ノードが閉じている場合、その子ノードの展開状態は記憶していない -- このサンプルでの例を挙げると`Set 004`を展開して親の`Set 001`を折り畳んだ状態で`JTree#getExpandedDescendants(TreePath)`を使用した場合、戻り値の`Enumeration<TreePath>`に`Set 004`へのパスは含まれない - [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/JTree.html#getExpandedDescendants-javax.swing.tree.TreePath- JTree#getExpandedDescendants(TreePath)]メソッドのドキュメントの説明がよく分からない(特に前半)がもしかしたら上記のことを言っているのかもしれない… If you expand/collapse nodes while iterating over the returned Enumeration this may not return all the expanded paths, or may return paths that are no longer expanded. 返された Enumeration で繰り返している間ノードを展開するか、折りたたむと、このメソッドは展開されたすべてのパスを返すのではなく、それ以上展開されていないパスを返します。 ---- - `JTree`のシリアライズに関するのメモ #code{{ //XMLEncoderではデフオルトのJTreeの場合、展開状態などは保存されない //??? 1.7.0_45では、整形式のXMLにならない場合がある ??? XMLEncoder xe = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(xmlFile))); xe.writeObject(tree); xe.flush(); // XMLEncoderではデフォルトのJTreeの場合、展開状態などは保存されない try (XMLEncoder xe = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(xmlFile)))) { xe.writeObject(tree); // xe.close(); // ... //ObjectOutputStreamの場合は、選択状態、展開状態なども保存、復元可能 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file)); oos.writeObject(tree); oos.flush(); // ObjectOutputStreamの場合は、選択状態、展開状態なども保存、復元可能 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file)); oos.writeObject(tree); // ... }} ** 参考リンク [#v305a865] * 参考リンク [#reference] - [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/JTree.html#getExpandedDescendants-javax.swing.tree.TreePath- JTree#getExpandedDescendants(TreePath) (Java Platform SE 8)] - [[JTreeのノードを展開・折り畳み>Swing/ExpandAllNodes]] - [http://www.javalobby.org/java/forums/t19857.html Swing: Retaining JTree Expansion State] - [http://orinanobworld.blogspot.jp/2013/03/auto-collapsing-tree-in-java.html OR in an OB World: Auto-collapsing Tree in Java] ** コメント [#c8627c0b] * コメント [#comment] #comment #comment