11 Replies Latest reply on Sep 30, 2010 6:08 PM by camickr

    missing MouseExit events

    casadelgato-JavaNet
      We have a little JDialog as an info popup window. We would like it to disappear when the mouse moves off the component that displayed it.
      So I've added a MouseListener to the parent for MouseExited events.
      What I've noticed is that you can move the mouse off the parent and NOT receive a MouseExited message.
      This happens sometimes if you just move the mouse quickly, and almost always when the edge of the parent is the edge of the Application Window.
      I also tried just tracking MouseMoved events, and checking to see if the cursor is no longer over the parent. MouseMove events don't happen when the cursor moves off the app. So that doesn't handle that case either.

      Any suggestion on what I SHOULD be doing to detect the mouse moving off my Component?
        • 1. Re: missing MouseExit events
          camickr
          Where is your SSCCE demonstrating the problem?
          • 2. Re: missing MouseExit events
            casadelgato-JavaNet
            So, your response is that MouseListener's watching for MouseExited events is the correct way to do this?
            Sure would have been nice for you to state that.

            Now, since I've been told that that IS the way this is supposed to be done, and I haven't missed some other obvious API (which does happen, and which was why I asked the question in the first place.), I'll try and make a SS(whatever).
            • 3. Re: missing MouseExit events
              darrylburke
              CasaDelGato wrote:
              So, your response is that MouseListener's watching for MouseExited events is the correct way to do this?
              Sure would have been nice for you to state that.
              No, his response was
              camickr wrote:
              Where is your SSCCE demonstrating the problem?
              Now, since I've been told that that IS the way this is supposed to be done, and I haven't missed some other obvious API (which does happen, and which was why I asked the question in the first place.), I'll try and make a SS(whatever).
              Way to go, poking fun at useful advice. Best of luck getting the help you need.

              db
              • 4. Re: missing MouseExit events
                casadelgato-JavaNet
                What useful advice?
                I asked if there is a known technique for detecting the mouse leaving the window in all cases.
                He responded with a message saying that I should write a new program showing all the things I've tried.
                There was no information in that message.

                If I had just posted a message saying "How do I detect that the mouse has moved off a panel", with NO mention of having tried a few things, would that have gotten a different answer?

                What I have discovered here is that if you mention AT ALL that you have actually TRIED some things, and even if they are blatantly the WRONG things, you will be required to post code. (that must meet specific peoples design guidelines, heaven help you if you use NetBeans to generate all the boilerplate for you.)

                So, I'll probably create a new userid, and from now on only post questions of the form "I'd like to do xxxx, how should I do it?"
                so that I can actually GET some useful replies.

                I (obviously) do NOT know the full Swing API in detail. I've often discovered that I'm doing something the hard way, when there is an easy API to do the same thing. So I generally ask a question to discover if there IS some easy technique that I don't currently know about.
                If there isn't, a polite "what you are doing should work, so you'll need to post a SSCCE so we can help find the problem" would be a much more polite answer.
                • 5. Re: missing MouseExit events
                  camickr
                  If you post a question like "How do I determine when a mouse leaves a component", then I will answer it with a link to the Swing tutorial on "How to Write a Mouse Listener". I will NOT give you the code. I expect you to read the tutorial and make an effort to solve the problem once you've been pointed in the right direction. You should be able to do simple things like scan the API and read the Swing tutorial.

                  If you then read the tutorial and test how to use the mouse listener and the code does not work the way you expect. The you should post your SSCCE showing how you tested the code. Then we can answer:

                  a) it you are using the methods correctly. If not, then we might be able to provide a solution showing the proper use o the method/class.

                  b) if the code looks reasonable, then maybe it is a bug or a feature. In this case if we find the question interesting, we may decide to do some debugging on our own to try and find an alternate solution. This is where the SSCCE comes in handy because we don't have to create a test program from scratch which saves us time.

                  Our time is important and most of us choose to help those who understand this concept. After multiple attempts to get you to post a SSCCE you still don't understand this, which is why I don't attempt to provide a detailed answer.

                  You get what you give. If you give a general question you will get a general answer that generally refers to the API or a tutorial. If you give test code you will generally get working code back. Its a simple concept.
                  • 6. Re: missing MouseExit events
                    casadelgato-JavaNet
                    You said: "If you post a question like "How do I determine when a mouse leaves a component", then I will answer it with a link to the Swing tutorial on "How to Write a Mouse Listener".

                    Yes, EXACTLY!
                    So, if your original response to my question had been "That should work, please post a SSCCE" I would have been happy, and spent time building an SSCCE.
                    But you didn't tell me that what I was doing SHOULD work. As I said, I've found several times that I was going down the completely wrong path, and what I was trying SHOULDN'T work and something else would have.
                    That's why I'd like at least an indication that what I mentioned SHOULD work or SHOULDN'T work.
                    Just that tiny bit of information makes a big difference.
                    (and yes, I've seen the same thing on other questions in my large number of searches on the forums. Someone says "I'm trying X to do Y", and they'll get a reply stating that either A) Don't do that, use Z instead, or B) That should work, there must be a problem with your code.)
                    • 7. Re: missing MouseExit events
                      casadelgato-JavaNet
                      And now that I know that the MouseExited message is supposed to work, I did a bunch of testing. (including writing a SSCCE - which I won't bother posting since it just verified that MouseExited does work.)

                      I had to trace through the code, and found that someone had "fixed" a problem - introducing the new problem.
                      The old problem was that the info popup would disappear if the mouse moved from the parent to over the info popup. Not what we wanted.
                      So a check was made in the code to see if the Point from MouseExit was still in the parent component. (the popup comes up over part of the parent). If so, it ignored the MouseExit since it was still over the parent.
                      This kinda worked - since the info popup was also dismissed when the Mouse moved off the info popup.

                      Where this didn't work was if the info popup was over a JTable, which did not have enough rows to fill it's area.
                      With this case, you get a MouseExited message when the cursor moves below the last row of data, even though it's still over the JTable. So the mouse wasn't over our info popup, but we ignored the MouseExited since it was still over the table. So the MouseExited got lost.

                      (I do think it's kind of odd that you get a MouseExited when the cursor moves off the last row of data, but is still over the table.)

                      As I said originally, just a little statement that "Yes, your use of MouseExit IS the way to do that, and it should work" would have answered my question quite nicely.
                      • 8. Re: missing MouseExit events
                        pietblok
                        >
                        (I do think it's kind of odd that you get a MouseExited when the cursor moves off the last row of data, but is still over the table.)
                        Are you sure that the mouse is indeed over the JTable when below the last row? If inside a JScrollPane, it may be the JViewPort that is entered by the mouse. If the JTable is inside another component, it depends on the layout manager if the JTable totally covers its parent.

                        I'd very much like to see a SSCCE that shows the phenomenon you describe.

                        Piet
                        • 9. Re: missing MouseExit events
                          walterln
                          CasaDelGato wrote:
                          (I do think it's kind of odd that you get a MouseExited when the cursor moves off the last row of data, but is still over the table.)
                          It's over the viewport of the scroll pane unless you set JTable#setFillsViewportHeight(true).
                          • 10. Re: missing MouseExit events
                            casadelgato-JavaNet
                            You are right! I just checked the (old) code again, and it was doing a .contains() on the panel that enclosed the JTable/JscrollPane - after setting the listener on the Table.
                            i.e. a mess.
                            • 11. Re: missing MouseExit events
                              camickr
                              So, if your original response to my question had been "That should work, please post a SSCCE" I would have been happy, and spent time building an SSCCE.
                              No, you still don't get it. We are not here to confirm how you think the API works. We are here to help you when you have a problem.

                              We should not have to waste time reading a question and then determine that a SSCCE should be posted and then return hours later to rered the question with a SSCCE. All the information should be included up front.
                              I had to trace through the code, and found that someone had "fixed" a problem - introducing the new problem.
                              Exactly. We don't have time to waste because of coding problems you introduce. Which is why you simplify your requirement to the simplest code possible to see if it works the way your think it does. Then if it works you slowly add back in customizations you have made.

                              Nowhere in your original question did you mention a JTable or scrollpane so we have no idea the context of your overall question, which again is why you need a SSCCE to demonstrate the problem.