根据数据库建成了一个目录树,想通过点击界面上的刷新按钮来实现数据库改变了点刷新按钮就能生成新的目录树这样的功能.请问刷新按钮的事件应该怎么写呢?请高手帮帮忙!解决后立即结分!

解决方案 »

  1.   

    public class TreeDemo extends JFrame implements ActionListener
    {.......
        public TreeDemo() 
        {   
            super("个人新闻");
            String s=null;
            s2="shyh";
            Container con=getContentPane();   
            JPanel p1=new JPanel();
            JPanel p2=new JPanel();
            
            final Container container = this.getContentPane();
            top = new DefaultMutableTreeNode("个人新闻");        
            createNodes(top, "0");//创建根节点
            treeModel = new DefaultTreeModel(top);
            tree = new JTree(treeModel);tree.setEditable(true); 
            tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
            tree.addTreeSelectionListener(new TreeSelectionListener() 
             {public void valueChanged(TreeSelectionEvent e)
              {
                 DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
                 if (node == null)
                  return;
                 Object nodeInfo = node.getUserObject();
                    if (node.isLeaf()) 
                    {
                     NewsNode book = (NewsNode) nodeInfo;
                     createNodes(node, book.id);
                     container.validate();
                   }
                    else 
                     {
                        NewsNode newsNode = (NewsNode) nodeInfo;
                        createNodes(node, newsNode.id);
                        container.validate();
                     }
                    if (DEBUG) 
                     {
                        System.out.println(nodeInfo.toString());
                     }
                }
             });
                ......
              Icon icon3=new ImageIcon("D:/pic/sx.gif");
              button6=new JButton(icon3);//刷新按钮
             
              p1.setLayout(new GridLayout(3,5));
              p1.add(label1);p1.add(new Label());p1.add(new Label());p1.add(new Label());p1.add(new Label());
              p1.add(menubar);p1.add(new Label());p1.add(new Label());p1.add(new Label());p1.add(new Label());
              p1.add(button4);p1.add(button5);p1.add(button6);p1.add(button7);p1.add(button8);
              p2.add(button2);p2.add(button3);
              JScrollPane scrollpane=new JScrollPane(tree);
              htmlPane=new JEditorPane();
              htmlPane.setEditable(false);
              JScrollPane htmlView = new JScrollPane(htmlPane);
              JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,scrollpane,htmlView);
              con.add(p1,BorderLayout.NORTH);
              con.add(splitPane,BorderLayout.CENTER);
              con.add(p2,BorderLayout.SOUTH);
          }
        
         public void actionPerformed(ActionEvent e) 
           {
             if (e.getSource()==button6)//点刷新按钮
               {
                 请问该加那段呢?
                } 
            }
       
         public DefaultMutableTreeNode addObject(Object child)
         { DefaultMutableTreeNode parentNode = null;
           TreePath parentPath = tree.getSelectionPath();
           if (parentPath == null)
            { parentNode = top; }
           else 
            { parentNode = (DefaultMutableTreeNode) (parentPath.getLastPathComponent()); } 
           return addObject(parentNode, child, true);
         }
        public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent, Object child) 
         { return addObject(parent, child, false); } 
        public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent, Object child, boolean shouldBeVisible)
        { DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(child); 
           if (parent == null)
            { parent = top; } 
           treeModel.insertNodeInto(childNode, parent, parent.getChildCount());
           if (shouldBeVisible) 
           { tree.scrollPathToVisible(new TreePath(childNode.getPath())); }
          return childNode;
        }
        
      private void createNodes(DefaultMutableTreeNode top, String id) 
        { .......}  public static void main(String[] args) 
        {
            JFrame frame = new TreeDemo();
            .....
        }
    }
      

  2.   

    把构造函数里面的东西提出来,新建一个init函数,在actionPerformed中调用init函数。