清除jList的选择

时间:2014-06-06 19:27:42

标签: java swing jlist jtree

我有一个jtree和一个jlist,jlist取决于选择了什么jtree节点,当我选择de节点颜色时,jlist添加两个项目。然后我有一个jTextfield,我把选中的值放在jlist中。

我的问题是当我想在jTree中选择另一个节点时,选择了jlist上的值。

这就是我在jTreeValueChanged行动中所拥有的

private void jTree1ValueChanged(javax.swing.event.TreeSelectionEvent evt) {                                    

    jTextField1.setText("");
    jList1.clearSelection();


    jList1.repaint();
    //DefaultMutableTreeNode seleccion;
    seleccion=(DefaultMutableTreeNode) jTree1.getLastSelectedPathComponent();

    if(seleccion.toString()=="colors"){

        jList1.setModel( model  );
        model.addElement("rojo");
        model.addElement("azul");
    }
    else if(seleccion.toString()=="sports"){
         jList1.setModel(model );
        model.addElement("futbol");
        model.addElement("beisbol");

    }

} 

现在这是jListValueChanged中的代码:

private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {   

    String seleccion =jList1.getSelectedValue().toString();
    jTextField1.setText(seleccion);


}  

当我选择另一个节点时有例外:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at navegacion.arbolconlist.jList1ValueChanged(arbolconlist.java:124)
at navegacion.arbolconlist.access$100(arbolconlist.java:17)
at navegacion.arbolconlist$2.valueChanged(arbolconlist.java:60)
at javax.swing.JList.fireSelectionValueChanged(JList.java:1796)
at javax.swing.JList$ListSelectionHandler.valueChanged(JList.java:1810)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:184)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:164)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:211)
at javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:405)
at javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:415)
at javax.swing.DefaultListSelectionModel.removeSelectionIntervalImpl(DefaultListSelectionModel.java:576)
at javax.swing.DefaultListSelectionModel.clearSelection(DefaultListSelectionModel.java:420)
at javax.swing.JList.clearSelection(JList.java:2043)
at navegacion.arbolconlist.jTree1ValueChanged(arbolconlist.java:101)
at navegacion.arbolconlist.access$000(arbolconlist.java:17)
at navegacion.arbolconlist$1.valueChanged(arbolconlist.java:53)
at javax.swing.JTree.fireValueChanged(JTree.java:2921)
at javax.swing.JTree$TreeSelectionRedirector.valueChanged(JTree.java:3382)
at javax.swing.tree.DefaultTreeSelectionModel.fireValueChanged(DefaultTreeSelectionModel.java:635)
at javax.swing.tree.DefaultTreeSelectionModel.notifyPathChange(DefaultTreeSelectionModel.java:1093)
at javax.swing.tree.DefaultTreeSelectionModel.setSelectionPaths(DefaultTreeSelectionModel.java:294)
at javax.swing.tree.DefaultTreeSelectionModel.setSelectionPath(DefaultTreeSelectionModel.java:188)
at javax.swing.JTree.setSelectionPath(JTree.java:1633)
at javax.swing.plaf.basic.BasicTreeUI.selectPathForEvent(BasicTreeUI.java:2393)
at javax.swing.plaf.basic.BasicTreeUI$Handler.handleSelection(BasicTreeUI.java:3609)
at javax.swing.plaf.basic.BasicTreeUI$Handler.mousePressed(BasicTreeUI.java:3548)
at java.awt.Component.processMouseEvent(Component.java:6524)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6292)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4883)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4530)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

1 个答案:

答案 0 :(得分:1)

首先检查选择,然后从JList获取值。如果没有选择,请尝试使用返回-1的{​​{3}}而不是null,如果没有选择则返回NullPointerException

如果没有选择,那么下面一行将导致String seleccion =jList1.getSelectedValue().toString();

{{1}}

详细了解JList#getSelectedIndex()并查找示例代码。