Solved

Cascading mouseEntered events to sub components

Posted on 2004-09-16
9
325 Views
Last Modified: 2008-03-06
Hi all,

I have a JPanel which contains other components.  Whenever the user puts the mouse anywhere in the JPanel, I want it to fire a mouseEntered event.

I've almost got it working, but often the event fails to fire if the mouse is moved quickly into the JPanel.  I suspect this is because if the mouse goes from outside the JPanel to inside one of the sub components, then the event doesn't fire (am I right ?).

Is there a way round this?
0
Comment
Question by:agrees
  • 5
  • 4
9 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 12075144
If you want to capture mouse events on the sub components, you have to perform

           addMouseListener( yourMouseListener )

on your sub components too
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12075212
>> I suspect this is because if the mouse goes from outside the JPanel to inside one of the sub components, then the event doesn't fire (am I right ?).
Right. Entering the subcomponent is NOT the same as entering the JPanel
0
 

Author Comment

by:agrees
ID: 12075624
The problem is, in the mouseEntered() method I want to use data contained within the panel object itself, like this:

 public void mouseEntered(MouseEvent e) {
    MyCustomPanel panel = (myCustomPanel )e.getSource();
    customData data = panel.getData();
    anotherPanel.displayData(data);
}

So if I add MouseListeners to the sub components then e.getSource() will return one of the panel's sub components, in which case the above won't be possible.  I can see a possible hack involving something like:

if (e.getSource() == isInstanceOf(MyCustomPanel ) { do same as above}
else {  
  MyCustomPanel panel = e.getSource().getParent();
}

but this seems a bit messy to me... is there a better way?
0
 
LVL 37

Accepted Solution

by:
zzynx earned 50 total points
ID: 12075766
>> is there a better way?
Yes, pass your MyCustomPanel as a parameter to your MouseListener.


public class MyMouseListener extends MouseAdapter {

     private MyCustomPanel thePanel = null;

     public MyMouseListener(MyCustomPanel panel) {
           super();
           thePanel = panel;
     }

     public void mouseEntered(MouseEvent e) {
         customData data = thePanel.getData();
         anotherPanel.displayData(data);
     }
   
}

and then of course:

       yourComponent.addMouseListener( new MyMouseListener(myCustomPanel) );
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:agrees
ID: 12075832
Ah ok I've had my JFrame implementing Mouselistener rather than creating a MouseAdapter class, I'll give that a go!
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12075874
>> I've had my JFrame implementing Mouselistener

Then you already have that myCustomPanel available, haven't you?
0
 

Author Comment

by:agrees
ID: 12076022
I do but it is one of a very large linked list of them
0
 

Author Comment

by:agrees
ID: 12076339
Ok I think I've got it sorted.  Each 'myCustomPanel' now has its own MouseAdapter as an inner class, which it adds as a listener to each of its sub components.  It seems to work perfectly but please let me know if it's a bad idea for some reason!  

Thanks for you help :)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12078246
Thanks for accepting

>> It seems to work perfectly but please let me know if it's a bad idea for some reason!
No, that's OK.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now