aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes How to add a MouseListener to a ScrollPane? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to add a MouseListener to a ScrollPane?" Watch "How to add a MouseListener to a ScrollPane?" New topic
Author

How to add a MouseListener to a ScrollPane?

Lars Tode
Ranch Hand

Joined: Feb 07, 2002
Posts: 45
Hi,
if tried to add a MouseListener to a ScrollPane by this way:
ScrollPanemyScrollPane= new ScrollPane();
myScrollPane.addMouseListener(new MouseListener());
Why does it not work? Whats my mistake?
Thanks
Lars
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
MouseListener is an interface, it doesnt have any implementation of methods so u cant create objects from an interface.
you have some options:
1) make a class that implements MouseListener and override all the methods there.
then create an object from that class and pass it as parameter to the addMouseListener fucntion.
2) make a class that exetnds MouseAdapter and then u can override only the methods u want to. (u dont need to override all of them like before).
3) make an annonymous class by either implementning the interface MouseListener or extending MouseAdpater.

for the last option (its the easyest but not necessarily the best) ill give u a short example)
yourscrollpane.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
//do your stuff here
}
});
Lars Tode
Ranch Hand

Joined: Feb 07, 2002
Posts: 45
Hi,
i done by the second way u explained

but it doesn't work
Lars
Lars Tode
Ranch Hand

Joined: Feb 07, 2002
Posts: 45
Hi,
i tried ur firstway too.
It also doesn't work.
Lars
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
like this:
public class MyListener extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
// your code here
}
}
in your program:
MyListener x=new MyListener();
yourpanel.addMouseListener(x);

what do u mean it doesnt work?
what error are u getting?
post some code.
Lars Tode
Ranch Hand

Joined: Feb 07, 2002
Posts: 45
Here some Code:

Here the Listener:

I get nothing, no error.
greetx
Lars
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
the code u posted works fine for me.
i just added, setVisible(true) and validate() which im sure u have .
it works pefect, so i dont know what wrong in your side.
as a side note, its not good idea to mix swing and awt. i noticed u used getContentPAne() so i assume u have a JFrame. in that case it is always best to use JScrollPane instead of scrollPane but like i said it isnt your problem in this case.
Lars Tode
Ranch Hand

Joined: Feb 07, 2002
Posts: 45
Hi,
it seems to be that i've combinate the 2 diffrent types, after i changed from ScrollPane to JScrollPane i only saw a very small Pane (only a pixel) but when i clickt on it, i get the message.
Now i only must find out, how i get the old size back.
Thanks 4 ur help
Lars
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to add a MouseListener to a ScrollPane?