Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inner classes

Status
Not open for further replies.

Beracosta

Programmer
Oct 25, 2000
47
0
0
SE
Hi everybody... i have a little problem with java.
I have a application that contains alot of differnt classes. And in one of these classes i have a inner class.

My question is: How do i call the inner class?

The application is a "Drawing program". The class that contains the innerclass is called TheCanvas.java and it is used to create a canvas. And in that class i have a inner class called MouseListen and that class is a MouseListener. I want to call the MouseListener in the void main() file but i can't... any help?

/Björn Helin
 
call the MouseListen class through the constructor

 
the constructor of the theCanvas class?

ok thanx
 
But if i wanna call one of the innerclasses in another class? For an exampel if i wanna register a ActionListener on a button that is defined in a class and the listener is a inner class in some other class... how do i do that?

 
ok. in the class MyFrame.java i wanna create a listener with the class CheckButtons that is a inner class in MyCanvas.java do u understand or should i describe more?
 
Her is an example which will help you.
Hpe this helps do let me know if you need explanation

class Automobile {
class Wheel {
String hubcapType;
float radius;
}

Wheel leftWheel = this. new Wheel();
Wheel rightWheel = this. new Wheel();
Wheel extra;

static void thirdWheel(Automobile car) {
if (car.extra == null) {
car.extra = car. new Wheel();
}
return car.extra;
}
}

class WireRimWheel extends Automobile.Wheel {
WireRimWheel(Automobile car, float wireGauge) {
car. super();
}
}
 
Why call the MouseListener direct? If it has been applied to the Canvas then it is listening for mouse events on that class. Just mimic a mouse event and the listener will catch it and react accordingly.



You can refer to the inner class, but an instance of the enclosing class has to exist first.

e.g.
NormClass nc = new NormClass();
NormClass.InnerClass ic = nc.new InnerClass();

Generally, inner classes are used when you don't ever want to refer to the inner class from outside. My home ----> My company ->
 
.... and presumably then referring to the inner class with:

Code:
nc.ic.methodName()

or
Code:
canvasInstance.mouseListenInstance.mouseMethod()

(and the inner class also if necessary being declared public:
class theCanvas{
...
public class MouseListen{
}
}
}
?
 
I believe so, but it does sort of defeat the object of an inner class.... i.e. you create inner classes when they will only ever be called by one class. It is often used to make your code cleaner as the number of public classes is reduced.

In the case of the window listener, the actions defined will be specific to that particular frame and not much use in other frames.

Event Listener inner classes can be triggered by mimicing the appropriate event on the containing class. e.g. JTree.setSelectionPath(aNewPath) will trigger a treeSelectionListener. Calling it directly is often innappropiate. My home ----> My company ->
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top