JComboBox的getSelectedItem()有什么意义?

时间:2023-01-28 07:30:42

I just read the JavaDoc for JComboBox (I promise I have a life... I wasn't reading through for fun. =P), and I think the problems I'm having with my program can be attributed to the getSelectedItem() method. The documentation says:

我刚刚阅读了JavaDoc for JComboBox(我保证我有生命...我不是为了好玩而读的。= P),我认为我对程序的问题可以归结为getSelectedItem()方法。文件说:

Returns the current selected item.

返回当前选定的项目。

If the combo box is editable, then this value may not have been added to the combo box with addItem, insertItemAt or the data constructors.

如果组合框是可编辑的,则此值可能尚未添加到具有addItem,insertItemAt或数据构造函数的组合框中。

If you can't get values that were added with those methods or with the constructor, of what use is the method? And how can I get the value from an "editable" JComboBox?

如果您无法获取使用这些方法或构造函数添加的值,该方法的用途是什么?如何从“可编辑”的JComboBox中获取值?

Link to JavaDoc: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JComboBox.html#getSelectedItem()

链接到JavaDoc:http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JComboBox.html#getSelectedItem()

4 个解决方案

#1


2  

If you can't get values that were added with those methods or with the constructor, of what use is the method? And how can I get the value from an "editable" JComboBox?

如果您无法获取使用这些方法或构造函数添加的值,该方法的用途是什么?如何从“可编辑”的JComboBox中获取值?

That's not what the docs say. The docs say the selected item might not have been added with add/insertItem , which might very well be the case if the user edited/typed in the value himself.

这不是文档所说的。文档说可能没有使用add / insertItem添加所选项目,如果用户自己编辑/输入值,则可能就是这种情况。

In any case, getSelectedItem() gives you whatever is selected in the combobox, wether it was one of the values you filled in, or one the user typed.

在任何情况下,getSelectedItem()都会为您提供在组合框中选择的任何内容,它是您填写的值之一,或者是用户键入的值之一。

#2


2  

the extra value added by the user in the JComboxBox will not be added in the ComboBoxModel but will be available as a java.lang.String via getSelectedItem()

用户在JComboxBox中添加的额外值不会添加到ComboBoxModel中,而是通过getSelectedItem()以java.lang.String的形式提供

#3


0  

final Object object = jComboBox.getEditor().getItem();
if (object instanceof String) {
    final String string = (String)object;
}

#4


0  

Useful for cases when you allow certain input outside the supplied range of values in the combobox model.

适用于在组合框模型中允许某些输入超出所提供的值范围的情况。

#1


2  

If you can't get values that were added with those methods or with the constructor, of what use is the method? And how can I get the value from an "editable" JComboBox?

如果您无法获取使用这些方法或构造函数添加的值,该方法的用途是什么?如何从“可编辑”的JComboBox中获取值?

That's not what the docs say. The docs say the selected item might not have been added with add/insertItem , which might very well be the case if the user edited/typed in the value himself.

这不是文档所说的。文档说可能没有使用add / insertItem添加所选项目,如果用户自己编辑/输入值,则可能就是这种情况。

In any case, getSelectedItem() gives you whatever is selected in the combobox, wether it was one of the values you filled in, or one the user typed.

在任何情况下,getSelectedItem()都会为您提供在组合框中选择的任何内容,它是您填写的值之一,或者是用户键入的值之一。

#2


2  

the extra value added by the user in the JComboxBox will not be added in the ComboBoxModel but will be available as a java.lang.String via getSelectedItem()

用户在JComboxBox中添加的额外值不会添加到ComboBoxModel中,而是通过getSelectedItem()以java.lang.String的形式提供

#3


0  

final Object object = jComboBox.getEditor().getItem();
if (object instanceof String) {
    final String string = (String)object;
}

#4


0  

Useful for cases when you allow certain input outside the supplied range of values in the combobox model.

适用于在组合框模型中允许某些输入超出所提供的值范围的情况。