2017-07-25 7 views
0

これは本当に初心者の質問だと思いますが、私のJavaブックや他の場所ではここで答えが見つかりません。JComboBoxからオブジェクトを返します

私はさまざまな種類のワインを登録できるSwingを使用してGUIを構築しようとしています。私はいくつかの文字列と整数(名前、年など)とカントリー、ディストリクトのようなオブジェクトの束で構成するために、私のワインクラス(ワインのスーパークラスと3つのサブクラス:赤、白とバラがあります)家など。

私はJPanelからワインオブジェクトを作成しました。これは名前のためにで構成されています。コンボボックスにはforループを使用してオブジェクトを作成します。配列リスト。

ここは私のローズワインのためのフォームですが、他のものは同じように見えます。

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

ここでここで

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

フォームにユーザーを送信しActionListenerは、私の国のクラスです。

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;   

} 
} 

私の質問は:私は私の国の名前を選択すると、コンボボックスStringStringというだけでなく、オブジェクトを返すにはどのようにすればいいですか?String nameという変数でワインオブジェクトを作成できるように、countryName d Country country

あなたはそこにスウェーデン語があることを理解できますか?

+0

ソースコード内の空白行が空白で1行だけ必要です。 '{'の前または '}'の前の空白行も通常は冗長です。 –

答えて

1

代わりにあなたが今やっているように、単に国の名前を追加するのでは、国オブジェクト自体を追加する必要があります、このようなものは、罰金になります:

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"メソッドをオーバーライドする必要があります。私はそれをうまくやっていると信じて、読みやすくするためにコードを書式設定することをお勧めします。

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

そして、あなたはあなたがしたい機能のために必要となるID、名前やその他のプロパティを取得:

public class Country { 

    private String countryName; 

    //Constructor, getters and setters 

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

} 

そして、あなたは、あなたが選択した国のオブジェクトを取得したいときはいつでも、あなただけのことができます実装する。

希望します。

+0

これはほとんど問題なく動作しますが、ワインオブジェクトを作成すると、国でnull値が取得され、ワインのオブジェクトが作成されますが、国変数はnullです。だから私は私のRoseWineFormから "selectedCountry"を返すか、私がJComboBoxの情報を失った国を置くとどちらかと思います。あなたは正しい方向に私を向けることができますか? – SamSpj

+0

ワインオブジェクトの作成方法とカントリー値の割り当て方法についてコードを投稿できますか?コンポーネントが値を表示しているので、JComboBoxに入れたときに国情報が失われるとは思いませんか?より多くのヘルプを提供するために、UIから値を取得する方法に関するコードを提供してください。 –

+0

RoseWineForm rose = new RoseWineForm(); int answer = JOptionPane.showConfirmDialog(TestVin.this、rose、 "Nyttrosévin"、 JOptionPane.OK_CANCEL_OPTION); (答え= JOptionPane.OK_OPTION!){ \t \t \t \t \t \tリターンであれば、 \t \t} else { RoseWine r =新しいRoseWine(rose.getName()、rose.getCountry()); allWines.add(r); allRoseWines.add(r); System.out.println(r.toString()); } – SamSpj

0

DefaultComboBoxModelクラスを探していると思います。これは、オブジェクトに動的にオブジェクトを割り当てることができるためです。その後、国で@OverridetoString()countryNameを返す関数が必要ですので、DefaultComboBoxModelオブジェクトがコンボボックスにプッシュすると名前が表示されますが、それが理にかなっていればオブジェクトを返します。作成したモデルをJPanelに設定するには、countryBox.setModel(<name of DefaultComboBoxModel>)を使用できます。

関連する問題