• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Creating Basic swing example

 
Ranch Hand
Posts: 172
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



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

Help me please!!!


 
Greenhorn
Posts: 21
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic