繁体   English   中英

从JComboBox返回对象

[英]Return object from JComboBox

我想这是一个真正的新手问题,但我在Java书中或其他地方找不到任何答案。

我正在尝试使用Swing构建GUI,可以在其中注册不同种类的葡萄酒。 我希望我的葡萄酒类(将有一个葡萄酒超类和三个子类:红色,白色和玫瑰)由一些字符串和整数(名称,年份等)以及一堆对象组成,例如Country,District,房子等等。

我从一个JPanel创建了wine对象,该面板现在由名称的JTextArea和国家的JComboBox ,并使用for循环填充组合框,该循环从存储在arraylist中的对象国家/地区收集名称变量。

这是我的玫瑰酒形式,其他形式差不多。

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 ;
  }}

这是将用户发送到表单的ActionListener

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!");
        }
    }

这是我的国家/地区课程:

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;         

}
}

我的问题是:当我在组合框中选择国家名称时,如何返回对象,而不仅仅是返回名为countryNameString以便可以使用变量String nameCountry country创建我的酒对象?

希望您能了解那里有一些瑞典语。

与其像现在那样仅添加国家名称,还需要添加国家对象本身,如下所示就可以了:

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);
  }

然后,在您的“国家/地区”类中,您将需要覆盖“ toString”方法,我相信您做得很好,将代码格式化以使其更具可读性是一个好主意。

public class Country {

    private String countryName;

    //Constructor, getters and setters

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

}

每当您要获取所选的国家对象时,都可以:

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

并获取您要实现的功能所需的ID,名称或任何其他属性。

希望能帮助到你。

我相信您正在寻找DefaultComboBoxModel类,因为它允许您动态地将对象分配给它。 然后,在国家需要@OverridetoString()函数返回countryName ,这样的方式,当DefaultComboBoxModel对象推到组合框就会显示出名字,但如果是有道理的,将返回的对象。 为了将您创建的模型设置为JPanel,可以使用countryBox.setModel(<name of DefaultComboBoxModel>)

暂无
暂无

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

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