wood burning stoves 2.0*
The moose likes Java in General and the fly likes Something about anonymous  classes and variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Something about anonymous  classes and variables" Watch "Something about anonymous  classes and variables" New topic
Author

Something about anonymous classes and variables

Petar Thomas
Ranch Hand

Joined: Oct 11, 2009
Posts: 234

Hi, here is a code, I will break it where I want to ask a question




I gave to b reference to 'this'. It seems to me too clumsy. Can I do something else to still hold this two lines?


Thank You....

Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

Both of those method are inside of your Board class correct? So you can just use the this keyword to refer to the board instead of assigning this to b.



Hunter


"If the facts don't fit the theory, get new facts" --Albert Einstein
Petar Thomas
Ranch Hand

Joined: Oct 11, 2009
Posts: 234
Thank You very much , it is exactely what I am asking, but, I get an error, working in Eclipse:

The method getWidth() is undefined for the type new ComponentListener(){}

..which sounds logical...

Thanks, : ))
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

Wait so which line are you getting an error at? When trying to add the component listener to the board? (line 15)
or on the getHeight() and getWidth() lines (20,21)?

Also do you mind posting your whole class so I can see the getWidth() and getHeight() methods?

Hunter
Petar Thomas
Ranch Hand

Joined: Oct 11, 2009
Posts: 234
... I don't mind anything... getWidth() and getHeight() are methods from JPanel. This class is all I written, I mean, it is posted whole as it is, so.. this two methods belong to the Board, becaouse Board extends JPanel...

..and error... is there where this is inserted, error is becaouse of "this"..., 20, 21..

Not when i add component listener to the board...


Thank You !

Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

I am not sure if this is right or not, do you have a test program that runs this? or were you getting the error just trying to compile the class?

This is what I tried:


Hope this helps,
Hunter

Edit:I dont think this will help you, sorry I dont have much applet experience
Petar Thomas
Ranch Hand

Joined: Oct 11, 2009
Posts: 234
Heres ready for execution, but it won't, it won't compile..

here's error:
Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems:
The method getWidth() is undefined for the type new ComponentListener(){}
The method getHeight() is undefined for the type new ComponentListener(){}

....




Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

ComponentListener is an interface, so if you are going to implement its method in your class you need to add:

to your class declaration.

Hunter
Petar Thomas
Ranch Hand

Joined: Oct 11, 2009
Posts: 234
Yes, I'll do that right now...
Petar Thomas
Ranch Hand

Joined: Oct 11, 2009
Posts: 234
Ockay, the real problem is solved... I wanted to do it like that anyway.. maybe I'm litlle bit experimenting... I will just have to read about anonymous and inner classes.

Thank You very much......



Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

To get a reference of the "this" of the enclosing instance inside anonymous classes (and all other inner classes), prefix "this" with the class name. For example:
Note that I switched from ComponentListener to ComponentAdapter. The latter is an abstract class that implements all methods of ComponentListener with empty bodies. Most event listener interfaces with more than 1 method have such an Adapter class (e.g. MouseAdapter).

Now that I've shown you how to get Board.this, I'll also tell you that you don't need to use "this" all of the time. Unless the inner class also has a method called getWidth (with the same or different parameter types), "w = getWidth();" is the same as "w = Board.this.getWidth();". "this" is only required when another field / parameter / method shadows (or hides) the instance field / method.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Petar Thomas
Ranch Hand

Joined: Oct 11, 2009
Posts: 234
Very nice.
Petar Thomas
Ranch Hand

Joined: Oct 11, 2009
Posts: 234
Thank You
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

You're welcome.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Something about anonymous classes and variables
 
Similar Threads
Image Crop
cool java swing application
Magnifying only a part of the image
Please help with this program
single dimension array to multi dimension