java - how can I convert an Object to a string? -
i want take list user's choice , convert string jbox. how can convert contents string can use it?
public class graph extends jframe { private string temp; public graph() { } public void createbox(string[] a) { jframe frame = new jframe(); frame.setdefaultcloseoperation(jframe.exit_on_close); jbutton jbutton1 = new jbutton("ok"); final jlist jlist1 = new jlist(a); jbutton1.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(actionevent e) { object contents = jlist1.getselectedvalue(); //system.out.println(contents); setchoise((string)contents);//how can convert string ? } }); jbutton jbutton2 = new jbutton("close"); jbutton2.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { system.exit(0); } }); frame.add(jlist1, "center"); frame.add(jbutton1,"south"); frame.add(jbutton2,"north"); frame.setsize(300, 200); frame.setvisible(true); } public void setchoise(string temp) { this.temp=temp; } public string getchoise() { return this.temp; } }
i going different , explain solution not 1 proposed of converting object
string
. rather declare jlist
proper use (which seems hold strings
). declare list
final jlist<string> jlist1 = new jlist<string>(a);
and getselectedvalue()
method return string
not raw object
allowing use in methods take in string
.
however people have noted, not work if need manipulate list objects
, if using list strings
Comments
Post a Comment