Solved

Cascading mouseEntered events to sub components

Posted on 2004-09-16
9
326 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JDeveloper 12c for 32 bit 4 71
Problem to start Neon 20 88
object oriented programming comparison 5 56
fibonacci ten numbers 4 30
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

867 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

15 Experts available now in Live!

Get 1:1 Help Now