试用JTree中。。
无聊ing,于是写一个代码库用来自己存代码。。
希望按项目保存,因此用树形列表显示。。效果如图:
- 显示根节点:setRootVisible
- 显示根节点的句柄:setShowsRootHandles
- 将对象作为节点:
重写对象的toString()方法,然后在创建节点的时候设置UserObject:DefaultMutableTreeNode projectNode = new DefaultMutableTreeNode(); projectNode.setUserObject(project);
- 获得当前节点的TreePath或TreeNode:
TreePath path = tree.getSelectionPath(); if (path == null || path.getLastPathComponent() == null) return; DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
- 编辑列表中的元素名:
首先打开tree的edit开关(setEditable为真),然后实现一个CellEditorListener,重写其中的editingCanceled和editingStopped方法,然后给tree注册:tree.getCellEditor().addCellEditorListener(new CellEditorListener() { @Override public void editingCanceled(ChangeEvent e) { // 取消编辑 } @Override public void editingStopped(ChangeEvent e) { String newValue = tree.getCellEditor().getCellEditorValue().toString(); // 执行编辑 } });
- 节点原本是对象,编辑后变成字符串:
这是因为默认的DefaultTreeCellEditor直接保存字符串为节点。。因此需要写一个CellEditor给tree用,覆盖它的getCellEditorValue()方法,使它返回UserObject。tree.setCellEditor(new DefaultTreeCellEditor(tree, (DefaultTreeCellRenderer) tree.getCellRenderer()) { @Override public Object getCellEditorValue() { if (currentCode != null) { return currentCode; } if (currentProject != null) { return currentProject; } return super.getCellEditorValue(); } });
- 节点编辑时长度比原来长,宽度足够,但是编辑完后还是原来的长度:
因为没有及时UpdateUI的关系,可是在editingStopped方法里面使用的话tree会报空指针,因此对tree加一个FocusListener,使它在得到焦点的时候自己刷新:tree.addFocusListener(new FocusListener() { @Override public void focusGained(FocusEvent e) { tree.updateUI(); } @Override public void focusLost(FocusEvent e) { } });
- 清空节点:
递归一下:private void clearNodeData(DefaultMutableTreeNode parent) { for (int i = 0; i < parent.getChildCount(); ++i) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) parent.getChildAt(i); clearNodeData(node); } parent.removeAllChildren(); } public void clearData() { clearNodeData((DefaultMutableTreeNode) tree.getModel().getRoot()); tree.updateUI(); }
- 跳到指定节点(可以做成添加结点后跳到新节点、删除结点后跳到父节点):
TreePath path = new TreePath(node.getPath()); tree.scrollPathToVisible(path); tree.setSelectionPath(path);
- 节点的拖拽:
打开drop开关(setDragEnabled为真),实现一个DropTargetAdapter,重写drop(DropTargetDropEvent dtde)方法,创建DropTarget(tree, adapter)即可:new DropTarget(tree, new DropTargetAdapter() { @Override public void drop(DropTargetDropEvent dtde) { Point pt = dtde.getLocation(); TreePath parentPath = tree.getClosestPathForLocation(pt.x, pt.y); if (parentPath == null || parentPath.getLastPathComponent() == null) { return; } DefaultMutableTreeNode node = (DefaultMutableTreeNode) parentPath.getLastPathComponent(); Object obj = node.getUserObject(); // 执行迁移 } });
嗯。。目前就用到这些。
2010年4月23日 23:30
Swing好久没写了~~
现在很不喜欢的两件事:
1.写界面
2.写Dao
喜欢写工具类~和写模拟程序~主要是测试方便~~
2010年4月23日 23:56
@筱米加步枪:
我 想写自己要用的一些小软件,因为找不到比较合适的,只好自己写Swing咯
看到后面越来越觉得Swing这套设计得太彪悍了,里面的思想很值得学习~
2023年4月16日 15:33
How to Update/Register Your Cell Number with Indian Overseas Bank, The Reserve Bank of India’s guidelines are followed by the national bank Indian Overseas Bank. The bank has been providing its clients in the public banking sector with a wealth of convenient services and amenities. Indian Overseas Bank (IOB) Mobile Banking Registration, How to Update/Register Your Cell Number with Indian Overseas Bank, The Reserve Bank of India’s guidelines are followed by the national bank Indian Overseas Bank. ekhan.net The bank has been providing its clients in the public banking sector with a wealth of convenient services and amenities. Indian Overseas Bank (IOB) Mobile Banking Registration.