简体   繁体   中英

Return object from JComboBox

I guess this is a real newbie question but I cant find any answers here, in my java-book or elsewhere.

I'm trying to build a GUI using Swing where I can register different kinds of wine. I want my wine-class (there will be a wine super class and three sub classes: red, white and rose) to consist of both some strings and integers (name, year etc.) and a bunch of objects like Country, District, House and more.

I create the wine object from a JPanel now consisting of a JTextArea for the name and a JComboBox for the country, I populate my combo box by using a for loop that collects the name variable from the object country that's stored in a arraylist.

Here is my form for rose wine, the others look about the same.

class RoseWineForm extends JPanel {

   private JTextField wineName = new JTextField(15);
   private JComboBox countryBox = new JComboBox();

   public RoseWineForm() {
       JPanel line1 = new JPanel();
       setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
       line1.add(new JLabel("Namn: "));
       line1.add(wineName);
       add(line1);

       JPanel line2 = new JPanel();
       line2.add(new JLabel("Ursprungsland"));
       line2.add(countryBox);
       for(Country c : listOfCountries) {
       countryBox.addItem(c.getCountryName());
       }
       add(line2);
  }

  public String getName() {
      return wineName.getText();
  }

  public Country getCountry() {
      return ;
  }}

Here is the ActionListener that sends the user to the forms

class NewWineListener implements ActionListener {
 public void actionPerformed (ActionEvent a) {
     try {
         JComboBox wineColor = (JComboBox) a.getSource();
         if (wineColor.getSelectedIndex() == 0) {
             RedWineForm red = new RedWineForm();
             int answer = JOptionPane.showConfirmDialog(TestVin.this, red, "Nytt rött vin",
                JOptionPane.OK_CANCEL_OPTION);
         } else if (wineColor.getSelectedIndex() == 1) {
             WhiteWineForm white = new WhiteWineForm();
             int answer = JOptionPane.showConfirmDialog(TestVin.this, white, "Nytt vitt vin",
                JOptionPane.OK_CANCEL_OPTION);
         } else {
             RoseWineForm rose = new RoseWineForm();
             int answer = JOptionPane.showConfirmDialog(TestVin.this, rose, "Nytt rosé vin",
                JOptionPane.OK_CANCEL_OPTION);
         }
     } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(TestVin.this, "Fel inmatning!");
        }
    }

Here is my country class:

public class Country {

private String countryName;

public Country(String countryName) {
    this.countryName = countryName;
}

public String getCountryName() {
    return countryName;
}

public void setCountryName(String newCountryName) {
    if ( newCountryName == null || newCountryName.trim().isEmpty()) {
       System.out.println("You have to set a name.");       
    }  else {
    countryName = newCountryName;
    }} 
    public String toString() {
    return countryName;         

}
}

My question is: When I select the country name in my combo box how do I return the object and not just the String called countryName so that I can create my wine object with the variables String name and Country country ?

Hope you can have understanding that there is some swedish in there.

Instead of adding just the country name like you are doing now, you will need to add the country object itself, something like this will be fine:

public RoseWineForm() {
       JPanel line1 = new JPanel();
       setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
       line1.add(new JLabel("Namn: "));
       line1.add(wineName);
       add(line1);

       JPanel line2 = new JPanel();
       line2.add(new JLabel("Ursprungsland"));
       line2.add(countryBox);
       for(Country c : listOfCountries) {
           //This does the trick
           countryBox.addItem(c);
       }
       add(line2);
  }

Then in your class "Country" you will need to override the "toString" method, I believe you are doing it fine, would be a good idea to format your code to make it more readable.

public class Country {

    private String countryName;

    //Constructor, getters and setters

    @Override
    public String toString() {
        return this.countryName;
    }

}

And whenever you want to get the country object you have selected, you can just:

Country selectedCountry = (Country) countryBox.getSelectedItem();

And get the ID, name or any other property you would need for the functionality you wish to implement.

Hope it helps.

I believe you are looking for DefaultComboBoxModel class since this allows you to dynamically allocate the objects to it. Then in country you need to @Override the toString() function to return countryName so that way when the DefaultComboBoxModel object pushes to the combobox it will show the name but it will return object if that makes sense. which in order to set the model you created to the JPanel you can use countryBox.setModel(<name of DefaultComboBoxModel>) .

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