473,396 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 6184
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Stephen Williams | last post by:
I am migrating a VB 6 PictureBox control to VB.NET. In VB 6, this control modified its border style during the mouse down and up events to provide user feedback that it was selected. Once...
1
by: Jean-Gael GRICOURT | last post by:
I am trying to capture mouse events when entering and leaving a DIV layer. This test code works fine with IE 6.0 and Opera 7.21 but fails with Mozilla/Netscape. The strange thing is that the mouse...
3
by: red | last post by:
mouse events when the mouse is on a "child control" hi everyone; my problem: I have a userControl in this usercontrol, I have a child control (a button) when the mouse moves over the...
3
by: mitsura | last post by:
Hi, I have included a small listing. The test program opens a panel and show a bitmap. What I want is to when the mouse is over the bitmap panel, I want to trap the left mouse click. The...
5
by: Bill Henning | last post by:
Does anyone know a good method of preventing keyboard and mouse events from interrupting processing? My situation is: 1) I need to track and handle all key and mouse events 2) I need to perform...
3
by: Rick Strahl [MVP] | last post by:
I'm working on an app that's using the WebBrowser control. I got the control working fine, hooking to the document object. But I've run into a major issue with hooking the Document events....
3
by: Charles Law | last post by:
In a user control, is it possible to replace the default mouse events with my own? In particular, I want the consumer of my control to get MouseMove events when the mouse is over my control, so...
2
by: bretth | last post by:
In a VB.Net Windows Forms application, I have a user control that handles mouse events. Another section of code programmatically adds a label to the control. I would like label to ignore all...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.