gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 14:40:01 26 4
gpt4 key购买 nike

我试图理解为什么以下代码不起作用:

           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());
}
});

最佳答案

来自 JComboBox#setRenderer javadoc:

To display the selected item, aRenderer.getListCellRendererComponent is called, passing the list object and an index of -1.

关于java - 为什么 JComboBox 在构建时需要项目 -1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16062608/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com