hi guys, I wonder if anybody can clarify something for me.
Take these 2 fragments of code (2 constructors belonging to 2 different applications):
First one:
public MouseTrackerFrame()
{
super( "Demonstrating Mouse Events" );
mousePanel = new JPanel(); // create panel
mousePanel.setBackground( Color.WHITE ); // set background color
add( mousePanel, BorderLayout.CENTER ); // add panel to JFrame
statusBar = new JLabel( "Mouse outside JPanel" );
add( statusBar, BorderLayout.SOUTH ); // add label to JFrame
// create and register listener for mouse and mouse motion events
MouseHandler handler = new MouseHandler();
mousePanel.addMouseListener( handler );
mousePanel.addMouseMotionListener( handler );
} // end MouseTrackerFrame constructor
private class MouseHandler implements MouseListener,
MouseMotionListener
...
Second one:
public MouseDetailsFrame()
{
super( "Mouse Clicks and Buttons" );
statusBar = new JLabel( "Click the mouse" );
add( statusBar, BorderLayout.SOUTH );
addMouseListener( new MouseClickHandler() ); // add handler
} // end MouseDetailsFrame constructor
private class MouseClickHandler extends MouseAdapter{
...
Now, the first one is registering the handlers like so:
MouseHandler handler = new MouseHandler();
mousePanel.addMouseListener( handler );
mousePanel.addMouseMotionListener( handler );
with an object of type JPanel (mousePanel
) that calls addMouseListener method.
In the second example the handler is registered only with addMouseListener( new MouseClickHandler() ); // add handler
...so what is addMouseListener
method applied to if there is no object that is used to call the method?!
I hope what I mean is clear,