2013-02-27 68 views
2

我想在JAVA设计级联三JComboBox的JComboBox级联

import java.awt.FlowLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.WindowConstants; 

public class ThreeCascadeJComboBox { 

private JComboBox combo1; 

private JComboBox combo2; 

private JComboBox combo3; 

public static void main(String[] args) { 
    new ThreeCascadeJComboBox(); 
} 

public ThreeCascadeJComboBox() { 
    JFrame v = new JFrame(); 
    v.getContentPane().setLayout(new FlowLayout()); 
    combo1 = new JComboBox(); 
    loadCombo1(); 
    combo1.addActionListener(new ActionListener() { 

     public void actionPerformed(ActionEvent arg0) { 
      loadCombo2((String) combo1.getSelectedItem()); 
     } 

    }); 

    combo2 = new JComboBox(); 
    loadCombo2((String) combo1.getSelectedItem()); 
    combo2.addActionListener(new ActionListener() { 

     public void actionPerformed(ActionEvent arg0) { 
      loadCombo3((String) combo2.getSelectedItem()); 
     } 

    }); 


    combo3 = new JComboBox(); 
    loadCombo3((String) combo2.getSelectedItem()); 

    v.getContentPane().add(combo1); 
    v.getContentPane().add(combo2); 
    v.getContentPane().add(combo3); 
    v.pack(); 
    v.setVisible(true); 
    v.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
} 

private void loadCombo1() { 
    combo1.addItem("letters"); 
    combo1.addItem("numbers"); 
} 

private void loadCombo2(String seleccionEnCombo1) { 
    combo2.removeAllItems(); 
    if (seleccionEnCombo1.equals("letters")) { 
     combo2.addItem("A"); 
     combo2.addItem("B"); 
     combo2.addItem("C"); 
    } else if (seleccionEnCombo1.equals("numbers")) { 
     combo2.addItem("1"); 
     combo2.addItem("2"); 
     combo2.addItem("3"); 
    } 

} 

private void loadCombo3(String seleccionEnCombo2) { 
    combo3.removeAllItems(); 
    if (seleccionEnCombo2.equals("A")) { 
     combo3.addItem("A-1"); 
     combo3.addItem("A-2"); 
     combo3.addItem("A-3"); 
    } else if (seleccionEnCombo2.equals("B")) { 
     combo3.addItem("B-1"); 
     combo3.addItem("B-2"); 
     combo3.addItem("B-3"); 
    } else if (seleccionEnCombo2.equals("C")) { 
     combo3.addItem("C-1"); 
     combo3.addItem("C-2"); 
     combo3.addItem("C-3"); 
    } else if (seleccionEnCombo2.equals("1")) { 
     combo3.addItem("1-a"); 
     combo3.addItem("1-b"); 
     combo3.addItem("1-c"); 
    } else if (seleccionEnCombo2.equals("2")) { 
     combo3.addItem("2-a"); 
     combo3.addItem("2-b"); 
     combo3.addItem("2-c"); 
    } else if (seleccionEnCombo2.equals("3")) { 
     combo3.addItem("3-a"); 
     combo3.addItem("3-b"); 
     combo3.addItem("3-c"); 
    } 
} 
} 

,但我得到下一个异常时,我选择在jcombo1numbers值:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
    at es.mycompany.MyView.ThreeCascadeJComboBox.loadCombo3(ThreeCascadeJComboBox.java:78) 
    at es.mycompany.MyView.ThreeCascadeJComboBox.access$3(ThreeCascadeJComboBox.java:76) 
    at es.mycompany.MyView.ThreeCascadeJComboBox$2.actionPerformed(ThreeCascadeJComboBox.java:40) 
    at javax.swing.JComboBox.fireActionEvent(Unknown Source) 
    at javax.swing.JComboBox.contentsChanged(Unknown Source) 
    at javax.swing.JComboBox.intervalRemoved(Unknown Source) 
    at javax.swing.AbstractListModel.fireIntervalRemoved(Unknown Source) 
    at javax.swing.DefaultComboBoxModel.removeAllElements(Unknown Source) 
    at javax.swing.JComboBox.removeAllItems(Unknown Source) 
    at es.mycompany.MyView.ThreeCascadeJComboBox.loadCombo2(ThreeCascadeJComboBox.java:63) 
    at es.mycompany.MyView.ThreeCascadeJComboBox.access$1(ThreeCascadeJComboBox.java:62) 
    at es.mycompany.MyView.ThreeCascadeJComboBox$1.actionPerformed(ThreeCascadeJComboBox.java:30) 
    at javax.swing.JComboBox.fireActionEvent(Unknown Source) 
    at javax.swing.JComboBox.setSelectedItem(Unknown Source) 
    at javax.swing.JComboBox.setSelectedIndex(Unknown Source) 
    at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(Unknown Source) 
    at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source) 
    at java.awt.Component.processMouseEvent(Unknown Source) 
    at javax.swing.JComponent.processMouseEvent(Unknown Source) 
    at javax.swing.plaf.basic.BasicComboPopup$1.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$400(Unknown Source) 
    at java.awt.EventQueue$2.run(Unknown Source) 
    at java.awt.EventQueue$2.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) 
    at java.security.AccessControlContext$1.doIntersectionPrivilege(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.AccessControlContext$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

这将是一个很容易,如果你格式化堆栈跟踪 – arcy 2013-02-27 19:07:57

回答

2

抛出异常,因为在那一点seleccionEnCombo2null

你可以在combo2ActionListener添加支票null,它会很好地工作:

if (combo2.getSelectedItem() != null) { 
    loadCombo3((String) combo2.getSelectedItem()); 
} 
+0

谢谢丹。 !! – user998871 2013-02-27 19:19:05

+0

不客气。 – 2013-02-27 19:19:23

1

的问题是,ActionListenercombo1被触发ActionEventcombo2,不会有任何选择的项目(因为它是空的)。您可以添加支票:

if (combo2.getSelectedItem() != null) { 
    loadCombo3((String) combo2.getSelectedItem()); 
} 
0

正如其他帖子所述,在某些情况下组合的选定值为空。这是因为你可能没有意识到组合2的ActionListener被调用两次。第一次拨打电话时,拨打removeAllElements。这是空值来自的地方。第二次是你在你的代码中认为是唯一的调用 - 这是对组合框的人口以及用户交互的回应。

0

加载第二个组合框时,会触发该框的动作事件(因为动作已发生[动作不限于选择]。第二个组合框的actionPerformed尝试加载第三个组合框,第二个组合框的选择,并没有任何这是你的空指针,从第二个组合框不存在的选择