how to call combobox selected item from another class in java?

会有一股神秘感。 提交于 2019-12-13 09:18:11

问题


final JComboBox departure = new JComboBox();
departure.setModel(new DefaultComboBoxModel(new String[] {"city1", "city2", "city3"}));
departure.setBounds(413, 11, 147, 20);
int selectedIndex1=departure.getSelectedIndex();
contentPane.add(departure);

I am coding a bus reservation system for my homework, I use JComboBox to choose destination and departure city. I want to call selected item from another class. In this class the user will choose his seat.

How can I call selected item from another class? Please help me.. thanks.


回答1:


You could make your JComboBox a class member variable & add a method to return result of getSelectedItem:

public class MyGuiApp {

    private JComboBox comboBox;

    // constructor, init method, etc.

    public String getSelectedItem() {
       return (String)comboBox.getSelectedItem();
    }
}


来源:https://stackoverflow.com/questions/14085734/how-to-call-combobox-selected-item-from-another-class-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!