java - How ActionListener Knows What Button Was Pushed? -


i have written 2 methods take , return string. 1 converts english pig latin , vice versa. have 2 jtextfields (one english , other piglatin- user input). likewise, 2 jbuttons: "to english" , "to pig latin". confused how use action listeners. looks possible have 1 actionlistener execute want "on-click", confused how knows button pushed. instinctively want make 2 actionlisteners each button, whereby when button clicked appropriate code executed.

could please show and/or explain me how works? summary of how understand far:

  1. type english words jtextfield
  2. click "to pig latin" button
  3. actionlistener somehow knows "to pig latin" button clicked , executes topiglatin() method jtextfield text parameter. (assuming jtextfield input automatically considered string type, have not got far yet).
  4. the string returned method output pig latin jtextfield user see.

thanks everyone!

i found looking for. sorry posted on wrong forum , migrating on here belongs...i meant post here! :) for trying figure out how make each button executes block of code when clicked use following: [actionevent].getactioncommand().equals("[name of button]") here's final solution if interested:

public void actionperformed(actionevent e) {     if (e.getactioncommand().equals("to pig latin")){         string english = english.gettext();         piglatin.settext(englishtopiglatin(english));     }     if (e.getactioncommand().equals("to english")){         string piglatin = piglatin.gettext();         english.settext(piglatintoenglish(piglatin));     }     string piggycount = integer.tostring(pigcount);     countp.settext("number of successful word translations:   english: "+piggycount+"   ");     string englishycount = integer.tostring(englishcount);     counte.settext(string.format("pig-latin: "+englishycount)); } 

thanks again tried me out!

there's tutorial on actionlisteners here. easiest way use same listener 2 buttons call e.getactioncommand() on actionevent passed actionperformed function. return string label of button, can use in if statement perform appropriate action.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -