This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Creating Basic swing example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Creating Basic swing example" Watch "Creating Basic swing example" New topic
Author

Creating Basic swing example

Pranit Sonawane
Ranch Hand

Joined: Jul 29, 2011
Posts: 155




error: non-static method getContentPane() cannot be referenced from a static context Container contentPane = getContentPane();

Help me please!!!


Satyen Singh
Greenhorn

Joined: Oct 05, 2011
Posts: 21

the getContentPane() returns the non-static reference of class Container
and Since you used it in your main() method which is static method it will give an error.

You cannot access non-static reference from the static context.

so do one thing create a contructor for your label class and write your code inside it.
and then invoke the constructor from the main() method.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
That is a standard error caused by trying to access fields from the main() method. You should know to get your code out of the main method. And I think you ought to have two classes. Also you should use the usual conventions about class names; “label” is a name which has already been used, and which ought to have a capital L.
Now you want all your “real” code in the run() method.

Of course, you are making like difficult for yourself, using applets and Swing. Swing classes should be called on the Event Dispatch Thread only. And applets are even more complicated. You will have to go through the Java Tutorials, here, and here. An applet is called within an HTML document, and you usually put the initialisation code in its init() method, rather than the constructor.

Get a FooDemo working with a main method like what I showed you. Then you can try a little Swing thing like what I showed here, last week. Beware: there are two errors in my code. You will find out what they are if you read the whole thread. I have also omitted some of the import declarations.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating Basic swing example