Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Swing / AWT / SWT and the fly likes Trouble with runnable and inner classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Trouble with runnable and inner classes" Watch "Trouble with runnable and inner classes" New topic
Author

Trouble with runnable and inner classes

Ken Butters
Greenhorn

Joined: Oct 23, 2008
Posts: 6
I'm new to Java and am having some trouble with runnable in conjunction with inner classes. I was able to get the code to work until I made the the gui runnable. once I did that I get the following error:

"No enclosing instance of type Temperature is accessible. Must qualify the allocation enclosing instance of type Temperature. ...."

This application simply converts from fahrenheit to celcius. I was learn swing, and through examples I found online... saw an indication that the gui form should be invoked as a runnable thread.

Once I modified the code to use runnable... I seem to have lost accessibility to the other classes in my main class "Temperature".

Any idea why?

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

First of all, please Use Code Tags in the future. I've added them for you, and your code looks a lot more readable.

The problem is the creation of instances of your two action listeners. You try to do this from a static method, but because the classes are not static inner classes you need an instance of Temperature to instantiate them. An easy fix in this case is to make those classes static, but in cases you can't you would need to create a Temperature object first:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
Don't get me started about those stupid light bulbs.
 
subject: Trouble with runnable and inner classes