java - Why i can't add actionListener written in other file -


i have actionlistener in other file & want use actionlistener in gui code

package gui;  public class checkbuttongui {      private jpanel mainpanel;     private jbutton one, two, three, four;     jlabel buttonstatus;      public jpanel getgui()     {         checkbuttongui cbg = new checkbuttongui();         mainpanel = new jpanel(new gridbaglayout());         gridbagconstraints gbc = new gridbagconstraints();         gbc.insets = new insets(5, 5, 0, 0);         1 = new jbutton("1");         2 = new jbutton("2");         3 = new jbutton("3");         4 = new jbutton("4");         buttonstatus = new jlabel("no button pressed");          gbc.gridx = 0;         gbc.gridy = 0;         gbc.gridwidth = gridbagconstraints.relative;         gbc.fill = gridbagconstraints.horizontal;         mainpanel.add(one,gbc);          gbc.gridx = 4;         //gbc.gridy = 0;         mainpanel.add(two,gbc);          gbc.gridx = 0;         gbc.gridy = 1;         gbc.gridwidth = gridbagconstraints.relative;         gbc.fill = gridbagconstraints.horizontal;         mainpanel.add(three,gbc);          gbc.gridwidth = 1;         gbc.fill = gridbagconstraints.none;         gbc.gridx = 4;         mainpanel.add(four,gbc);          gbc.gridwidth=3;         gbc.gridx = 0;         gbc.gridy = 2;         mainpanel.add(buttonstatus,gbc);          // action listeners         checklistener cli = new checklistener(this);         one.addactionlistener(cli);         two.addactionlistener(cli);         three.addactionlistener(cli);         four.addactionlistener(cli);           return mainpanel;     }     public void displaytext(string str)     {         buttonstatus.settext(str);     }     private static void createandshowgui() {          jframe.setdefaultlookandfeeldecorated(true);         jframe frame = new jframe("[=] jtextfield of dreams [=]");          checkbuttongui demo = new checkbuttongui();         frame.setcontentpane(demo.getgui());         frame.setlocation(550, 350);         frame.setdefaultcloseoperation(jframe.exit_on_close);         // no longer manually re-size, use pack automatically size frame.         frame.pack();         frame.setvisible(true);     }     public static void main(string[] args) {         swingutilities.invokelater(new runnable() {             public void run() {                 createandshowgui();             }    });     }  } 

each & works fine try add actionlistener, eclipse ide shows error. if put null, error removes compiler doesn't work. don't understand doing wrong... following actionlistener, in case need more details

package gui;  import java.awt.event.actionevent; import java.awt.event.actionlistener;  public class checklistener implements actionlistener {     //private buttoncheckgui bcg;     private checkbuttongui cbg;      public checklistener(buttoncheckgui b)     {         //bcg = b;         cbg = b;     }     public void actionperformed(actionevent action)     {         string op = action.getactioncommand();         if(op.equals("1"))         {             cbg.displaytext("button 1 pressed");         }else if (op.equals("2"))         {             cbg.displaytext("button 2 pressed");         }else if (op.equals("3"))         {             cbg.displaytext("button 3 pressed");         }else if (op.equals("4"))         {             cbg.displaytext("button 4 pressed");         }else          {             cbg.displaytext("unexpected input");         }     }  } 

actually error in actionlistener's constructor's parameter. i,somehow wrongly, instantiated actionlistner's constructor wrong 'gui model' object. can see, in controller class i.e. actionlistener there buttoncheckgui b instantiated rather checkbuttongui b. replaceing 'buttoncheckgui' checkbuttongui resolved issue!


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -