Solved

Cascading mouseEntered events to sub components

Posted on 2004-09-16
9
329 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
Independent Software Vendors: 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 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
 

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

Suggested Solutions

Title # Comments Views Activity
pagenation logic how it is working in my code 1 80
program arguments vs VM arguments 4 56
ArrayList Adding/settign data 3 37
Java ArrayList and if statement 2 55
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

732 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