java - Using an array to store values from a GUI -
i having problem controlgui class. want create array store integer values of data entered on gui jtextfield , return input use in a different class. code is:
public class controlgui extends jframe{ private temperaturecontrolagent myagent;*emphasized text* public static jtextfield mouldtempfield, melttempfield; public controlgui() { } public static int[] getarray(){ int input[] = new int [2]; jtextfield t[]=new jtextfield [10]; //public textfieldarray jpanel panel=new jpanel(new gridlayout(5,2)); for(int i=0;i<10;i++) { t[i]= new jtextfield(10); panel.add(t[i]); input[0] = integer.parseint(mouldtempfield.gettext()); input[1] = integer.parseint(melttempfield.gettext()); system.out.println("input mouldtempfield"+ input[0]); return input;} controlgui (temperaturecontrolagent a) { super (a.getlocalname()); myagent = a; jpanel p = new jpanel(); p.setlayout(new gridlayout (3, 3)); p.add(new jlabel ("mould temperature set value: ")); mouldtempfield = new jtextfield (10); p.add(mouldtempfield); p.add(new jlabel ("melt temperature: ")); melttempfield = new jtextfield (10); p.add(melttempfield); getcontentpane().add(p, borderlayout.center); jbutton addbutton = new jbutton ("enter"); addbutton.addactionlistener (new actionlistener() { @override public void actionperformed(actionevent ev) { try { string mouldtemp = mouldtempfield.gettext().trim(); string melttemp = melttempfield.gettext().trim(); myagent.updatecatalogue(integer.parseint(mouldtemp), integer.parseint(melttemp)); mouldtempfield.settext(""); melttempfield.settext(""); } catch (exception e) { joptionpane.showmessagedialog(controlgui.this, "invalid values. "+e.getmessage(), "error", joptionpane.error_message); } } }); p = new jpanel(); p.add(addbutton); getcontentpane().add(p, borderlayout.south); //ensure termination of agent when user closes gui //using "close" button addwindowlistener(new windowadapter() { @override public void windowclosing(windowevent e) { myagent.dodelete(); } }); setresizable(false); } public void showgui(){ pack (); dimension screensize; screensize = toolkit.getdefaulttoolkit().getscreensize(); int centerx = (int)screensize.getwidth() / 2; int centery = (int) screensize.getheight() / 2; setlocation(centerx - getwidth() / 2, centery - getheight() / 2 ); super.setvisible(true); } }
i getting java.lang.numberformatexception error line 39 , 40 on code, when try extract integer value of inputed on jtextfield. stuck , not know do. in advance
this error occurred because 1 of jtextfield
has bot int value, need catch exception like:
try { input[0] = integer.parseint(mouldtempfield.gettext().trim());//use trim remove white spaces. } catch(numberformatexception e) { //handle exception }
Comments
Post a Comment