无法从jcombobox中删除第一个元素

时间:2011-08-24 12:27:29

标签: java swing jcombobox

我无法从jcombobox中删除第一个元素。我的代码如下,

JComboBox cBox= cBox= new JComboBox();
...    
while (cBox.getItemCount() > 0)
  cBox.removeItemAt(0);

对于测试运行,我在cBox中有3个项目。当它到达removeItemAt(0)时,调试会进入一些绝对不相关的文件访问代码。这两次是否会得到以下异常。我尝试了removeAllItems(),它直接获得相同的异常。但是,removeItem(1)可以正常工作,直到只剩下1个元素。该异常不会使应用程序崩溃,我可以看到组合框中没有任何项目,所以它工作了一点。我究竟做错了什么。

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at util.Gui$4.actionPerformed(Gui.java:111)
at javax.swing.JComboBox.fireActionEvent(Unknown Source)
at javax.swing.JComboBox.contentsChanged(Unknown Source)
at javax.swing.AbstractListModel.fireContentsChanged(Unknown Source)
at javax.swing.DefaultComboBoxModel.setSelectedItem(Unknown Source)
at javax.swing.DefaultComboBoxModel.removeElementAt(Unknown Source)
at javax.swing.JComboBox.removeItemAt(Unknown Source)
at util.Gui.prepareSubLists(Gui.java:164)
at util.Gui$3.actionPerformed(Gui.java:97)
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.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(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)

2 个答案:

答案 0 :(得分:1)

你的条件陈述不对吗?将while替换为if,因此

if(cBox.getItemCount() > 0){
  cBox.removeItemAt(0);
}

这是SSCCE

public final class JComboBoxDemo {
    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                createAndShowGUI();             
            }
        });
    }

    public static void createAndShowGUI(){
        final JFrame frame = new JFrame("JComboBox Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new FlowLayout());
        frame.getContentPane().add(JComboPane.newInstance());
        frame.setSize(new Dimension(250, 100)); // for demonstration purposes only
        //frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private static final class JComboPane extends JPanel{
        private JComboPane(){
            super();
            setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
            JCenteredComboBox comboBox = JCenteredComboBox.newInstance();
            JCenteredButton button = JCenteredButton.newInstance(comboBox);
            add(comboBox);
            add(button);
        }

        public static final JComboPane newInstance(){
            return new JComboPane();
        }

        private static final class JCenteredComboBox extends JComboBox{
            private JCenteredComboBox(){
                super(new String[]{"Item 1", "Item 2", "Item 3"});
                setAlignmentX(Component.CENTER_ALIGNMENT);
            }

            public static final JCenteredComboBox newInstance(){
                return new JCenteredComboBox();
            }
        }

        private static final class JCenteredButton extends JButton{
            private JCenteredButton(final JComboBox comboBox){
                super("Remove First Item");
                setAlignmentX(Component.CENTER_ALIGNMENT);
                addActionListener(new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if(comboBox.getItemCount() > 0){
                            comboBox.removeItemAt(0); // your logic
                        }
                    }
                });
            }

            public static final JCenteredButton newInstance(final JComboBox comboBox){
                return new JCenteredButton(comboBox);
            }
        }
    }
}

enter image description here

运行此操作时,按JButton将删除JComboBox中的第一项。你可以按住它直到它为空。

答案 1 :(得分:0)

可能会发生此异常,因为在删除组合项时会触发事件,而在此事件处理方法中,您仍然会引用组合框项目。

例如,当你在代码中的某个地方(除了在actionPeformed()中)删除combo.removeItem(0)或removeAllItems()的组合框中的最后一项时,仍然会触发/执行事件actionPerformed。但是,actionPerformed()方法通常包含对用户操作做出反应的代码(用户在组合框上的某处单击)。因此,当最后一项被删除时,组合框中没有其他项目,并且对actionPerformed()中的项目或索引的任何引用都将导致异常。

解决方法是将代码从actionPerformed移动到例如mouseClicked()或其他事件处理程序取决于你想要做什么。

相关问题