0
votes

I have written a program with two components that use mouseListeners:

  1. ResizeRectangle draws a rectangle with handles and allows moving and resizing of the rectangle. It handles MouseEvents and MouseMotionEvents (MouseMove, MousePressed, MouseDragged and MouseReleased).
  2. IconGrid draws a grid of icons within the rectangle and allows selecting (clicking) an icon. It handles the MouseClicked event for this.

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?

2
I'm not sure how you got mouseReleased to work, since there isn't any such method in cocoa (nor mouseClicked, mousePressed). The methods are mouseEntered, mouseExited, mouseDown,mouseDragged,mouseMoved and mouseUp.rdelmar
@rdelmar it is a Java problem, and Java has those eventsRobin
1) What GUI library are you using? Swing? AWT? SWT? other? This information is somewhat germane to this discussion I think. 2) If no decent answers are presented in the near future, consider creating a small compilable and runnable program that we can inspect, run, test, and alter, that demonstrates your problem and has no extraneous code unrelated to the problem at hand, an sscce.Hovercraft Full Of Eels

2 Answers

0
votes

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 ) {
        }
      }
    } );
  }
}
0
votes

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.