472,111 Members | 2,027 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,111 software developers and data experts.

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

Jul 17 '05 #1
5 6014
John Champaign <jc******@hopper.math.uwaterloo.ca> wrote in message news:<Pi**************************************@hop per.math.uwaterloo.ca>...
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


If it's me, I would try implement ALMOST every method defined in the
MouseInputListener and register it onto the JButton in question. Then
I would do some trial and error.
Jul 17 '05 #2
On 22 Feb 2004, hiwa wrote:
John Champaign <jc******@hopper.math.uwaterloo.ca> wrote in message news:<Pi**************************************@hop per.math.uwaterloo.ca>...
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).
If it's me, I would try implement ALMOST every method defined in the
MouseInputListener and register it onto the JButton in question. Then
I would do some trial and error.


I had pretty well done this, but I added the mouseMoved and mouseDragged
methods required to conform to MouseInputListener (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

Jul 17 '05 #3
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."

http://java.sun.com/docs/books/tutor.../rootpane.html

"John Champaign" wrote:
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.

Jul 17 '05 #4
"Tom N" wrote:
"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."

http://java.sun.com/docs/books/tutor.../rootpane.html


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().addMouseListener(new MA("Glasspane", contentPane));
getGlassPane().setVisible(true);
button.setAction(new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("Button pressed");
System.out.flush();
}
}
);

class MA extends MouseAdapter
{
private String name;
private Container container;
MA(String name, Container container)
{
this.name = name;
this.container = container;
}
public void mouseClicked(MouseEvent e)
{
redispatchMouseEvent(e, " mouseClicked ");
}
public void mouseEntered(MouseEvent e)
{
redispatchMouseEvent(e, " mouseEntered ");
}
public void mouseExited(MouseEvent e)
{
redispatchMouseEvent(e, " mouseExited ");
}
public void mousePressed(MouseEvent e)
{
redispatchMouseEvent(e, " mousePressed ");
}
public void mouseReleased(MouseEvent e)
{
redispatchMouseEvent(e, " mouseReleased ");
}
private void redispatchMouseEvent(MouseEvent e, String type)
{
System.out.println(name + type + e.getWhen());
System.out.flush();
if (container == null)
return;

if (e.getID() == e.MOUSE_PRESSED)
{
System.out.println("Ignored");
System.out.flush();
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.getDeepestComponentAt(
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_RELEASED)
{
component.dispatchEvent(new MouseEvent(component,
e.MOUSE_PRESSED,
e.getWhen(),
e.getModifiers(),
componentPoint.x,
componentPoint.y,
e.getClickCount(),
e.isPopupTrigger()));
}
component.dispatchEvent(new MouseEvent(component,
e.getID(),
e.getWhen(),
e.getModifiers(),
componentPoint.x,
componentPoint.y,
e.getClickCount(),
e.isPopupTrigger()));
}
}
}
}
Jul 17 '05 #5
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:
"Tom N" wrote:
"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."

http://java.sun.com/docs/books/tutor.../rootpane.html


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().addMouseListener(new MA("Glasspane", contentPane));
getGlassPane().setVisible(true);
button.setAction(new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("Button pressed");
System.out.flush();
}
}
);

class MA extends MouseAdapter
{
private String name;
private Container container;
MA(String name, Container container)
{
this.name = name;
this.container = container;
}
public void mouseClicked(MouseEvent e)
{
redispatchMouseEvent(e, " mouseClicked ");
}
public void mouseEntered(MouseEvent e)
{
redispatchMouseEvent(e, " mouseEntered ");
}
public void mouseExited(MouseEvent e)
{
redispatchMouseEvent(e, " mouseExited ");
}
public void mousePressed(MouseEvent e)
{
redispatchMouseEvent(e, " mousePressed ");
}
public void mouseReleased(MouseEvent e)
{
redispatchMouseEvent(e, " mouseReleased ");
}
private void redispatchMouseEvent(MouseEvent e, String type)
{
System.out.println(name + type + e.getWhen());
System.out.flush();
if (container == null)
return;

if (e.getID() == e.MOUSE_PRESSED)
{
System.out.println("Ignored");
System.out.flush();
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.getDeepestComponentAt(
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_RELEASED)
{
component.dispatchEvent(new MouseEvent(component,
e.MOUSE_PRESSED,
e.getWhen(),
e.getModifiers(),
componentPoint.x,
componentPoint.y,
e.getClickCount(),
e.isPopupTrigger()));
}
component.dispatchEvent(new MouseEvent(component,
e.getID(),
e.getWhen(),
e.getModifiers(),
componentPoint.x,
componentPoint.y,
e.getClickCount(),
e.isPopupTrigger()));
}
}
}
}


Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Stephen Williams | last post: by
1 post views Thread by Jean-Gael GRICOURT | last post: by
3 posts views Thread by mitsura | last post: by
3 posts views Thread by Rick Strahl [MVP] | last post: by
3 posts views Thread by Charles Law | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.