Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Cascading mouseEntered events to sub components

Posted on 2004-09-16
9
Medium Priority
?
332 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 37

Accepted Solution

by:
zzynx earned 200 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
 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

610 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