Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training  

Action Listeners



Share on Google+Share on Google+

Samuel A.
Action Listeners
1 Answer(s)      6 years and 9 months ago
Posted in : Java Beginners

Please, could someone help me with how to use action listeners I am creating a gui with four buttons. I will like to know how to apply the action listener to these four buttons.

View Answers

October 29, 2010 at 5:34 PM

Hello Friend,

Try the following code:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class  ButtonAction extends JFrame implements ActionListener{
    JButton b1,b2,b3,b4;
    b1=new JButton("A");
    b2=new JButton("B");
    b3=new JButton("C");
    b4=new JButton("D");
    JPanel p=new JPanel();
public void actionPerformed(ActionEvent e){
    String choice=e.getActionCommand();
        JOptionPane.showMessageDialog(null,"This is button A");
else if(choice.equals("B")){
    JOptionPane.showMessageDialog(null,"This is button B");
else if(choice.equals("C")){
    JOptionPane.showMessageDialog(null,"This is button C");
else if(choice.equals("D")){
    JOptionPane.showMessageDialog(null,"This is button D");
public static void main(String[] args){
     new ButtonAction();



Related Tutorials/Questions & Answers:
Action Listeners
Action Listeners  Please, could someone help me with how to use action listeners I am creating a gui with four buttons. I will like to know how to apply the action listener to these four buttons.   Hello Friend, Try
Event Listeners
Event Listeners  I want to learn the Event Listeners in Java. Any... is the good tutorial and example of Event Listeners in Java. Please check the tutorial Java - Event Listeners Example in Java Applet. Thanks
Event listeners in mxml
Event listeners in mxml  hi.. just tell me about How do you add event listeners in mxml components. Now AS3 components? Please give... for add a event listner in flex using action script. 1. addEventListener(type:String
Action Event Listener
Action Event Listener          Action Listeners can be implemented... event listeners. 1. Using "actionListener" attribute of the component
Listeners - Java Beginners
)"; } actionString = "action key? "; if (e.isActionKey
adding mouse listeners to drop target
adding mouse listeners to drop target  import java.awt.*; import java.awt.dnd.DnDConstants; import java.awt.dnd.DragGestureRecognizer; import... with adding mouse listeners to "table" which is drop target, to accept drop
no action mapped for action - Struts
no action mapped for action  Hi, I am new to struts. I followed...: There is no Action mapped for action name HelloWorld
Action and ActionSupport
Action and ActionSupport  Difference between Action and ActionSupport.... The developer implements this interface of accessing string field in action... for implements Action and some other interfaces and provides some feature like data
Action form
Action form  how to store details from user and how to create asp file or etc
UISlider action
UISlider action  Hi, How to capture and use UISlider action? Thanks   Hi, Following code might helpful. -(IBAction)changeSlider:(id)sender { NSString *value= [[NSString alloc] initWithFormat:@" Value %d
Struts Action Chaining
Struts Action Chaining  Struts Action Chaining
ActionMapping and is the Action Mapping specified
ActionMapping and is the Action Mapping specified  What is ActionMapping and is the Action Mapping specified
What is Action Class?
What is Action Class?  What is Action Class? Explain with Example
the Action Mapping specified
the Action Mapping specified  How is the Action Mapping specified
Action Configuration - Struts
Action Configuration  I need a code for struts action configuration in XML
TriggerListeners and JobListeners
;    To perform any action you create Listeners objects... listeners are registered with the scheduler, and must be given a name. Listeners can be registered as either "global" or "non-global". Global listeners
Action without a form.
Action without a form.  Can I have an Action without a form
Struts Action Class
Struts Action Class  What happens if we do not write execute() in Action class
Jsp Action Tags
Jsp Action Tags  how can i use jsp forward action tag?i want examples
JSP Action Tag
JSP Action Tag   Defined JSP Action Tag ?   Action tag... JSP action tags to either link to a Java Bean set its properties, or get its properties. syntax of Action Tag : <jsp:action attributes /> The most
action tag - Struts
action tag  Is possible to add parameters to a struts 2 action tag? And how can I get them in an Action Class. I mean: xx.jsp Thank you
JSP Forward action
JSP Forward action  Jsp forward action tag examples
Action Or DispatchAction - Development process
Action Or DispatchAction   Hi, Action class has execute() only where as dispatchaction class has multiple methods. plz tell me when to use action and dispatchaction.can we use multiple actions in Action class. Thanks Prakash
Struts Action Classes
Struts Action Classes  1) Is necessary to create an ActionForm to LookupDispatchAction. If not the program will not executed. 2) What is the beauty of Mapping Dispatch Action
RADIO FROM JSP TO ACTION.  Hi frds, how to get the selected multiple radio button values from jsp to action
Action classes in struts
Action classes in struts  how many type action classes are there in struts   Hi Friend, There are 8 types of Action classes: 1.ForwardAction class 2.DispatchAction class 3.IncludeAction class 4.LookUpDispatchAction
Login Action Class - Struts
Login Action Class  Hi Any one can you please give me example of Struts How Login Action Class Communicate with i-batis
uibarbuttonitem initwithcustomview action
uibarbuttonitem initwithcustomview action  HI, Can anyone provide me the code for uibarbuttonitem initwithcustomview action? In my application I am adding custom BarButtonItem, how I can attach an action to it? Thanks
action Servlet - Struts
action Servlet  What is the difference between ActionServlet ans normal servlet? And why actionServlet is required
Action tag - JSP-Servlet
Action tag  Hello, I want to help ....i hav one feedback form there is action , .. can i use two action at the same form because i want... action="services.html " but it only shows html page bt does not submit my data