Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    ActionListener Help

    I need some help adding an ActionListener to a button.

    Here's the one error I'm getting:

    Code:
    .\Frame.java:38: addActionListener(java.awt.event.ActionListener) in javax.swing
    .AbstractButton cannot be applied to (Frame)
                    button2.addActionListener(this);
                           ^
    1 error
    Press any key to continue . . .
    Here's my Frame.java:

    Code:
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    
    public class Frame extends JFrame {
    
    
    	public Frame() {
    
    		super("Java");
                    JButton button=new JButton("Go Left");
    		JButton button1=new JButton("Go Right");
    		JButton button2=new JButton("Go Forward");
    		JButton button3=new JButton("Go Backward");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		//addWindowListener(this);
    		
    		setLayout(null);
    
    	    	setSize(500, 500);
    		setBackground(Color.red);
    
    		add(button);
    		add(button1);
    		add(button2);
    		add(button3);
    
    		button.setSize(100, 32);
    		button.setLocation(75, 200);
    
    		button1.setSize(100, 32);
    		button1.setLocation(315, 200);
    
    		button2.setSize(100, 32);
    		button2.setLocation(196, 130);
    		button2.addActionListener(this);
    
    		button3.setSize(110, 32);
    		button3.setLocation(191, 260);
    	
                    setVisible(true);
    		setResizable(false);
    		
            }
    
    	public void actionPerformed(ActionEvent e) {
    		Forward.goForward();
    	}
    
    }

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    addActionListener needs to take a parameter that is of type ActionListener. According to the API, JFrame doesn't implement ActionListerner (which wouldn't make much sense anyway), so you need to add an implements ActionListener to your class signature.
    That error sure doesn't look right though.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Wow, thanks. Can't believe I missed that. Anyways, now I need something else. How would you go about adding separate ActionListeners to each button?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    If they are completely different, write three actionlistener classes, or add an anonymous class to the button's action listener.
    PHP Code:
    button.addActionListener(new ActionListener()
    {
        public 
    void actionPerformed(ActionEvent e)
        {
        }
    }); 
    If they are roughly the same, write a single actionListener class with specific instance arguments to control what they do (only changes in data for example).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    New Coder
    Join Date
    Feb 2011
    Posts
    48
    Thanks
    1
    Thanked 2 Times in 2 Posts
    You can also extend you class (JButton) where you can add an implements ActionListener. You have more solutions and you can choose the one which suits best your needs.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •