Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Quick question regarding ActionListeners

  1. #1
    Member
    Join Date
    Oct 2010
    Location
    UK
    Posts
    42
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Quick question regarding ActionListeners

    Hey, just have a quick question. I'm creating a GUI for a program which does different calculations based on what button you press and it's my first time using ActionListeners. I'm wondering, do I have to create a new class implementing ActionListener for every individual button or is there a different way of coding them. For example, below is the part of my code where I have my action listeners and it just seems a bit tedious to have to keep implementing that class, especially when some of the actions are very similar. Many thanks in advance for any help given.

            class BasicListener implements ActionListener { // Calculate basic pay
     
    		public void actionPerformed(ActionEvent e) {
    			String bPay = _basicHours.getText();
    			bAmount = Double.parseDouble(bPay) * BASIC_PAY;
    			_basicTotal.setText(""+bAmount);
     
    		}
    	}
     
    	class HolsListener implements ActionListener { // Calculate holiday pay
    		public void actionPerformed(ActionEvent e) {
    			String hPay = _holsHours.getText();
    			hAmount = Double.parseDouble(hPay) * HOLS_PAY;
    			_holsTotal.setText(""+hAmount);
     
    		}
     
    	}
     
    	class TotalListener implements ActionListener { // Calculate final earnings
     
    		public void actionPerformed(ActionEvent e) {
    			finalAmount = bAmount+hAmount;
    			_finalAmount.setText("" + finalAmount);
    		}
    	}


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Quick question regarding ActionListeners

    You could use one instance of a single ActionListener, added to each JButton, that checks the source in the actionEvent() method and does the appropriate thing. Something like:

    class MyActionListener implements ActionListener(){
       public void actionPerformed(ActionEvent e){
          if(e.getSource() == button1){
          System.out.println("clicked 1");
          }
          else if(e.getSource() == button2){
             System.out.println("clicked 2");
          }
       }
    });
    //...
    ActionListener al = new MyActionListener();
    button1.addActionListener(al);
    button2.addActionListener(al);

    Or you could look into using an anonymous inner class. Something like:

    button1.addActionListener(new ActionListener(){
       public void actionPerformed(ActionEvent e){
          System.out.println("clicked 1");
       }
    });
     
    button2.addActionListener(new ActionListener(){
       public void actionPerformed(ActionEvent e){
          System.out.println("clicked 2");
       }
    });
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Stockholm Syndrome (October 4th, 2011)

  4. #3
    Member
    Join Date
    Oct 2010
    Location
    UK
    Posts
    42
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Quick question regarding ActionListeners

    Ah they seem much handier. I knew there had to be a better way I just couldn't work out how. Thanks a lot (Apologies for making a thread out of this)

Similar Threads

  1. quick question, please HELP!
    By Nemus in forum What's Wrong With My Code?
    Replies: 14
    Last Post: September 29th, 2011, 01:23 PM
  2. Quick Question
    By sird00dguyman in forum Java Theory & Questions
    Replies: 2
    Last Post: August 4th, 2011, 06:14 PM
  3. Another Quick Question
    By Jacksontbh in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 1st, 2011, 07:18 AM
  4. Hi, quick question
    By curras in forum Member Introductions
    Replies: 1
    Last Post: March 21st, 2011, 03:21 PM
  5. quick question - new keyword
    By bbr201 in forum Java Theory & Questions
    Replies: 2
    Last Post: July 18th, 2010, 09:43 PM