java - Getting values from JOptionPane and using them to create a new instance of a class -
i want retrieve user values joptionpane, want use values parameters creation of new instance of class. code:
protected void addcar() { string[] size = {"large","small"}; string[] value = {"valuable","not valuable"}; jtextfield regnum = new jtextfield(); jlist carsize = new jlist(size); jlist carvalue = new jlist(value); carvalue.getselectedvalue(); system.out.println(carsize.getselectedvalue()); object[] fields = { "registration number", regnum, "car size", carsize, "car value", carvalue }; joptionpane.showoptiondialog(rootpane, fields, "wish continue?", joptionpane.default_option, joptionpane.yes_no_option, null, null, regnum); }//end addcar
you can user input components passed showoptiondialog()
after has returned.
one thing note: 4th parameter of showoptiondialog()
option type should yes_no_option
, not default_option
. , 5th parameter message type can pass information_message
example. other parameters not relevant in case.
see example:
// code here create components joptionpane.showoptiondialog(null, fields, "wish continue?", joptionpane.yes_no_option, joptionpane.information_message, null, null, null); system.out.println(fields[0] + ": " + regnum.gettext()); system.out.println(fields[2] + ": " + carsize.getselectedvalue()); system.out.println(fields[4] + ": " + carvalue.getselectedvalue());
output:
registration number: 1234 car size: large car value: not valuable
if want use these values create instance of class, can pass them this:
otherclass oc = new otherclass(regnum.gettext(), carsize.getselectedvalue(), carvalue.getselectedvalue());
this assumed otherclass
has constructor this:
public otherclass(string regnum, string carsize, string carvalue) { }
Comments
Post a Comment