java - Passing an integer from user input to another class -
i have problem getting integer passed 1 class properly.
i'm creating gui user can input number text field resize grid in jpanel.
if write normal int below works fine,
public int getgridsize() { homecoming 6; }
but when seek alter code takes string text field (default text in field number "12") follows.
private void initcomponents() { gridpanel1 = new lizasummerschol.gridpanel(); gridsize = new javax.swing.jtextfield(); gridsize.settext("12"); gridsize.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { gridsizeactionperformed(evt); } }); private void gobactionperformed(java.awt.event.actionevent evt) { size = integer.parseint(gridsize.gettext()); gridpanel1.executeusercommands("reset"); gob.settext("s "+size); } public void setgridsize() { this.size = size; } public int getgridsize() { if (this.size > 0) { homecoming this.size; } else { homecoming 10; } }
the grid remains 10x10, regardless of input user, if isn't meeting status of beingness greater 0. grid generated in jpanel called gridpanel. here code think relevant.
package lizasummerschol; import java.awt.color; import java.awt.graphics; import java.awt.image; import java.awt.point; import java.awt.rectangle; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.mouseevent; import java.awt.event.mouselistener; import javax.swing.jpanel; public class gridpanel extends jpanel implements mouselistener, actionlistener { gridjapplet mygridjapplet = new gridjapplet(); public int number_rows; public int number_cols; public static int left = 15; public static int downwards = 15; public static int size = 15; private int particlecolour; private int fixedness; private int iteration; private graphics buffergraphics; private image offscreenimage; private image offscreenimagedrawed; private graphics offscreengraphics; private graphics offscreengraphicsdrawed; private int [][] particle; private int [][] fixed; public gridpanel() { addmouselistener( ); reset(); } //initialise particle random colours private void initialiserandomly() { number_rows = number_cols = mygridjapplet.getgridsize(); particle = new int[number_rows][number_cols]; fixed = new int[number_rows][number_cols]; ( int i=0; < number_rows; i++ ) { ( int j=0; j < number_cols; j++ ) { if( math.random()*3 < 1 ) { particle[i][j] = 0 ; } else if ( math.random()*3 < 2) { particle [i][j] = 1 ; } else { particle[i][j] = 2 ; } fixed[i][j] = 0; } } iteration = 1; }
is string has been parsed integer different integer? thought should take "12" gridsize when applet initialises, , when text in gridsize changed (and "go" button pressed) redraw gridpanel new dimensions.
when asked when parsing taking place, state:
it's called reset() starts when applet starts.
then it's suspected: you're calling gettext()
on jtextfield @ time when gui under construction , way before user has had time interact gui. makes sense text field hold nil of logical value @ time.
the solution i've stated in comment above, parsing in event listener such actionlistener. means parsing take place when user triggers event such pressing jbutton triggers actionlistener or when clicking on jlabel , thereby triggering mouselistener. way can info user when they've indicated they've completed entering it, , calculation needs done.
for example:
import java.awt.event.*; import java.lang.reflect.invocationtargetexception; import javax.swing.*; public class parsetextfieldeg extends japplet { @override public void init() { seek { swingutilities.invokeandwait(new runnable() { public void run() { creategui(); } }); } grab (invocationtargetexception e) { e.printstacktrace(); } grab (interruptedexception e) { e.printstacktrace(); } } private void creategui() { parsetextfieldpanel panel = new parsetextfieldpanel(); panel.reset(); // *** calling reset @ wrong time! getcontentpane().add(panel); } } class parsetextfieldpanel extends jpanel { private jtextfield textfield = new jtextfield(10); private jbutton button = new jbutton("push me"); public parsetextfieldpanel() { add(textfield); add(button); button.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent arg0) { reset(); // *** calling reset @ *right* time! } }); } public void reset() { int myint; seek { myint = integer.parseint(textfield.gettext()); joptionpane.showmessagedialog(this, "myint " + myint); } grab (numberformatexception e) { joptionpane.showmessagedialog(this, "myint not yet available or non-number"); } } }
java string parsing user-interface integer
No comments:
Post a Comment