JComboBox设置标签和值
是否可以为 JComboBox
设置值和标签,以便我可以显示标签但获得不同的值?
例如在 JavaScript 中我可以这样做:
document.getElementById("myselect").options[0].value //accesses value attribute of 1st option
document.getElementById("myselect").options[0].text //accesses text of 1st option
Is it possible to set a value and a label to a JComboBox
so I can show a label but get a value that is different?
For example in JavaScript I can do:
document.getElementById("myselect").options[0].value //accesses value attribute of 1st option
document.getElementById("myselect").options[0].text //accesses text of 1st option
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以将任何对象放入 JComboBox 中。默认情况下,它使用对象的 toString 方法来显示标签,并使用键盘在组合框中导航。因此,最好的方法可能是在组合内定义和使用适当的对象:
You can put any object inside of a JComboBox. By default, it uses the toString method of the object to display a label navigate in the combo box using the keyboard. So, the best way is probably to define and use appropriate objects inside the combo :
这是一个实用程序接口和类,可以轻松获得组合框以使用不同的标签。它不会创建替换的
ListCellRenderer
(并且如果外观发生变化,也会冒着看起来不合适的风险),而是使用默认的ListCellRenderer
(无论是什么) ,但交换您自己的字符串作为标签文本,而不是值对象中toString()
定义的字符串。正如您所看到的,
ToStringListCellRenderer
从ToString
实现中获取自定义字符串,然后将其传递给原始ListCellRenderer
,而不是传递值对象本身。要使用此代码,请执行如下操作:
除了使用此代码制作自定义标签之外,如果您创建一个基于系统区域设置创建字符串的
ToString
实现,您还可以轻松国际化组合框无需更改值对象中的任何内容。Here's a utility interface and class that make it easy to get a combo box to use different labels. Instead of creating a replacement
ListCellRenderer
(and risking it looking out of place if the look-and-feel is changed), this uses the defaultListCellRenderer
(whatever that may be), but swapping in your own strings as label text instead of the ones defined bytoString()
in your value objects.As you can see the
ToStringListCellRenderer
gets a custom string from theToString
implementation, and then passes it to the originalListCellRenderer
instead of passing in the value object itself.To use this code, do something like the following:
As well as using this to make custom labels, if you make a
ToString
implementation that creates strings based on the system Locale, you can easily internationalize the combo box without having to change anything in your value objects.Enum
的实例对此特别方便,如toString()
“返回声明中包含的此枚举常量的名称。”Instances of
Enum
are particularly convenient for this, astoString()
"returns the name of this enum constant, as contained in the declaration."使用 ListCellRenderer 来实现你想要的。创建一个扩展
JLabel
并实现ListCellRenderer
的类。使用setRenderer()
方法将该类设置为JComboBox
中的渲染器。现在,当您从 jcombobox 访问值时,它将是 jlabel 类型。Use ListCellRenderer to achieve what you want. Make a class that extends
JLabel
and implementsListCellRenderer
. Set that class as a renderer in yourJComboBox
usingsetRenderer()
method. Now when you access values from your jcombobox it will be of type jlabel.第 1 步
创建一个类,其中包含
JComboBox
的两个属性,id、name,例如第 2 步
在表单的设计中,右键单击
JComboBox
并选择“属性”,现在打开“代码”选项卡,在属性“类型参数”中写入类的名称,在我们的示例中为“产品”。第 3 步
现在创建一个方法,通过查询连接到数据库以生成产品列表,该方法接收
JComboBox
对象作为参数。第 4 步
您可以从表单调用该方法
现在您可以使用
getItemAt
方法访问选定的 idStep 1
Create a class with the two properties of the
JComboBox
, id, name for exampleStep 2
In the design of the form, right click in the
JComboBox
and select Properties, now open the code tab and in the property Type Parameters write the name of the class, in our example it is Product.Step 3
Now create a method that connects to the database with a query to generate a list of products, this method receives as a parameter a
JComboBox
object.Step 4
You can call the method from the form
Now you can access the selected id using
getItemAt
method