简体   繁体   中英

How can I store inputs given by the user from GUI to a string array in java? I am using java swing to create the GUI

i have created a drop down menu and i want to store which option the user is choosing from the menu to an array of strings so that i can use the string array later. How can I do it?

您可以使用ArrayList并使用方法getSelectedItem()存储选择。

Whenever possible try to use Collection framework in java, it gives more flexibility.

Example:

ArrayList<String> arr = new ArrayList<String>();
JComboBox jb = new JComboBox();
jb.add("Vivek");
jb.add("Vishal");



jb.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {

        arr.add(jb.getSelectedItem().toString());
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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