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

Thread: addActionListener to JButton

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default addActionListener to JButton

    Hello,

    I'm trying to write a method that makes a JButton and adds actionlistener but it doesn't work.
    The code below is written in a Class that extends JPanel implements ActionListener.

    I tried to replace "this" with instance of class but that also didn't work. Can somebody help me out, I wrote this to understand
    how addActionListener works. But I can't figure it out

    Thanks in advance!

    	public void setButton(JButton knop, String naam)
    	{
    		knop = new JButton(naam);
    		knop.setBackground(Color.LIGHT_GRAY);
    		knop.setForeground(Color.BLACK);
    		knop.setFont(new Font("Ariel", Font.PLAIN, 20));
    		knop.addActionListener(this); <<<<<<-----
    		this.add(knop);
    	}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: addActionListener to JButton

    it doesn't work
    Please explain what "doesn't work"means.
    Copy the full text of any error messages and paste it here,
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: addActionListener to JButton

    It doesnt give an error message, the ActionListener just doesn't work. When I click a button it does nothing..

    Thank you for your response!

    public class MainMenu extends JPanel implements ActionListener
    {
    	private JButton PLAY, HELP, CREDITS, EXIT, knop;
    	private Image Background;
     
    	public MainMenu()
    	{
    		setButton(PLAY, "Play");
    		setButton(HELP, "Help");
    		setButton(CREDITS, "Credits");
    		setButton(EXIT, "Exit");
     
    		Background = new ImageIcon("C:\\projectJava\\MenuBG.jpg").getImage();
    	}
     
    	public JButton setButton(JButton knop, String naam)
    	{
    		knop = new JButton(naam);
    		knop.setBackground(Color.LIGHT_GRAY);
    		knop.setForeground(Color.BLACK);
    		knop.setFont(new Font("Ariel", Font.PLAIN, 20));
    		knop.addActionListener(this);
    		knop.setSize(350,350);
    		knop.setAlignmentX(CENTER_ALIGNMENT);
    		knop.setAlignmentY(CENTER_ALIGNMENT);
    		this.add(Box.createRigidArea(new Dimension(0,20)));
    		this.add(knop);
    		return knop;
    	}
     
    	public void paintComponent(Graphics g)
    	{
    		super.paintComponents(g);
    		g.drawImage(Background,0,0,null);
    	}
     
    	public void actionPerformed(ActionEvent e) 
    	{
    		if(e.getSource() == PLAY)
    		{
    			System.out.println("hello");
    		}
    		else if(e.getSource()==HELP)
    		{
     
    		}
    		else if(e.getSource()==CREDITS)
    		{
    			System.out.println("werkt");
    		}
    		if(e.getSource()==knop)
    		{
    			System.out.println("Hello");
    		}
    	}
     
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: addActionListener to JButton

    Add a println statement to the listener method that prints a message when it is called and also prints out the value of e
    so you can see if it is executed and what the contents of the event object is.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2012
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: addActionListener to JButton

    If I modify the constructor like this (with JButton knop) the ActionListener works and does print "Hello" like it is written in the actionPerformed method.
    so from that I can see that the problem should be with that setButton method where I addActionListener(THIS), I think the problem is with that THIS.

    Could it be? I'm not sure because I have no clue how to make it work otherwise

    Thank you for your response

    public MainMenu()
    	{
                    knop = new JButton("Help")
                    knop.addActionListener(this);
                    this.add(knop);
     
    		setButton(PLAY, "Play");
    		setButton(HELP, "Help");
    		setButton(CREDITS, "Credits");
    		setButton(EXIT, "Exit");
     
    		Background = new ImageIcon("C:\\projectJava\\MenuBG.jpg").getImage();
    	}

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: addActionListener to JButton

    If the listener called OK now, what is the code in the listener method supposed to do?

    think the problem is with that THIS.
    What is the problem now?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    May 2012
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: addActionListener to JButton

    When I click a button, suppose PLAY, it should print out "hello". It doesn't do that, so I think the THIS in knop.addActionListener(THIS) is for the ActionListener and is supposed to be written in the constructor. Am I correct?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: addActionListener to JButton

    The this variable can be used in any non static method in a class.

    You need to make a small, complete program that compiles, executes and shows the problem. There small pieces of code that you've posted are not enough to show the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    ICEPower (April 5th, 2013)

  10. #9
    Junior Member
    Join Date
    May 2012
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: addActionListener to JButton

    I don't think I understand the problem, but I also don't really understand what you're saying about a program that shows the problem. I'm kind of really new to Java.
    Thank you for the help though!

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: addActionListener to JButton

    I was asking for a program that will compile, execute and show the problem. Without that I can't say what is happening.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Junior Member
    Join Date
    May 2012
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: addActionListener to JButton

    The only other class I've made is a JFrame to show a GUI, except that I haven't written any other program.
    I still don't understand why it doesn't recognize actionperformed for the Button but I found another way to fix it

    I made another class ButtonListener implements ActionListener and an actionPerformed in that class which will handle the buttonclick.
    In my MainMenu class, in setButton method I've written knop.addActionListener(new ButtonListener()).

    And that works

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: addActionListener to JButton

    Mark this as solved then.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. for Jbutton
    By Rizalyn in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 8th, 2013, 10:16 PM
  2. Clarification needed for addActionListener and actionPerformed
    By mabelanger in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 7th, 2013, 09:03 PM
  3. Can't use button.addActionListener(this); ?
    By xdega in forum AWT / Java Swing
    Replies: 2
    Last Post: April 23rd, 2012, 08:44 AM
  4. Not sure how to deal with addActionListener! :3
    By Asteroid555 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 1st, 2011, 01:57 PM
  5. non-static variable, addActionListener()
    By bobbyrne01 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 31st, 2010, 10:58 AM

Tags for this Thread