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: what's wrong in this code

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Location
    Chennai
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default what's wrong in this code

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class A implements ActionListener
    {
    public static void main(String args[])throws Exception
    {
    JFrame f=new JFrame("aravind");
    JTextField t=new JTextField(30);
    JButton b=new JButton("CLICK ME");
    f.setSize(1000,1000);
    f.setLayout(new FlowLayout());
    f.add(t);
    f.add(b);
    b.addActionListener(this);
    f.setVisible(true);
    }
    public void actionPerformed(ActionEvent e)
    {
    System.out.println("CLICKED");
    System.exit(0);
    }
    }


    ERROR MESSAGE is:


    C:\Documents and Settings\prabhu\Desktop>javac A.java
    A.java:15: non-static variable this cannot be referenced from a static context
    b.addActionListener(this);
    ^
    1 error


  2. #2
    Member goldest's Avatar
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    63
    Thanks
    1
    Thanked 12 Times in 10 Posts

    Wink Re: what's wrong in this code

    Hey Arvind,

    Usually in java the keyword "this" refers to the current object. And static means per class and not per object. The rule is that you can't access a non-static variable from a static method, which in your case is main method.

    So instead of generating your GUI through main, you can build the same in some other method and call that method from main.

    Here is the example,

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class A implements ActionListener {
    	public static void main(String args[]) {
    		A a = new A();
    		a.click();
    	}
     
    	public void click() {
    		JFrame f = new JFrame("aravind");
    		JTextField t = new JTextField(30);
    		JButton b = new JButton("CLICK ME");
    		f.setSize(200, 200);
    		f.setLayout(new FlowLayout());
    		f.add(t);
    		f.add(b);
    		b.addActionListener(this);
    		f.setVisible(true);
    	}
     
    	public void actionPerformed(ActionEvent e) {
    		System.out.println("CLICKED");
    		System.exit(0);
    	}
    }
    I am not sure, why your main method was throwing the Exception, that wasn't needed; so I have removed it in this code above.

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!

    Sun: Java Coding Conventions

    Click on THANKS if you like the solution provided.
    Click on Star if you are really impressed with the solution.

  3. #3
    Member goldest's Avatar
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    63
    Thanks
    1
    Thanked 12 Times in 10 Posts

    Wink Re: what's wrong in this code

    Just in case if you are interested to do the things from main only, then you will have to change the implementation a bit.

    You will need to use the static nested class inside your class. That nested class will have to implement the ActionListener interface. And you can pass the instance of that class to the addActionListener method.

    Just like following,

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class A {
    	public static void main(String args[]) {
    		JFrame f = new JFrame("aravind");
    		JTextField t = new JTextField(30);
    		JButton b = new JButton("CLICK ME");
    		f.setSize(200, 200);
    		f.setLayout(new FlowLayout());
    		f.add(t);
    		f.add(b);
    		b.addActionListener(new ClickTest());
    		f.setVisible(true);
    	}
     
    	static class ClickTest implements ActionListener {
    		public void actionPerformed(ActionEvent e) {
    			System.out.println("CLICKED");
    			System.exit(0);
    		}
    	}
    }
    Hope that's clear to you...

    Goldest
    Java Is A Funny Language... Really!

    Sun: Java Coding Conventions

    Click on THANKS if you like the solution provided.
    Click on Star if you are really impressed with the solution.

Similar Threads

  1. What's wrong with my code
    By javapenguin in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 10th, 2010, 03:24 PM
  2. What's wrong with my code ?
    By mithani in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 5th, 2010, 08:57 AM
  3. [SOLVED] what is wrong for the java code
    By chuikingman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 11th, 2010, 02:00 AM
  4. I can't find out what is wrong with my code. Please Help!
    By hallor618 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 10th, 2010, 02:44 PM
  5. Generation of Palindrome number in Java
    By tina.goyal in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 26th, 2009, 08:49 AM