java - Setting the text of an Array of TextFields using an ActionListener -
i'm trying set text of array of textfields using actionlistener. have main class set , actionlistener class setup. believe problem setting actionlistener multiple jtextfields generated loop in main class i'm not sure. here main class: package lab5.pkg2;
import java.awt.borderlayout; import java.awt.panel; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jtextfield; public class lab52 { public static void main(string[] args) { jframe frame = new jframe(); panel npanel = new panel(); frame.add(npanel, borderlayout.north); panel spanel = new panel(); frame.add(spanel, borderlayout.south); jtextfield[] tf = new jtextfield[10]; for(int k = 0; k < tf.length; k++) { tf[k] = new jtextfield(4); npanel.add(tf[k]); } randlistener listen = new randlistener(tf); jbutton brand = new jbutton("randomize"); brand.setactioncommand("brand"); brand.addactionlistener(listen); spanel.add(brand); jbutton bmaxmin = new jbutton("max, min"); brand.setactioncommand("bmaxmin"); brand.addactionlistener(listen); spanel.add(bmaxmin); frame.pack(); frame.setvisible(true); frame.setdefaultcloseoperation(jframe.exit_on_close); } }
and here actionlistener class:
class randlistener implements actionlistener { private final jtextfield[] tf = new jtextfield[10]; public randlistener(jtextfield[] tf) { tf[0] = tf[0]; tf[1] = tf[1]; tf[2] = tf[2]; tf[3] = tf[3]; tf[4] = tf[4]; tf[5] = tf[5]; tf[6] = tf[6]; tf[7] = tf[7]; tf[8] = tf[8]; tf[9] = tf[9]; } @override public void actionperformed(actionevent e) { if(e.getactioncommand().equals("brand")) { for(int k = 0; k < 10; k++) { random rnum = new random(); tf[k].settext(rnum.tostring()); } } if(e.getactioncommand().equals("bmaxmin")) { } else { joptionpane.showmessagedialog(null, "error"); } } }
the code seems working ;) i've wrote comments in code things i've changed.
import java.awt.borderlayout; import java.awt.panel; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jtextfield; public class lab52 { public static void main(string[] args) { jframe frame = new jframe(); panel npanel = new panel(); frame.add(npanel, borderlayout.north); panel spanel = new panel(); frame.add(spanel, borderlayout.south); jtextfield[] tf = new jtextfield[10]; for(int k = 0; k < tf.length; k++) { tf[k] = new jtextfield(4); npanel.add(tf[k]); } randlistener listen = new randlistener(tf); jbutton brand = new jbutton("randomize"); brand.setactioncommand("brand"); brand.addactionlistener(listen); spanel.add(brand); jbutton bmaxmin = new jbutton("max, min"); bmaxmin.setactioncommand("bmaxmin"); // these 2 lines refering brand, not bmaxmin bmaxmin.addactionlistener(listen); spanel.add(bmaxmin); frame.pack(); frame.setvisible(true); frame.setdefaultcloseoperation(jframe.exit_on_close); } }
your listener class simplifications - in constructor - in if of brand i'm generating random integer, i'm not sure if goal.
import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.util.random; import javax.swing.joptionpane; import javax.swing.jtextfield; class randlistener implements actionlistener { private final jtextfield[] tf; public randlistener(jtextfield[] tf) { this.tf = tf; // simplified code here } @override public void actionperformed(actionevent e) { if(e.getactioncommand().equals("brand")) { for(int k = 0; k < tf.length; k++) { random rnum = new random(); tf[k].settext(rnum.nextint() + ""); // not sure if wanted random number.. } } if(e.getactioncommand().equals("bmaxmin")) { } else { joptionpane.showmessagedialog(null, "error"); } } }
Comments
Post a Comment