java - Return object from JComboBox -
i guess real newbie question cant find answers here, in java-book or elsewhere.
i'm trying build gui using swing can register different kinds of wine. want wine-class (there wine super class , 3 sub classes: red, white , rose) consist of both strings , integers (name, year etc.) , bunch of objects country, district, house , more.
i create wine object jpanel consisting of jtextarea
name , jcombobox
country, populate combo box using loop collects name variable object country that's stored in arraylist.
here form rose wine, others 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 actionlistener
sends user 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 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 set name."); } else { countryname = newcountryname; }} public string tostring() { return countryname; } }
my question is: when select country name in combo box how return object , not string
called countryname
can create wine object variables string name
, country country
?
hope can have understanding there swedish in there.
instead of adding country name doing now, need add country object itself, 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 trick countrybox.additem(c); } add(line2); }
then in class "country" need override "tostring" method, believe doing fine, idea format code make more readable.
public class country { private string countryname; //constructor, getters , setters @override public string tostring() { return this.countryname; } }
and whenever want country object have selected, can just:
country selectedcountry = (country) countrybox.getselecteditem();
and id, name or other property need functionality wish implement.
hope helps.
Comments
Post a Comment