File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Creating Basic swing example" Watch "Creating Basic swing example" New topic

Creating Basic swing example

Pranit Sonawane
Ranch Hand

Joined: Jul 29, 2011
Posts: 171

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

Help me please!!!

Satyen Singh

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

Joined: Oct 13, 2005
Posts: 46412
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:
subject: Creating Basic swing example
It's not a secret anymore!