Mouse Events not being registered

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Champaign

    Mouse Events not being registered

    Hi all,

    I'm working on an educational applet for a child with special needs.

    He's got a bit of a trick to make my life more difficult... To interact
    with the applet he needs to click on buttons, which is fine most of the
    time (he comes from a Mac environment, so I accept mouse clicks from the
    right or left button when he's working on the PC). But every once in a
    while, he'll press and hold the right mouse button, move onto a JButton,
    then left-click, which my applet doesn't seem to like (it doesn't
    register that the mouse has been clicked).

    My *GUESS* of what the problem is is that the mouse event begins when he
    is off the JButton, so its not registered with the JButton's mouse
    listener (although the Panel it begins on isn't a mouse listener).
    When he moves onto the JButton and clicks the new "Mouse Presed" event
    isn't passed to the JButton's listener. This doesn't make complete sense
    as the JButton DOES detect when the cursor moves on or off the JButton (so
    its still registering *some* new mouse events).

    The only way I currently see around this is to make the Panel the button
    was added to a mouse listener, then pass clicks to the appropriate buttons
    depending on the cursor's location. Does anyone have any ideas for a
    cleaner way to fix this? The user is non-verbal, so telling him not to do
    this isn't an option.

    My apologies for the lengthy explaination. I've searched for this, but
    couldn't find anyone experiencing something similar. If you have any
    suggestions for search terms that I might have missed (my apologies in
    advance), I'd love to hear them.

    Thanks,

    John

  • hiwa

    #2
    Re: Mouse Events not being registered

    John Champaign <jchampai@hoppe r.math.uwaterlo o.ca> wrote in message news:<Pine.SOL. 4.44.0402220216 [email protected] ath.uwaterloo.c a>...[color=blue]
    > Hi all,
    >
    > I'm working on an educational applet for a child with special needs.
    >
    > He's got a bit of a trick to make my life more difficult... To interact
    > with the applet he needs to click on buttons, which is fine most of the
    > time (he comes from a Mac environment, so I accept mouse clicks from the
    > right or left button when he's working on the PC). But every once in a
    > while, he'll press and hold the right mouse button, move onto a JButton,
    > then left-click, which my applet doesn't seem to like (it doesn't
    > register that the mouse has been clicked).
    >
    > My *GUESS* of what the problem is is that the mouse event begins when he
    > is off the JButton, so its not registered with the JButton's mouse
    > listener (although the Panel it begins on isn't a mouse listener).
    > When he moves onto the JButton and clicks the new "Mouse Presed" event
    > isn't passed to the JButton's listener. This doesn't make complete sense
    > as the JButton DOES detect when the cursor moves on or off the JButton (so
    > its still registering *some* new mouse events).
    >
    > The only way I currently see around this is to make the Panel the button
    > was added to a mouse listener, then pass clicks to the appropriate buttons
    > depending on the cursor's location. Does anyone have any ideas for a
    > cleaner way to fix this? The user is non-verbal, so telling him not to do
    > this isn't an option.
    >
    > My apologies for the lengthy explaination. I've searched for this, but
    > couldn't find anyone experiencing something similar. If you have any
    > suggestions for search terms that I might have missed (my apologies in
    > advance), I'd love to hear them.
    >
    > Thanks,
    >
    > John[/color]

    If it's me, I would try implement ALMOST every method defined in the
    MouseInputListe ner and register it onto the JButton in question. Then
    I would do some trial and error.

    Comment

    • John Champaign

      #3
      Re: Mouse Events not being registered

      On 22 Feb 2004, hiwa wrote:
      [color=blue]
      > John Champaign <jchampai@hoppe r.math.uwaterlo o.ca> wrote in message news:<Pine.SOL. 4.44.0402220216 [email protected] ath.uwaterloo.c a>...[color=green]
      > > My *GUESS* of what the problem is is that the mouse event begins when he
      > > is off the JButton, so its not registered with the JButton's mouse
      > > listener (although the Panel it begins on isn't a mouse listener).
      > > When he moves onto the JButton and clicks the new "Mouse Presed" event
      > > isn't passed to the JButton's listener. This doesn't make complete sense
      > > as the JButton DOES detect when the cursor moves on or off the JButton (so
      > > its still registering *some* new mouse events).[/color][/color]
      [color=blue]
      > If it's me, I would try implement ALMOST every method defined in the
      > MouseInputListe ner and register it onto the JButton in question. Then
      > I would do some trial and error.[/color]

      I had pretty well done this, but I added the mouseMoved and mouseDragged
      methods required to conform to MouseInputListe ner (instead of just those
      to conform to MouseListener) and I get similar behaviour. When the mouse
      enters the JButton it registers everything fine, including the mouseMoved
      and mouseDragged, but if one of the buttons is pressed when you enter the
      JButton it will only register the mouseEntered and mouseExited events.

      Interestingly it does register events that begin in the JButton (e.g. when
      I pressed, moved out of the button and released, the JButton registered
      the mouse released).

      Would it be worthwhile to post (working) relevant code or should I just
      accept this is how the event model works and try to work around it?

      Thanks for your suggestion hiwa,

      John

      BTW - My apologies for not originally posting this in comp.lang.java. gui
      which is probably where it belongs

      Comment

      • Tom N

        #4
        Re: Mouse Events not being registered

        I tried a button next to a label, both with mouse listeners and an action on
        the button.

        If you press the mouse down outside the button (in the label in my code),
        than drag the mouse into the button and release it, then the mouseReleased
        event goes to the same place as the mousePressed event went (i.e. the
        label - where you pressed the mouse button, not where you released it).

        This seems to preclude simpling adding a mouse listener on the button that
        listens for mouse pressed and mouse released.
        I also tried a mouse listener on the panel and it behaves in exactly the
        same way as the label (i.e. the mouse released event goes to the panel if
        the mouse pressed event went to the panel).

        However the following seems to do what you want

        "The Glass Pane
        If you make the glass pane visible, then it's like a sheet of glass over all
        the other parts of the root pane. It's completely transparent unless you
        implement the glass pane's paintComponent method so that it does something,
        and it intercepts input events for the root pane.

        The glass pane is useful when you want to be able to catch events or paint
        over an area that already contains one or more components. For example, you
        can deactivate mouse events for a multi-component region by having the glass
        pane intercept the events. Or you can display an image over multiple
        components using the glass pane."



        "John Champaign" wrote:[color=blue]
        > I'm working on an educational applet for a child with special needs.
        >
        > He's got a bit of a trick to make my life more difficult... To interact
        > with the applet he needs to click on buttons, which is fine most of the
        > time (he comes from a Mac environment, so I accept mouse clicks from the
        > right or left button when he's working on the PC). But every once in a
        > while, he'll press and hold the right mouse button, move onto a JButton,
        > then left-click, which my applet doesn't seem to like (it doesn't
        > register that the mouse has been clicked).
        >
        > My *GUESS* of what the problem is is that the mouse event begins when he
        > is off the JButton, so its not registered with the JButton's mouse
        > listener (although the Panel it begins on isn't a mouse listener).
        > When he moves onto the JButton and clicks the new "Mouse Presed" event
        > isn't passed to the JButton's listener. This doesn't make complete sense
        > as the JButton DOES detect when the cursor moves on or off the JButton (so
        > its still registering *some* new mouse events).
        >
        > The only way I currently see around this is to make the Panel the button
        > was added to a mouse listener, then pass clicks to the appropriate buttons
        > depending on the cursor's location. Does anyone have any ideas for a
        > cleaner way to fix this? The user is non-verbal, so telling him not to do
        > this isn't an option.
        >
        > My apologies for the lengthy explaination. I've searched for this, but
        > couldn't find anyone experiencing something similar. If you have any
        > suggestions for search terms that I might have missed (my apologies in
        > advance), I'd love to hear them.[/color]


        Comment

        • Tom N

          #5
          Re: Mouse Events not being registered

          "Tom N" wrote:[color=blue]
          > "The Glass Pane
          > If you make the glass pane visible, then it's like a sheet of glass over[/color]
          all[color=blue]
          > the other parts of the root pane. It's completely transparent unless you
          > implement the glass pane's paintComponent method so that it does[/color]
          something,[color=blue]
          > and it intercepts input events for the root pane.
          >
          > The glass pane is useful when you want to be able to catch events or paint
          > over an area that already contains one or more components. For example,[/color]
          you[color=blue]
          > can deactivate mouse events for a multi-component region by having the[/color]
          glass[color=blue]
          > pane intercept the events. Or you can display an image over multiple
          > components using the glass pane."
          >
          > http://java.sun.com/docs/books/tutor.../rootpane.html[/color]

          Here's some code based on the above. When the user presses the mouse down,
          the event is discarded. When they release the mouse button over a JButton,
          the JButton gets a mouse pressed then a mouse released, and the JButton's
          action is invoked.

          In the JFrame...

          getGlassPane(). addMouseListene r(new MA("Glasspane" , contentPane));
          getGlassPane(). setVisible(true );
          button.setActio n(new AbstractAction( )
          {
          public void actionPerformed (ActionEvent e)
          {
          System.out.prin tln("Button pressed");
          System.out.flus h();
          }
          }
          );

          class MA extends MouseAdapter
          {
          private String name;
          private Container container;
          MA(String name, Container container)
          {
          this.name = name;
          this.container = container;
          }
          public void mouseClicked(Mo useEvent e)
          {
          redispatchMouse Event(e, " mouseClicked ");
          }
          public void mouseEntered(Mo useEvent e)
          {
          redispatchMouse Event(e, " mouseEntered ");
          }
          public void mouseExited(Mou seEvent e)
          {
          redispatchMouse Event(e, " mouseExited ");
          }
          public void mousePressed(Mo useEvent e)
          {
          redispatchMouse Event(e, " mousePressed ");
          }
          public void mouseReleased(M ouseEvent e)
          {
          redispatchMouse Event(e, " mouseReleased ");
          }
          private void redispatchMouse Event(MouseEven t e, String type)
          {
          System.out.prin tln(name + type + e.getWhen());
          System.out.flus h();
          if (container == null)
          return;

          if (e.getID() == e.MOUSE_PRESSED )
          {
          System.out.prin tln("Ignored");
          System.out.flus h();
          return;
          }

          Point glassPanePoint = e.getPoint();
          Container container = contentPane;
          Point containerPoint = SwingUtilities. convertPoint(
          getGlassPane(),
          glassPanePoint,
          contentPane);

          if (containerPoint .y < 0)
          { //we're not in the content pane
          //Could have special code to handle mouse events over
          //the menu bar or non-system window decorations, such as
          //the ones provided by the Java look and feel.
          }
          else
          {
          //The mouse event is probably over the content pane.
          //Find out exactly which component it's over.
          Component component =
          SwingUtilities. getDeepestCompo nentAt(
          container,
          containerPoint. x,
          containerPoint. y);

          if (component instanceof JButton)
          {
          //Forward events over JButtons
          Point componentPoint = SwingUtilities. convertPoint(
          getGlassPane(),
          glassPanePoint,
          component);
          if (e.getID() == e.MOUSE_RELEASE D)
          {
          component.dispa tchEvent(new MouseEvent(comp onent,
          e.MOUSE_PRESSED ,
          e.getWhen(),
          e.getModifiers( ),
          componentPoint. x,
          componentPoint. y,
          e.getClickCount (),
          e.isPopupTrigge r()));
          }
          component.dispa tchEvent(new MouseEvent(comp onent,
          e.getID(),
          e.getWhen(),
          e.getModifiers( ),
          componentPoint. x,
          componentPoint. y,
          e.getClickCount (),
          e.isPopupTrigge r()));
          }
          }
          }
          }


          Comment

          • John Champaign

            #6
            Re: Mouse Events not being registered

            Hi Tom,

            That does seem to be exactly what I want, and seems to be a fairly clean
            way of doing it. Thanks so much!

            John

            On Tue, 24 Feb 2004, Tom N wrote:
            [color=blue]
            > "Tom N" wrote:[color=green]
            > > "The Glass Pane
            > > If you make the glass pane visible, then it's like a sheet of glass over[/color]
            > all[color=green]
            > > the other parts of the root pane. It's completely transparent unless you
            > > implement the glass pane's paintComponent method so that it does[/color]
            > something,[color=green]
            > > and it intercepts input events for the root pane.
            > >
            > > The glass pane is useful when you want to be able to catch events or paint
            > > over an area that already contains one or more components. For example,[/color]
            > you[color=green]
            > > can deactivate mouse events for a multi-component region by having the[/color]
            > glass[color=green]
            > > pane intercept the events. Or you can display an image over multiple
            > > components using the glass pane."
            > >
            > > http://java.sun.com/docs/books/tutor.../rootpane.html[/color]
            >
            > Here's some code based on the above. When the user presses the mouse down,
            > the event is discarded. When they release the mouse button over a JButton,
            > the JButton gets a mouse pressed then a mouse released, and the JButton's
            > action is invoked.
            >
            > In the JFrame...
            >
            > getGlassPane(). addMouseListene r(new MA("Glasspane" , contentPane));
            > getGlassPane(). setVisible(true );
            > button.setActio n(new AbstractAction( )
            > {
            > public void actionPerformed (ActionEvent e)
            > {
            > System.out.prin tln("Button pressed");
            > System.out.flus h();
            > }
            > }
            > );
            >
            > class MA extends MouseAdapter
            > {
            > private String name;
            > private Container container;
            > MA(String name, Container container)
            > {
            > this.name = name;
            > this.container = container;
            > }
            > public void mouseClicked(Mo useEvent e)
            > {
            > redispatchMouse Event(e, " mouseClicked ");
            > }
            > public void mouseEntered(Mo useEvent e)
            > {
            > redispatchMouse Event(e, " mouseEntered ");
            > }
            > public void mouseExited(Mou seEvent e)
            > {
            > redispatchMouse Event(e, " mouseExited ");
            > }
            > public void mousePressed(Mo useEvent e)
            > {
            > redispatchMouse Event(e, " mousePressed ");
            > }
            > public void mouseReleased(M ouseEvent e)
            > {
            > redispatchMouse Event(e, " mouseReleased ");
            > }
            > private void redispatchMouse Event(MouseEven t e, String type)
            > {
            > System.out.prin tln(name + type + e.getWhen());
            > System.out.flus h();
            > if (container == null)
            > return;
            >
            > if (e.getID() == e.MOUSE_PRESSED )
            > {
            > System.out.prin tln("Ignored");
            > System.out.flus h();
            > return;
            > }
            >
            > Point glassPanePoint = e.getPoint();
            > Container container = contentPane;
            > Point containerPoint = SwingUtilities. convertPoint(
            > getGlassPane(),
            > glassPanePoint,
            > contentPane);
            >
            > if (containerPoint .y < 0)
            > { //we're not in the content pane
            > //Could have special code to handle mouse events over
            > //the menu bar or non-system window decorations, such as
            > //the ones provided by the Java look and feel.
            > }
            > else
            > {
            > //The mouse event is probably over the content pane.
            > //Find out exactly which component it's over.
            > Component component =
            > SwingUtilities. getDeepestCompo nentAt(
            > container,
            > containerPoint. x,
            > containerPoint. y);
            >
            > if (component instanceof JButton)
            > {
            > //Forward events over JButtons
            > Point componentPoint = SwingUtilities. convertPoint(
            > getGlassPane(),
            > glassPanePoint,
            > component);
            > if (e.getID() == e.MOUSE_RELEASE D)
            > {
            > component.dispa tchEvent(new MouseEvent(comp onent,
            > e.MOUSE_PRESSED ,
            > e.getWhen(),
            > e.getModifiers( ),
            > componentPoint. x,
            > componentPoint. y,
            > e.getClickCount (),
            > e.isPopupTrigge r()));
            > }
            > component.dispa tchEvent(new MouseEvent(comp onent,
            > e.getID(),
            > e.getWhen(),
            > e.getModifiers( ),
            > componentPoint. x,
            > componentPoint. y,
            > e.getClickCount (),
            > e.isPopupTrigge r()));
            > }
            > }
            > }
            > }
            >
            >
            >[/color]

            Comment

            Working...