Solved

Cascading mouseEntered events to sub components

Posted on 2004-09-16
9
327 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Should localization be done inside spring controller 5 26
javap not working 8 46
how to see all occupied ports on windows 10 laptop 15 65
hibernate example using maven 12 42
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…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

810 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