为什么JComboBox在构建时需要项目-1?

时间:2013-04-17 14:22:01

标签: java swing custom-controls jcombobox

我试图理解为什么以下代码无效:

           JComboBox status = new javax.swing.JComboBox();
           status.setModel(new DefaultComboBoxModel() {
            @Override
            public void addElement(Object anObject) {
                statusList.add((RequirementStatus) anObject);
                super.addElement(anObject);
            }

            @Override
            public int getSize() {
                return statusList.size();
            }

            @Override
            public Object getElementAt(int index) {
                return index >= 0 && index < statusList.size()
                        ? statusList.get(index) : null;
            }
        });
        status.setRenderer(new ListCellRenderer() {
            @Override
            public Component getListCellRendererComponent(JList list,
                    Object value, int index, boolean isSelected,
                    boolean cellHasFocus) {

                return index >= 0 ? new JLabel(
                        ((RequirementStatus) ((ComboBoxModel) status.getModel())
                        .getElementAt(index)).getStatus())
                        : new JLabel("");
            }
        });

这一个确实:

           JComboBox status = new javax.swing.JComboBox();
           status.setModel(new DefaultComboBoxModel() {
            @Override
            public void addElement(Object anObject) {
                statusList.add((RequirementStatus) anObject);
                super.addElement(anObject);
            }

            @Override
            public int getSize() {
                return statusList.size();
            }

            @Override
            public Object getElementAt(int index) {
                return statusList.get(index);
            }
        });
        status.setRenderer(new ListCellRenderer() {
            @Override
            public Component getListCellRendererComponent(JList list,
                    Object value, int index, boolean isSelected,
                    boolean cellHasFocus) {

                return index >= 0 ? new JLabel(
                        ((RequirementStatus) ((ComboBoxModel) status.getModel())
                        .getElementAt(index)).getStatus())
                        : new JLabel("-none-");
            }
        });

我不得不将这个条件添加到代码中,因为在构建GUI时由于某种原因,代码请求将元素呈现在-1位置。如果我没有返回带有文字的标签,则组合框根本不会显示。

修改

如果此解决方法不存在,这是我得到的错误:

