I have written a program with two components that use mouseListeners:
It all works fine under Windows. I tried to port the program to the Mac today, but there the MouseClicked event never gets fired. I put the MouseClicked event in ResizeRectangle, but there it also wont get fired. I put the MouseReleased event in IconGrid and that does get fired. So the problem really seems to be with the mouseClicked event.
I read another article that stated that on the Mac even a small pixel change between mouse press and mouse release would result in MouseClicked not being fired. But even when I click with my mouse in the air (so no chance of moving the mouse between the press and the release), the mouseClicked event doesnt get fired.
Anyone else had this problem? Is this a bug on the Mac?
I tried the following code on OS X with JDK1.7 and I can click on the icon and trigger the listener. Feel free to modify this code to match your situation allowing us to reproduce the issue and include this in your question.
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.URL;
public class MouseClickedIcon {
public static void showUI() throws IOException {
JFrame testFrame = new JFrame( "TestFrame" );
String imageSource = "http://www.mynewitguys.com/wp-content/uploads/2011/04/java1.png";
ImageIcon icon = new ImageIcon( ImageIO.read( new URL( imageSource ) ) );
JLabel label = new JLabel( icon );
label.addMouseListener( new MouseAdapter() {
@Override
public void mouseClicked( MouseEvent e ) {
System.out.println( "MouseClickedIcon.mouseClicked" );
}
} );
testFrame.add( label, BorderLayout.CENTER );
testFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
testFrame.pack();
testFrame.setVisible( true );
}
public static void main( String[] args ) {
EventQueue.invokeLater( new Runnable() {
@Override
public void run() {
try {
showUI();
} catch ( IOException e ) {
}
}
} );
}
}
Got the source of the error. I run OSX in a virtual machine (VMWARE) and had a mouse compatibility option checked which caused the problem with the MouseClicked event. Sorry for the trouble.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.