java beginner if/else if problem -


something seems going wrong block of code tries set string variable, no matter when run program, dialog box shows otto. know i'm doing wrong here?

thanks, ravin

import java.awt.flowlayout; import java.awt.event.actionlistener; import java.awt.event.actionevent; import javax.swing.joptionpane; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpasswordfield; import javax.swing.jtextfield;  public class smalltingz extends jframe {     private jlabel item1;     private jtextfield tf;     private jtextfield tf2;     private jtextfield tf3;     private jpasswordfield pf;      public smalltingz() {         super("the title");         setlayout(new flowlayout());         jtextfield tf = new jtextfield("cool beans");         jtextfield tf2 = new jtextfield("uncool beans");         jtextfield tf3 = new jtextfield("hot beans");         jpasswordfield pf = new jpasswordfield("password");          add(tf);         add(tf2);         add(tf3);         add(pf);          thehandler handler = new thehandler();         tf.addactionlistener(handler);         tf2.addactionlistener(handler);         tf3.addactionlistener(handler);         pf.addactionlistener(handler);     }      private class thehandler implements actionlistener {          public void actionperformed(actionevent event) {             string string;                       if (event.getsource() == tf)                 string=string.format("field1: %s", event.getactioncommand());             else if (event.getsource() == tf2)                 string=string.format("field2: %s", event.getactioncommand());             else if (event.getsource() == tf3)                 string=string.format("field3: %s", event.getactioncommand());             else if (event.getsource() == pf)                 string=string.format("passfield: %s", event.getactioncommand());             else                 string="otto";              joptionpane.showmessagedialog(null, string);                 }     } } 

in smalltingz() constructor, remove variable declarations. declarations hiding member variables.

change

jtextfield tf = new jtextfield("cool beans"); jtextfield tf2 = new jtextfield("uncool beans"); jtextfield tf3 = new jtextfield("hot beans"); jpasswordfield pf = new jpasswordfield("password"); 

to

tf = new jtextfield("cool beans"); tf2 = new jtextfield("uncool beans"); tf3 = new jtextfield("hot beans"); pf = new jpasswordfield("password"); 

Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -