java 从 JComboBox 获取输入值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10072335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get input values from JComboBox
提问by special
How can I get the input for an editable JComboBox
. When user gives an input to the combo how I can get the input text from it?
如何获取可编辑JComboBox
. 当用户向组合提供输入时,我如何从中获取输入文本?
回答by Howard
You need to get the edited text from the combobox editor via combo.getEditor().getItem()
.
回答by Federico Vera
If you need the text that is selected on a JComboBox
and you are sure it's a String
and not any other object, just use something like String text = (String)myCombobox.getSelectedItem()
.
If the thing you have in your Model
is other than a String
, then you need to cast it to the appropriate class, and then use the toString()
method of that object.
If you need more help, you should paste a bit of your code, at least declaration and inicialization of your JComboBox
...
如果您需要在 a 上选择的文本JComboBox
并且您确定它是 aString
而不是任何其他对象,只需使用类似String text = (String)myCombobox.getSelectedItem()
. 如果你的东西Model
不是 a String
,那么你需要将它转换为适当的类,然后使用该toString()
对象的方法。如果您需要更多帮助,您应该粘贴一些代码,至少是声明和初始化JComboBox
...
回答by zip
Just have a look at the oracle tutorial. They do explain how to handle the common swing components http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html
看看oracle教程就知道了。他们确实解释了如何处理常见的 Swing 组件http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html