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: can we use the Action Listener in a awt without using constructors? clarify the following error?

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default can we use the Action Listener in a awt without using constructors? clarify the following error?

    ClickMe2.java:21: non-static variable button1 cannot be referenced from a static context
    button1 = new JButton("Click Me!");
    ^
    ClickMe2.java:22: non-static variable cl cannot be referenced from a static context
    button1.addActionListener(cl);
    ^
    ClickMe2.java:22: non-static variable button1 cannot be referenced from a static context
    button1.addActionListener(cl);
    ^
    ClickMe2.java:23: non-static variable button1 cannot be referenced from a static context
    panel1.add(button1);
    ^
    4 errors

    Code:

    import javax.swing.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    public class ClickMe2
    {
    	public static void main(String [] args)
    	{
    	new ClickMe2();
    	}
    	private JButton button1;
    	public ClickMe2()
    	{
    		JFrame f=new JFrame();
    		f.setSize(200,100);
    		f.setDefaultCloseOperation(
    		JFrame.EXIT_ON_CLOSE);
    		f.setTitle("I’m Listening");
    		ClickListener cl = new ClickListener(); 
    		JPanel panel1 = new JPanel(); 
    		button1 = new JButton("Click Me!"); 
    		    button1.addActionListener(cl); 
    		    panel1.add(button1); 
    		    f.add(panel1); 
    		f.setVisible(true);
    	}
     class ClickListener
    implements ActionListener
    {
    	private int clickCount = 0;
    	public void actionPerformed(ActionEvent e)
    	{
    		if (e.getSource() == button1)
    		{
    		clickCount++;
    		if (clickCount == 1)
    		button1.setText("I’ve been clicked!");
    		else
    		button1.setText("I’ve been clicked "
    		+ clickCount + " times!");
    	}
    }
    }
    }
    Last edited by jps; September 5th, 2013 at 01:54 AM. Reason: code tags


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: can we use the Action Listener in a awt without using constructors? clarify the following error?

    Welcome to the forum
    See the Announcements page for the use of code tags.

    "non-static variable button1 cannot be referenced from a static context"
    I see no such problem in the code as posted

  3. #3
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: can we use the Action Listener in a awt without using constructors? clarify the following error?

    Hello.
    Are you sure you compiled the above code which you have posted?
    Or did you post the different code by mistake?

    Syed.

Similar Threads

  1. Action Listener Error
    By dookie1293 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 22nd, 2011, 09:34 PM
  2. Action Listener
    By Suzanne in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 29th, 2010, 10:50 AM
  3. Beginner Help (Action Listener)
    By gradstudent in forum AWT / Java Swing
    Replies: 2
    Last Post: April 30th, 2010, 10:26 AM
  4. Action Listener
    By kray66 in forum AWT / Java Swing
    Replies: 2
    Last Post: April 19th, 2010, 03:26 PM
  5. Need Help Action Listener....
    By vhizent23 in forum AWT / Java Swing
    Replies: 2
    Last Post: October 9th, 2009, 01:46 PM