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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:18:52  来源:igfitidea点击:

Retrieve text from JComboBox

javacomboboxjcombobox

提问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

Simply use :

只需使用:

String value= comboBox.getSelectedItem().toString();

Other examples available here

此处提供的其他示例

回答by Marcelo

You can try the following

您可以尝试以下操作

String typedText = ((JTextField)myComboBox.getEditor().getEditorComponent()).getText();