java - ActionListener for Buttons -


so have developed program class of mine use actionlistener events when button clicked. have program working when clicking button have click input buttons multiple times answer register , move next level of program.

example1: alert button open's 2 3 "hey there's bug on you!" message dialogs.

example2: yes/no prompts 3-4 times return yes/no answer txt.

example3: color takes 3/4 clicks before returning input txt.

(i think picture...)

for life of me cannot find out why won't take 1 input , move on...

the code of program review... thank in advanced.

import javax.swing.*; import java.awt.*; import java.awt.event.*;   public class messageboxes{    private jbutton alert = new jbutton("alert");   private jbutton yesno = new jbutton("yes/no");   private jbutton color = new jbutton("color");   private jbutton vals = new jbutton("3 vals");   private jbutton input = new jbutton("input");    private jtextfield txt = new jtextfield(15);    private jframe frame;    private actionlistener al = new actionlistener(){     public void actionperformed(actionevent e){      alert.addactionlistener(new actionlistener() {       public void actionperformed(actionevent e)         {             //execute when button pressed             joptionpane.showmessagedialog(null,                                   "there's bug on you!", "hey!",                                   joptionpane.error_message);         }     });        yesno.addactionlistener(new actionlistener() {       public void actionperformed(actionevent e)         {             //execute when button pressed             int val = joptionpane.showconfirmdialog(null,                                   "choose yes or no", "your call...",                                   joptionpane.yes_no_option);              if(val != joptionpane.closed_option){               if(val == 0){                 txt.settext("yes");             }             else{                 txt.settext("no");             }             }         }     });        color.addactionlistener(new actionlistener() {       public void actionperformed(actionevent e)         {             //execute when button pressed             object[] options = {"red", "green"};              int sel = joptionpane.showoptiondialog(null,                             "choose color!", "warning",                             joptionpane.default_option,                             joptionpane.warning_message, null,                                         options, options[0]);              if(sel != joptionpane.closed_option){                 txt.settext("color selected: " + options[sel]);             }         }     });        vals.addactionlistener(new actionlistener() {       public void actionperformed(actionevent e)         {             //execute when button pressed             object[] selections = {"first", "second", "third"};              object val = joptionpane.showinputdialog(null,                                              "choose one", "input",                                              joptionpane.information_message,                                              null, selections, selections[0]);              if(val != null){                 txt.settext(val.tostring());             }         }     });      input.addactionlistener(new actionlistener() {       public void actionperformed(actionevent e)         {             //execute when button pressed             string val = joptionpane.showinputdialog("how mant fingers see?");                  txt.settext(val);         }     });     }   };      public messageboxes(){      frame = new jframe("title");      frame.setsize(250, 250);      frame.getcontentpane().setlayout(new flowlayout());      frame.setdefaultcloseoperation(jframe.exit_on_close);      frame.getcontentpane().add(alert);     frame.getcontentpane().add(yesno);     frame.getcontentpane().add(color);     frame.getcontentpane().add(vals);     frame.getcontentpane().add(input);     frame.getcontentpane().add(txt);      frame.setvisible(true);    }    public void launchjframe(){      alert.addactionlistener(al);     yesno.addactionlistener(al);     color.addactionlistener(al);     vals.addactionlistener(al);     input.addactionlistener(al);  }   public static void main(string [] args){      messageboxes messageboxes = new messageboxes();     messageboxes.launchjframe();  } } 

the problem is, each time actionperformed called within actionlistener al, registering new actionlisteners buttons

private actionlistener al = new actionlistener() {     public void actionperformed(actionevent e) {          alert.addactionlistener(new actionlistener() {             //...         });          yesno.addactionlistener(new actionlistener() {             //...         });          color.addactionlistener(new actionlistener() {             //...         });          vals.addactionlistener(new actionlistener() {             //...         });          input.addactionlistener(new actionlistener() {             //...         });     } }; 

so, each time actionperformed method called, each button registers new actionlistener.

instead, use if-else statement determine source of event, example...

object source = e.getsource(); if (source == alert) {     joptionpane.showmessagedialog(null,                     "there's bug on you!", "hey!",                     joptionpane.error_message); } else if (... 

or rid of actionlistener al altogether , simple register individual actionlisteners buttons within launchjframe method...

public void launchjframe() {      alert.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e) {             //execute when button pressed         }     });      yesno.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e) {             //execute when button pressed             int val = joptionpane.showconfirmdialog(null,                             "choose yes or no", "your call...",                             joptionpane.yes_no_option);              if (val != joptionpane.closed_option) {                 if (val == 0) {                     txt.settext("yes");                 } else {                     txt.settext("no");                 }             }         }     });      color.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e) {             //execute when button pressed             object[] options = {"red", "green"};              int sel = joptionpane.showoptiondialog(null,                             "choose color!", "warning",                             joptionpane.default_option,                             joptionpane.warning_message, null,                             options, options[0]);              if (sel != joptionpane.closed_option) {                 txt.settext("color selected: " + options[sel]);             }         }     });      vals.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e) {             //execute when button pressed             object[] selections = {"first", "second", "third"};              object val = joptionpane.showinputdialog(null,                             "choose one", "input",                             joptionpane.information_message,                             null, selections, selections[0]);              if (val != null) {                 txt.settext(val.tostring());             }         }     });      input.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e) {             //execute when button pressed             string val = joptionpane.showinputdialog("how mant fingers see?");              txt.settext(val);         }     });  } 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -