⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 treemodeleventdemo.java

📁 《java事件处理指南》一书的代码,好东西
💻 JAVA
字号:
import javax.swing.*;import javax.swing.tree.*;import javax.swing.event.*;import java.awt.*;import java.awt.event.*;public class TreeModelEventDemo extends JFrame implements ActionListener{   private JTree tree;   private JButton button;   private DefaultTreeModel model;   public TreeModelEventDemo()    {/*  A Collection of DefaultMutableTreeNode objects are created that  *//*  will form the components of a tree.                              */      DefaultMutableTreeNode league =                new DefaultMutableTreeNode("Soccer League");      DefaultMutableTreeNode north =                   new DefaultMutableTreeNode("North Division");      DefaultMutableTreeNode south =                   new DefaultMutableTreeNode("South Divison");      DefaultMutableTreeNode jets =                       new DefaultMutableTreeNode("Jets");      DefaultMutableTreeNode raiders =                       new DefaultMutableTreeNode("Raiders");      DefaultMutableTreeNode fins =                       new DefaultMutableTreeNode("Fins");      DefaultMutableTreeNode shooters =                       new DefaultMutableTreeNode("Shooters");      DefaultMutableTreeNode lions =                       new DefaultMutableTreeNode("Lions");      DefaultMutableTreeNode tigers =                       new DefaultMutableTreeNode("Tigers");/*  The node hierarchy is loaded into a DefaultTreeModel object.   *//*  The tree model registers a TreeModelListener.                  */      model = new DefaultTreeModel(league);      model.insertNodeInto(north, league, 0);      model.insertNodeInto(south, league, 1);      model.insertNodeInto(jets, north, 0);      model.insertNodeInto(raiders, north, 1);      model.insertNodeInto(fins, north, 2);      model.insertNodeInto(shooters, south, 0);      model.insertNodeInto(lions, south, 1);      model.insertNodeInto(tigers, south, 2);      model.addTreeModelListener(new TreeModelHandler());/*  A JTree object is created using the previously created tree model.  */      tree = new JTree(model);      tree.setShowsRootHandles(true);      tree.expandRow(1);      button = new JButton("remove");      button.setBorder(BorderFactory.createRaisedBevelBorder());      button.addActionListener(this);      JPanel panel = new JPanel();      panel.add(button);      getContentPane().add( new JScrollPane(tree), BorderLayout.CENTER );      getContentPane().add( panel, BorderLayout.SOUTH );      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      setBounds(100, 100, 400, 300);      setVisible(true);   }/*  The TreeModelListener is implemented as an inner class.           *//*  When the user removes a tree node, a TreeModelEvent is generated  *//*  and sent to the treeNodesRemoved() method. This method uses the   *//*  event to obtain a reference to the node that was removed, its     *//*  parent node, and its index.  It then displays a confirm dialog,   *//*  so the user can confirm the removal.  If the user does not        *//*  confirm the the removal, the node is placed back inside the       *//*  tree. The other methods declared in the TreeModelListener         *//*  interface are not used, so they are implemented as stub methods.  */   class TreeModelHandler implements TreeModelListener   {      public void treeNodesRemoved(TreeModelEvent event)      {         Object[] path = event.getPath();         Object[] child = event.getChildren();         int[] indices = event.getChildIndices();         int confirm =              JOptionPane.showConfirmDialog(tree, "Confirm node deletion?");         if  ( confirm != JOptionPane.YES_OPTION )         {            model.insertNodeInto( (MutableTreeNode)child[0],                                   (MutableTreeNode)path[path.length-1],                                   indices[0]);         }      }      public void treeNodesChanged(TreeModelEvent event) {}      public void treeNodesInserted(TreeModelEvent event) {}      public void treeStructureChanged(TreeModelEvent event) {}   }/*  When the "remove" button is pressed, the selected node is removed  *//*  from the tree.  This generates a TreeModelEvent that is sent to    *//*  the TreeModelListener.                                             */   public void actionPerformed(ActionEvent event)   {      DefaultMutableTreeNode node =                (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();      if ( !node.isRoot() )      {         model.removeNodeFromParent(node);      }   }   public static void main(String args[])    {      TreeModelEventDemo demo = new TreeModelEventDemo();   }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -