| |
JTree.getModel().addTreeModelListener(new TreeModelListener()) |
|
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeExpansionListener;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
public class Main {
public static void main(String args[]) {
JFrame f = new JFrame("JTree Demo");
f.setSize(260, 240);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
DefaultMutableTreeNode aNode = new DefaultMutableTreeNode("A");
root.add(aNode);
DefaultMutableTreeNode bNode = new DefaultMutableTreeNode("B");
aNode.add(bNode);
DefaultMutableTreeNode cNode = new DefaultMutableTreeNode("C");
aNode.add(cNode);
cNode.add(new DefaultMutableTreeNode("D"));
cNode.add(new DefaultMutableTreeNode("E"));
DefaultMutableTreeNode fNode = new DefaultMutableTreeNode("F");
root.add(fNode);
DefaultMutableTreeNode gNode = new DefaultMutableTreeNode("G");
fNode.add(gNode);
fNode.add(new DefaultMutableTreeNode("H"));
gNode.add(new DefaultMutableTreeNode("I"));
JTree jtree = new JTree(root);
jtree.setEditable(true);
TreeSelectionModel tsm = jtree.getSelectionModel();
tsm.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
jtree.addTreeExpansionListener(new TreeExpansionListener() {
public void treeExpanded(TreeExpansionEvent tee) {
TreePath tp = tee.getPath();
System.out.println("Expansion: " + tp.getLastPathComponent());
}
public void treeCollapsed(TreeExpansionEvent tee) {
TreePath tp = tee.getPath();
System.out.println("Collapse: " + tp.getLastPathComponent());
}
});
jtree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent tse) {
TreePath tp = tse.getPath();
System.out.println("Selection event: " + tp.getLastPathComponent());
}
});
jtree.getModel().addTreeModelListener(new TreeModelListener() {
public void treeNodesChanged(TreeModelEvent tme) {
TreePath tp = tme.getTreePath();
Object[] children = tme.getChildren();
DefaultMutableTreeNode changedNode;
if (children != null)
changedNode = (DefaultMutableTreeNode) children[0];
else
changedNode = (DefaultMutableTreeNode) tp.getLastPathComponent();
System.out.println("Model change path: " + tp + "New data: "
+ changedNode.getUserObject());
}
public void treeNodesInserted(TreeModelEvent tme) {
}
public void treeNodesRemoved(TreeModelEvent tme) {
}
public void treeStructureChanged(TreeModelEvent tme) {
}
});
f.add(new JScrollPane(jtree));
f.setVisible(true);
}
}
|
|
|
Related examples in the same category |
1. | 创建一个表格 | | | 2. | Creates tables that allow rows and columns to be added or deleted | | | 3. | 从列表数据和列名建立一个表 | | | 4. | Getting the Number of Rows and Columns in a JTable Component | | | 5. | 添加数据到表格组件 | | | 6. | 通过DefaultTableModel添加列到表格 | | | 7. | 通过DefaultTableModel插入一行到表格 | | | 8. | Insert a row to a table through DefaultTableModel at specified row | | | 9. | 滚动表 | | | 10. | 简单示范表格 | | | 11. | 创建一个表从两个二维数组 | | | 12. | 创建表与Unicode数据 | | | 13. | 使用表,以显示输入的数据(整数,布尔,彩色) | | | 14. | 调整表的大小 | | | 15. | 费用表 | | | 16. | StockTable 6:事件和动态显示 | | | 17. | 表格选择事件 | | | 18. | 表行和列的选择 | | | 19. | 表选择模式 | | | 20. | Use code to change Table Selection | | | 21. | 结果显示在表(表格) | | | 22. | 表格与JDBC结果 | | | 23. | 结果表:继承AbstractTable和JDBC结果集ResultSet | | | 24. | 列表界面性能,表格和排序 | | | 25. | 属性表:使用表格来显示和编辑属性 | | | 26. | 表与工具提示,单元格和列标题 | | | 27. | StockTable 4 :表排序 | | | 28. | Table Sort Test | | | 29. | 表格排序:点击表头进行表排序 | | | 30. | 创建表格的图像 | | | 31. | JTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); | | | 32. | JTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); | | | 33. | JTable.setColumnSelectionAllowed(boolean b); | | | 34. | JTable.setRowSelectionAllowed(boolean b); | | | 35. | JTable.setCellSelectionEnabled(boolean b); | | | 36. | 处理选择和模型的变化事件 | | | 37. | ListSelectionModel rowSelMod = JTable.getSelectionModel(); | | | 38. | ListSelectionModel colSelMod = JTable.getColumnModel().getSelectionModel(); | | | 39. | Move the last visible column so it becomes the first visible column | | | 40. | the last column is moved to the first position | | | 41. | 在一个表格允许用户调整列 | | | 42. | 检索表中可见单元格的值 | | | 43. | 从模型中检索表中的数据 | | | 44. | Change a cell in the 2nd visible column | | | 45. | Change a cell in the 3rd column in the model | | | 46. | 在非可卷动的表格显示表头 | | | 47. | 在一个表格改变列名称 | | | 48. | 在表头显示图标 | | | 49. | Implementing Variable-Height Column Headers in a JTable Component | | | 50. | Removing the Column Headers from a Scrollable in a JTable Component | | | 51. | Creating a Custom Column Header Renderer in a JTable Component | | | 52. | Setting Column Header Tool Tips in a JTable Components | | | 53. | 对表格单元格组件设置工具提示 | | | 54. | 在表格使行默认选择 | | | 55. | 在表格设置有效列 | | | 56. | 选择表格单元格 | | | 57. | 得到默认的选择模式: MULTIPLE_INTERVAL_SELECTION | | | 58. | 只允许单一选择 | | | 59. | Allow selection to span one contiguous set of rows, visible columns, or block of cells | | | 60. | Allow multiple selections of rows, visible columns, or cell blocks (default) | | | 61. | 选择一个列 | | | 62. | 选择一个系列 | | | 63. | 取消一系列栏的选择 | | | 64. | 选中某行-列0 | | | 65. | 选择一个额外的一系列行 | | | 66. | 取消一系列行的选择 | | | 67. | 选择一个单元格:单元格( 2,1 ) | | | 68. | 将选定内容扩展到包括所有单元格( 5,3 ) | | | 69. | Deselect a cell: cell (3,2), All cells in the row and column containing (3,2) are deselected. | | | 70. | Toggles the selection state, if it were called again, it exactly reverses the first call. | | | 71. | 选择所有单元格 | | | 72. | 取消所有单元格 | | | 73. | Column selection is enabled, get the indices of the selected columns | | | 74. | Row selection is enabled, Get the indices of the selected rows | | | 75. | 活动被选定行的索引 | | | 76. | 获得最大和最小范围内选定的单元格 | | | 77. | Check each cell in the min and max ranges of selected cells | | | 78. | 在一个表格禁用选则 | | | 79. | 获得选中的表单元格 | | | 80. | 建立一个可滚动表格 | | | 81. | Disable auto resizing to make the table horizontal scrollable | | | 82. | 确定是否一个单元格可见 | | | 83. | 使表格中的单元格可见 | | | 84. | 滚动单元格 | | | 85. | 显示横向和纵向网格线(默认) | | | 86. | 不显示任何网格线 | | | 87. | 只显示垂直网格线 | | | 88. | 只显示水平网格线 | | | 89. | 设置网格颜色 | | | 90. | 获取表格组件单元格之间间隙的大小 | | | 91. | 在单元格的左右添加空格 | | | 92. | 增加行高 | | | 93. | Programmatically Starting Cell Editing in a JTable Component | | | 94. | Save the current value in the cell being edited and stops the editing process | | | 95. | Discard any changes made by the user and stops the editing process | | | 96. | 禁用用户编辑表格 | | | 97. | 禁用用户编辑表格与DefaultTableModel | | | 98. | 基于列在一个表格排序列 | | | 99. | 表格排序列 | | | 100. | 表格组件选择事件监听 | | | 101. | Listening for Changes to the Rows and Columns of a JTable Component | | | 102. | 在一个表格监听列相关的变化事件 | | | 103. | Listening for Clicks on a Column Header in a JTable Component | | | 104. | 排序和筛选表 | | | 105. | 使用regexFilter过滤表 | | | 106. | 设置表格行高度 | | |
|