简体   繁体   English

JComboBox转换为字符串

[英]JComboBox to string

I have a String array of names, and then I added it into an editable JComboBox. 我有一个名称数组,然后将其添加到可编辑的JComboBox中。

The user can either pick his/her name from the choices or just input his/her name if not in the choices. 用户可以从选项中选择他/她的名字,或者如果不在选项中,则仅输入他/她的名字。

How do I put the user input into a new string variable? 如何将用户输入放入新的字符串变量中?

   String [] chooseName = { Mark, John, Allison, Jessica };
   JComboBox combo = new JComboBox (chooseName);
   combo.setEditable(true);

   String chosenName =  /*  how do i place what the user inputed here? */

I would add an action listener to the combo box. 我将动作侦听器添加到组合框。 By doing it in the same block there, it will simply grab the default (I believe). 通过在同一块中执行此操作,它将简单地获取默认值(我相信)。 This way we make sure we grab the value once selected. 这样,我们确保一旦选择就可以获取值。

String [] chooseName = { Mark, John, Allison, Jessica };
final JComboBox combo = new JComboBox (chooseName);
combo.setEditable(true);
combo.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        String chosenName =  (String) combo.getSelectedItem();
    }
});

您可以使用:

combo.getSelectedItem()

This will Help You.. 这将为您提供帮助。

combo.getSelectedItem();

For More reference follow this link, it will help you.. 有关更多参考,请单击此链接,它将为您提供帮助。

http://www.roseindia.net/java/example/java/swing/ComboBox.shtml http://www.roseindia.net/java/example/java/swing/ComboBox.shtml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM