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 5 of 5

Thread: Not sure how to deal with addActionListener! :3

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Not sure how to deal with addActionListener! :3

    package appletPackage;
     
    import javax.swing.JApplet;
    import javax.swing.JButton;
    import java.awt.event.*;
    import java.awt.*;
     
    public class MainClass extends JApplet,Frame implements ActionListener {
    	private JButton button;
    	private String text = "Screenflashing";
    	Button btn;
    	public MainClass(){
     
    	}
    	public void init(){
    		button = new JButton(text);
    		button.addActionListener(this);
    		add(button);
    		btn = new Button("Regular button");
    		btn.addActionListener(this);
    		add(btn);
     
    	}
    	public void actionPerformed(ActionEvent e){
    			button.setText("BUAHAHAH.");
    			showStatus("actionPerformed...");
    		}
     
    }

    Umm, yeah... I'm trying to make two buttons with seperate actionPerformed functions so I can make them do separate things; how exactly would I do that? Thanks in advance!


  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: Not sure how to deal with addActionListener! :3

    Instead of implementing ActionPerformed in that class, you want to create two separate classes that implement it instead. Those could be in separate class files, or as inner classes, or as anonymous inner classes.

    Also, you could use one ActionListener, and check which JButton e.getSource() returns, then do different things depending on which one it was.
    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. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Not sure how to deal with addActionListener! :3

    package appletPackage;
     
    import javax.swing.JApplet;
    import javax.swing.JButton;
    import java.awt.event.*;
    import java.awt.*;
     
    public class MainClass extends JApplet,Frame implements ActionListener {
    	private JButton button;
    	private String text = "Screenflashing";
    	Button btn;
    	public MainClass(){
     
    	}
    	public void init(){
    		button = new JButton(text);
    		button.addActionListener(this);
    		add(button);
    		btn = new Button("Regular button");
    		btn.addActionListener(this);
    		add(btn);
     
    	}
    	public void actionPerformed(ActionEvent e){
    			if e.getSource() == button {
    			button.setText("BUAHAHAH.");
    			showStatus("actionPerformed...");	
    			}
     
    		}
     
    }

    e.getSource returns an Object, so then exactly how do I correctly check with it? As you can see I tried checking of what I thought would be 2 objects, but...

  4. #4
    Junior Member
    Join Date
    Jun 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Not sure how to deal with addActionListener! :3

    Disregard that last post, I got it; now, notice I'm 'addActionListener' to 2 buttons, but it's erroring on me? How do I add the addActionListener to multiple buttons without it erroring?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Not sure how to deal with addActionListener! :3

    Does this code compile?

    public class MainClass extends JApplet,Frame implements ActionListener {


    but it's erroring on me?
    Please copy and paste the full text of the error message here.

Similar Threads

  1. No Time To Deal With The Assigment
    By Vins25 in forum Java Theory & Questions
    Replies: 14
    Last Post: May 3rd, 2011, 07:39 AM
  2. non-static variable, addActionListener()
    By bobbyrne01 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 31st, 2010, 10:58 AM