• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner classes in Eclipse

 
Liz Carter
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am half way in Head first java.. and trying out the pool puzzle on page 396. for the amazing shrinking blue rectangle.

I am using eclipse to complie and run my programs. but it doesnt seem to recognize the inner class MyDrawP. I am getting the error MyDrawP cannot be resolved to a type. Please let me know where I am making the mistake?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will be quicker to help you if you post the code.
 
Liz Carter
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok here goes...
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(I have enclosed your source with a "code" tag. Please use it the next time you post some code.)

You have declared the inner class inside a method. I don't know how the code looks in your book, but if you want to use an inner class that way, you'll have to declare the class before using variables of that class. Like this :


Are you sure that the MyDrawP class is inside the go() method ?
 
Liz Carter
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Declaring the inner class before the variables removed the error. Thanks.

and yes the inner class is declared in go() and it is given at the end of the method in the book.
 
Rob Spoor
Sheriff
Pie
Posts: 20659
64
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure the book didn't declare the class just after the method, instead of at the end inside?
 
Arend van der Kolk
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
indeed, according to the book the inner class MyDrawP comes after the go() method, not within the go()method.

But i have another interesting beginner problem (any problem is interesting, ;-)) The blue rectange does not show up although the compiler does not show any error. Just a grey frame.

I have added some print statements in between to check this problem. And i do see the print statement ""this for loop is used" + i. so this means the for loop runs. But i don't see the print qoute ""this painComponent method is used"

So, i think my inner class "class MyDrawP extends JPanel" is not running.

I found the problem. One little TYPO in painComponent. I forgot the "t", ;-)

 
Campbell Ritchie
Sheriff
Pie
Posts: 50168
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arend van der Kolk wrote:indeed, according to the book the inner class MyDrawP comes after the go() method, not within the go()method.. . . .[
That should make no difference. Inner classes declared outside methods are members of their surrounding class, and they are in scope throughout the whole class, even before where they are declared. In some other languages, e.g. C, you would have to declare something before it is used, but Java® can recognise member declarations backwards.

Don't use Thread#sleep() in Swing components. because it stops the display from responding. Remember everything has to be done on the one Thread. Try a Swing timer instead. What may be happening is that the time taken to repaint the GUI is longer than the time between the creation of the panel object and the thread going to sleep. So the display is never actually seen.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic