Java 从 JComboBox 检索文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3496532/
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
Retrieve text from JComboBox
提问by Reddy
How to retrieve Text that is typed in JComboBox. This text need not be an existing item.
如何检索在 JComboBox 中键入的文本。此文本不必是现有项目。
采纳答案by Carlos
You can get the selected or typed value from a JComboBox by calling method getSelectedItem
. If it is not an existing item, then you'll get a String object. Otherwise you'll get whatever object you populated the combo box with.
您可以通过调用方法从 JComboBox 获取选定或键入的值getSelectedItem
。如果它不是现有项目,那么您将获得一个 String 对象。否则,您将获得填充组合框的任何对象。
回答by Mehdi
回答by Marcelo
You can try the following
您可以尝试以下操作
String typedText = ((JTextField)myComboBox.getEditor().getEditorComponent()).getText();