从List中填充JTree

时间:2014-09-25 21:40:17

标签: java list jtree

我试图根据从数据库中获取的信息生成树,将信息放入列表中,然后用于填充树

    Connect dao = new Connect();
    List<String> genre = dao.getGenre();
    List<String> category;

    //create the root node
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
    //create the child nodes
    for (String e : genre) {
        DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(e);
        root.add(childNode);
        category = dao.getCategoryByGenre(e);
        for (String i : category) {
            childNode.add(new DefaultMutableTreeNode(i));
        }
    }


    //create the tree by passing in the root node
    tree = new JTree(root);
    add(new JScrollPane(tree));
    tree.setShowsRootHandles(true);
    tree.setRootVisible(false);

    //adds label to bottom
    selectedLabel = new JLabel();
    add(selectedLabel, BorderLayout.SOUTH);

    tree.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {
        @Override
        public void valueChanged(TreeSelectionEvent e) {
            DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
            selectedLabel.setText(selectedNode.getUserObject().toString());
        }
    });

第一个for循环获取每个类型项并获取相关类别并将它们添加为新节点,这种方法效果很好但是当您在不同的子节点之间发生问题显然是因为我覆盖了子节点时会引发大量错误有没有办法解决它

错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at tree.Tree2$1.valueChanged(Tree2.java:55)
at javax.swing.tree.DefaultTreeSelectionModel.fireValueChanged(Unknown Source)
at javax.swing.tree.DefaultTreeSelectionModel.notifyPathChange(Unknown Source)
at javax.swing.tree.DefaultTreeSelectionModel.removeSelectionPaths(Unknown Source)
at javax.swing.JTree.removeDescendantSelectedPaths(Unknown Source)
at javax.swing.JTree.setExpandedState(Unknown Source)
at javax.swing.JTree.collapsePath(Unknown Source)
at javax.swing.plaf.basic.BasicTreeUI.toggleExpandState(Unknown Source)
at javax.swing.plaf.basic.BasicTreeUI.handleExpandControlClick(Unknown Source)
at javax.swing.plaf.basic.BasicTreeUI.checkForClickInExpandControl(Unknown Source)
at javax.swing.plaf.basic.BasicTreeUI$Handler.handleSelection(Unknown Source)
at javax.swing.plaf.basic.BasicTreeUI$Handler.mousePressed(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

0 个答案:

没有答案