Swing/ExpandedDescendants のバックアップ(No.1)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - 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)
- 15 (2025-01-03 (金) 08:57:02)
- 16 (2025-01-03 (金) 09:01:23)
- 17 (2025-01-03 (金) 09:02:38)
- 18 (2025-01-03 (金) 09:03:21)
- 19 (2025-01-03 (金) 09:04:02)
- 20 (2025-06-19 (木) 12:41:37)
- 21 (2025-06-19 (木) 12:43:47)
TITLE:JTreeの展開状態を記憶・復元する
Posted by aterai at 2013-11-18
JTreeの展開状態を記憶・復元する
JTree
でノードの展開状態を記憶、復元します。
- &jnlp;
- &jar;
- &zip;
サンプルコード
visitAll(tree, rootPath, false); //Collapse all
if(expandedState == null) { return; }
while(expandedState.hasMoreElements()) {
tree.expandPath(expandedState.nextElement());
}
expandedState = tree.getExpandedDescendants(rootPath);
View in GitHub: Java, Kotlin解説
上記のサンプルでは、JTree#getExpandedDescendants(TreePath)
メソッドで展開されているノードのTreePath
をEnumeration
で保存しています。復元は一旦すべてのノードを折り畳んでから、JTree#expandPath(TreePath)
でEnumeration<TreePath>
から取得したノードを展開しています。
- 注:
- 親ノードが閉じている場合、その子ノードの展開状態は記憶していない
- JTree (Java Platform SE 7)
JTree
のシリアライズに関するのメモ
//XMLEncoderではデフオルトのJTreeの場合、展開状態などは保存されない
//??? 1.7.0_45では、整形式のXMLにならない場合がある ???
XMLEncoder xe = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(xmlFile)));
xe.writeObject(tree);
xe.flush();
//ObjectOutputStreamの場合は、選択状態、展開状態なども保存、復元可能
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(tree);
oos.flush();