• 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

Inner classes in Eclipse

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok here goes...
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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", ;-)

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