java - JOptionPane / Input Dialog -
i try input user :
int mynumber = integer.parseint((string) joptionpane.showinputdialog( frame, "number want:\n", "enter number", joptionpane.plain_message, null, null, "5"));
it works i'm not sure user enter number in field , don't want numberformatexception
thrown.
is there way set formatter
joptionpane
(like can text field jformattedtextfield
setting decimalformat
) ?
short answer: no, there no way it. joptionpane doesn't provide means validate input before closes.
one easy way around use jspinner. joptionpane allows components , arrays used message objects, can this:
int min = 1; int max = 10; int initial = 5; jspinner inputfield = new jspinner(new spinnernumbermodel(initial, min, max, 1)); int response = joptionpane.showoptiondialog(frame, new object[] { "number want:\n", inputfield }, "enter number", joptionpane.ok_cancel_option, joptionpane.plain_message, null, null, null); if (response == joptionpane.ok_option) { int mynumber = (integer) inputfield.getvalue(); // stuff mynumber here } else { system.out.println("user canceled dialog."); }
you can also, suggested, pass jformattedtextfield message object instead of jspinner.
Comments
Post a Comment