JTree用于以树状结构显示分层数据。JTree中的每个项目都称为节点。
节点还被分类为分支节点或叶节点。
JTree使用选择模型来跟踪选定的节点。选择模型是TreeSelectionModel接口的一个实例。
JTree允许用户以三种不同的模式选择节点。它们由TreeSelectionModel接口中定义的三个常量表示:
下面的代码演示了如何使用JTree的选择模型的一些方法:
TreeSelectionModel selectionModel = tree.getSelectionModel(); selectionModel.setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION); int selectedCount = selectionModel.getSelectionCount(); TreePath[] selectedPaths = selectionModel.getSelectionPaths();
我们可以向JTree添加一个TreeSelectionListener,当选择或取消选择一个节点时会通知它。
从向量创建JTree
import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTree; public class Main extends JFrame { public static void main(String[] args) { Main stt = new Main(); stt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); stt.setSize(250, 250); stt.setVisible(true); } public Main() { Object[] genealogy = { "A", "B", "C", "D", "E", "F" }; java.util.Vector v = new java.util.Vector() { public String toString() { return "DD"; } }; v.addElement("1"); v.addElement("2"); v.addElement("3"); v.addElement("4"); genealogy[0] = v; JTree tree = new JTree(genealogy); tree.setRootVisible(true); JScrollPane jsp = new JScrollPane(tree); getContentPane().add(jsp); } }
import java.awt.BorderLayout; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; public class Main { public static void main(String args[]) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root"); DefaultMutableTreeNode mercury = new DefaultMutableTreeNode("Mercury"); root.insert(mercury, 0); DefaultMutableTreeNode venus = new DefaultMutableTreeNode("Venus"); root.insert(venus, 1); DefaultMutableTreeNode mars = new DefaultMutableTreeNode("Mars"); root.insert(mars, 2); JTree tree = new JTree(root); JScrollPane scrollPane = new JScrollPane(tree); frame.add(scrollPane, BorderLayout.CENTER); frame.setSize(300, 150); frame.setVisible(true); } }
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.InputEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; //from w ww. j a v a2 s . c o m import javax.swing.JFrame; import javax.swing.JMenuItem; import javax.swing.JPopupMenu; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.TreePath; public class Main extends JFrame { protected JTree tree; public static void main(String[] args) { Main dn = new Main(new JTree()); dn.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); dn.setSize(400, 300); dn.setVisible(true); } public Main(JTree jt) { super(); tree = jt; getContentPane().add(tree); tree.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent event) { if (((event.getModifiers() & InputEvent.BUTTON3_MASK) != 0) && (tree.getSelectionCount() > 0)) { showMenu(event.getX(), event.getY()); } } }); } protected void showMenu(int x, int y) { JPopupMenu popup = new JPopupMenu(); JMenuItem mi = new JMenuItem("Delete"); TreePath path = tree.getSelectionPath(); Object node = path.getLastPathComponent(); if (node == tree.getModel().getRoot()) { mi.setEnabled(false); } popup.add(mi); mi.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { deleteSelectedItems(); } }); popup.show(tree, x, y); } protected void deleteSelectedItems() { DefaultMutableTreeNode node; DefaultTreeModel model = (DefaultTreeModel) (tree.getModel()); TreePath[] paths = tree.getSelectionPaths(); for (int i = 0; i < paths.length; i++) { node = (DefaultMutableTreeNode) (paths[i].getLastPathComponent()); model.removeNodeFromParent(node); } } }
我们将通过一个简单的 Demo 来阐述 MyBatis-Plus 的强大功能,在此之前,我们假设您已经:拥有 Java 开发环境以及相应 IDE熟...
MyBatis-Plus 条件构造器-AbstractWrapperlikeLeft(R column, Object val)likeLeft(boolean condition, R column, Obj...
MyBatis-Plus 条件构造器-AbstractWrappergroupBy(R... columns)groupBy(boolean condition, R... columns)分组:GROUPBY字段,.....
对象工厂(objectFactory)每次MyBatis创建结果对象的新实例时,它都会使用一个对象工厂(ObjectFactory)实例来完成实例化...
Spring教程 -Spring构造函数注入我们可以通过构造函数注入依赖。构造函数依赖注入发生在Java Bean的依赖关系在其构造函数中提供...