Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

How to add a MouseListener to a ScrollPane?

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
i done by the second way u explained

but it doesn't work
Lars
 
Lars Tode
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
i tried ur firstway too.
It also doesn't work.
Lars
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here some Code:

Here the Listener:

I get nothing, no error.
greetx
Lars
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic