T_B*_*own 2 java user-interface combobox netbeans
基本上我一直在尝试在netbeans中制作的GUI中使用drop-box.我知道使用.getText()方法从文本字段获取字符串变量是多么容易,但是我希望得到一个用户从下拉菜单中选择的值(即组合框),命中一个'submit'按钮(actionPerformed方法附加到该按钮)并将其放在字符串变量中.
这将允许我使用此字符串变量传递到另一个类中的set()方法.
这是一个令人烦恼的小问题,但我预计当我开始时会有一些问题.
任何帮助赞赏.
尝试
jComboBox.getSelectedItem()
Run Code Online (Sandbox Code Playgroud)
也许以下剪切可能对获取字符串很有用.
Object selectedItem = comboBox.getSelectedItem();
if (selectedItem != null)
{
String selectedItemStr = selectedItem.toString();
Foo(selectedItemStr); // Some method that takes a string parameter.
}
Run Code Online (Sandbox Code Playgroud)
获取所选项目.