SwingUtilities.convertMouseEvent () sets a NOBUTTON button in Java7
It is known to be MouseEvent
lost in JScrollPane
, that is, not delivered to parents, this is not yet fixed an old bug:
https://forums.oracle.com/forums/thread.jspa?threadID=1362237
So, I used a workaround, catching the event in JScrollPane
with a temporary listener, and then dispatching the event to it, the parent should be handled with a real listener.
private class DispatchListener extends MouseAdapter {
@Override
public void mousePressed (MouseEvent me) {
System.out.println("dispatch.mousePressed: " + me.getButton());
preDispatchEvent(me);
}
@Override
public void mouseReleased (MouseEvent me) {
preDispatchEvent(me);
}
@Override
public void mouseDragged (MouseEvent me) {
System.out.println("dispatch.mouseDragged");
preDispatchEvent(me);
}
private void preDispatchEvent (MouseEvent me) {
JScrollPane pane = (JScrollPane)me.getSource();
MouseEvent newMe = SwingUtilities.convertMouseEvent(pane.getViewport(), me, tablePanel);
dispatchEvent(newMe);
}
}
It worked well, but people with Java 7 started complaining that clicking with the mouse did not cause a reaction in the application. After switching to Java 6, the problem was resolved, but now I tested Applet
and Chrome
forced me to update the plugin to version 7.
After debugging, I figured out that it convertMouseEvent
sets the button MouseEvent.NOBUTTON
regardless of the passed button
MouseEvent newMe = SwingUtilities.convertMouseEvent(pane.getViewport(), me, tablePanel);
From source to Eclipse
else {
newEvent = new MouseEvent(newSource,
sourceEvent.getID(),
sourceEvent.getWhen(),
sourceEvent.getModifiers(),
p.x,p.y,
sourceEvent.getXOnScreen(),
sourceEvent.getYOnScreen(),
sourceEvent.getClickCount(),
sourceEvent.isPopupTrigger(),
MouseEvent.NOBUTTON ); //!!!
}
return newEvent;
I don't know why it is implemented this way? Is this another bug or feature?
It seems I need to create my own object without calling SwingUtilities.convertMouseEvent
and set the button from sourceEvent.
source to share
This is an interesting problem. Using MouseEvent.NOBUTTON
in SwingUtilities.convertMouseEvent
seems very strange. However, I cannot reproduce the problem with Java 7; for me, the button
converted mouse event field is correct for Java 7 and 6 (using 64-bit 64-bit and 1.6-bit 64-bit 1.7.0-b147 on a Windows 7 laptop).
Debugging through the constructor MouseEvent
I saw that when the field is button
initially set to zero, the call setNewModifiers
at the end of the constructor changes button
based on the field modifiers
(which gets its value from sourceEvent.getModifiers
when the transformed mouse event is built in SwingUtilities.convertMouseEvent
).
The converted mouse events from one click looks like this (for Java 7):
java.awt.event.MouseEvent[MOUSE_PRESSED,(185,175),absolute(593,305),button=1,modifiers=Button1,extModifiers=Button1,clickCount=1] on javax.swing.JPanel[,0,0,381x259,layout=java.awt.FlowLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=]
java.awt.event.MouseEvent[MOUSE_RELEASED,(185,175),absolute(593,305),button=1,modifiers=Button1,clickCount=1] on javax.swing.JPanel[,0,0,381x259,layout=java.awt.FlowLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=]
Answering your questions : I think that's MouseEvent.NOBUTTON
passed to the constructor MouseEvent
, because the field value button
will be determined by the field value modifiers
anyway. On my laptop, everything works as I expected for Java 7 and 6, so it doesn't look like it to me. Is your application running in a browser and does this affect how mouse events are translated? Have you created a job to solve the problem, or could you share some of the applet's code?
Other types of events
The first two cases that are handled SwingUtilities.convertMouseEvent
create an object MouseWheelEvent
or MenuDragMouseEvent
. Both constructors will (eventually) call the constructor MouseEvent
with a parameter MouseEvent.NOBUTTON
. (Looking at these events (without conversion) on my laptop, the field is button
MouseWheelEvent
always zero (my mouse only has one wheel). For MenuDragMouseEvent
objects, it button
is one for menuDragMouseReleased
and is zero otherwise, while the field is modifiers
always equal BUTTON1_MASK
.) So it looks like , which is MouseEvent.NOBUTTON
quite popular as a parameter for constructors MouseEvent
.
source to share
See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7181403 - it looks like convertMouseEvent () hasn't changed recently, so other code changes should be responsible for the change you see in Java 7 (I I can reproduce the problem here also with 1.7.0_21).
Anyway, it looks like the problem has been resolved in Java 8.
source to share