我有一個jtree一個jlist,jlist取決於什麼jtree節點被選中,當我選擇de節點顏色時,jlist添加兩個項目。然後我有一個jTextfield,我把選中的值放入jlist。清除選擇的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 thew代碼:
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)
'seleccion.toString()==「colors」'看起來不太好。看看[怎麼做我比較字符串在Java](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Pshemo
謝謝你評論,我解決,但我仍然有同樣的問題,當我選擇其他節點。 你知道我能做什麼嗎? –