-1
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.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
gzml
  • 1
  • 2
  • 4
  • 2
    Make your `JComboBox` a class member variable & add a method to return result of `getSelectedItem`. – Reimeus Dec 29 '12 at 20:46
  • 1
    can u explain more..how can i make JComboBox class member?like that,(public JComboBox departure)?? – gzml Dec 29 '12 at 21:03

1 Answers1

6

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();
    }
}
Reimeus
  • 158,255
  • 15
  • 216
  • 276