java.lang.ArrayIndexOutOfBoundsException: -1
    at java.util.ArrayList.get(ArrayList.java:324)
    at net.sourceforge.javydreamercsw.client.ui.nodes.actions.EditRequirementDialog$5.getElementAt(EditRequirementDialog.java:119)
    at net.sourceforge.javydreamercsw.client.ui.nodes.actions.EditRequirementDialog$6.getListCellRendererComponent(EditRequirementDialog.java:127)
    at javax.swing.plaf.basic.BasicComboBoxUI.getDisplaySize(BasicComboBoxUI.java:1334)
    at javax.swing.plaf.basic.BasicComboBoxUI.getMinimumSize(BasicComboBoxUI.java:903)
    at javax.swing.plaf.basic.BasicComboBoxUI.getPreferredSize(BasicComboBoxUI.java:892)
    at javax.swing.JComponent.getPreferredSize(JComponent.java:1642)
    at javax.swing.GroupLayout$ComponentSpring.calculateNonlinkedPreferredSize(GroupLayout.java:2944)
    at javax.swing.GroupLayout$ComponentSpring.calculatePreferredSize(GroupLayout.java:2905)
    at javax.swing.GroupLayout$Spring.getPreferredSize(GroupLayout.java:1325)
    at javax.swing.GroupLayout$Group.getSpringSize(GroupLayout.java:1604)
    at javax.swing.GroupLayout$Group.calculateSize(GroupLayout.java:1593)
    at javax.swing.GroupLayout$Group.calculatePreferredSize(GroupLayout.java:1568)
    at javax.swing.GroupLayout$Spring.getPreferredSize(GroupLayout.java:1325)
    at javax.swing.GroupLayout$Group.getSpringSize(GroupLayout.java:1604)
    at javax.swing.GroupLayout$Group.calculateSize(GroupLayout.java:1593)
    at javax.swing.GroupLayout$Group.calculatePreferredSize(GroupLayout.java:1568)
    at javax.swing.GroupLayout$Spring.getPreferredSize(GroupLayout.java:1325)
    at javax.swing.GroupLayout$Group.getSpringSize(GroupLayout.java:1604)
    at javax.swing.GroupLayout$Group.calculateSize(GroupLayout.java:1593)
    at javax.swing.GroupLayout$Group.calculatePreferredSize(GroupLayout.java:1568)
    at javax.swing.GroupLayout$Spring.getPreferredSize(GroupLayout.java:1325)
    at javax.swing.GroupLayout$Group.getSpringSize(GroupLayout.java:1604)
    at javax.swing.GroupLayout$Group.calculateSize(GroupLayout.java:1590)
    at javax.swing.GroupLayout$Group.calculatePreferredSize(GroupLayout.java:1568)
    at javax.swing.GroupLayout$Spring.getPreferredSize(GroupLayout.java:1325)
    at javax.swing.GroupLayout$Group.getSpringSize(GroupLayout.java:1604)
    at javax.swing.GroupLayout$Group.calculateSize(GroupLayout.java:1588)
    at javax.swing.GroupLayout$Group.calculatePreferredSize(GroupLayout.java:1568)
    at javax.swing.GroupLayout$Spring.getPreferredSize(GroupLayout.java:1325)
    at javax.swing.GroupLayout$Group.getSpringSize(GroupLayout.java:1604)
    at javax.swing.GroupLayout$Group.calculateSize(GroupLayout.java:1588)
    at javax.swing.GroupLayout$Group.calculatePreferredSize(GroupLayout.java:1568)
    at javax.swing.GroupLayout$Spring.getPreferredSize(GroupLayout.java:1325)
    at javax.swing.GroupLayout$SequentialGroup.setValidSize(GroupLayout.java:1972)
    at javax.swing.GroupLayout$Group.setSize(GroupLayout.java:1553)
    at javax.swing.GroupLayout.calculateAutopadding(GroupLayout.java:1058)
    at javax.swing.GroupLayout.layoutContainer(GroupLayout.java:897)
    at java.awt.Container.layout(Container.java:1419)
    at java.awt.Container.doLayout(Container.java:1408)
    at java.awt.Container.validateTree(Container.java:1505)
    at java.awt.Container.validateTree(Container.java:1511)
    at java.awt.Container.validateTree(Container.java:1511)
    at java.awt.Container.validateTree(Container.java:1511)
    at java.awt.Container.validate(Container.java:1478)
    at java.awt.Dialog.conditionalShow(Dialog.java:890)
    at java.awt.Dialog.show(Dialog.java:1036)
    at java.awt.Component.show(Component.java:1591)
    at java.awt.Component.setVisible(Component.java:1543)
    at java.awt.Window.setVisible(Window.java:843)
    at java.awt.Dialog.setVisible(Dialog.java:987)
    at net.sourceforge.javydreamercsw.client.ui.nodes.actions.EditRequirementAction$1.run(EditRequirementAction.java:33)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:666)
    at java.awt.EventQueue.access$400(EventQueue.java:81)
    at java.awt.EventQueue$2.run(EventQueue.java:627)
    at java.awt.EventQueue$2.run(EventQueue.java:625)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:636)
    at org.netbeans.core.TimableEventQueue.dispatchEvent(TimableEventQueue.java:159)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
[catch] at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

我知道这应该是一件容易的事情,比如要设置的旗帜,但我无法找到它。

有什么想法吗?

解决方案: 删除模型覆盖并更改渲染器代码,如下所示:

status.setRenderer(new ListCellRenderer() {
            @Override
            public Component getListCellRendererComponent(JList list,
                    Object value, int index, boolean isSelected,
                    boolean cellHasFocus) {
                return index >= 0 ? new JLabel(
                        ((RequirementStatus) value).getStatus())
                        : new JLabel(((RequirementStatus) list.getSelectedValue()).getStatus());
            }
        });

1 个答案:

答案 0 :(得分:1)

来自JComboBox#setRenderer javadoc:

  

要显示所选项目,将调用aRenderer.getListCellRendererComponent,传递列表对象并将索引设置为-1。