views:

428

answers:

3

Hi, I have problem with mouseListener in JPanel. I add JLabel which contain image(size 600 x 600) on JPanel, and I add mouseListener for the JPanel.

Everytimes I click on the image, the image should change to another image, and this is working fine. However, the problem is that, only if I click on the right side or at the center of the image, then the image will change to another image. The image doesn't change when I click on the top or on the left side. This make me confused. I want the image to change to another image when I click everywhere within the image display.

private final int SECOND= 1;
private final int FIRST = 0;
int imageCounter = 0;

JLabel picture;
JPanel panel;

...

private mainLayout () {
  GridBagLayout m = new GridBagLayout();
  Container c = (Container)getContentPane();
  GridBagConstraints con;
  c.setLayout (m);

  picture = new JLabel();
  picture.setIcon(getImages(myImage.get(imageCounter).get(FIRST)));  //first Image

  panel = new JPanel();  
  con = new GridBagConstraints();
  con.anchor=GridBagConstraints.CENTER;
  con.gridy = 1; con.gridx = 0;  
  m.setConstraints(panel, con);
  c.add(panel);
  panel.add(picture); //add the pictures

  panel.addMouseListener(l);
  ....
}

MouseListener l = new MouseAdapter(){
  public void mouseClicked (MouseEvent e) 
  {
    Point p = e.getPoint();
    if((panel.getBounds().contains(p))
       picture.setIcon(getImages(myImage.get(imageCounter).get(SECOND)));
  }
};
A: 

Are you positive that you are actually clicking? The mouse click event only fires if the mouse is pressed and released in the exact same spot. If your mouse is moving even minimally while you are clicking, the event won't be fired.

Samuel
A: 

Out of curiosity, why are you using a JLabel to display an image? You may be better off writing an ImagePanel class that extends JPanel and overrides the paint() method to do your own drawing. Then you can attach MouseListeners right to the panel.

Seth
-1 Why would you create a custom component and do custom painting when this fuctionality is built into the a JLabel already? Don't reinvent the wheel.
camickr
Hi Seth, I used the JLabel to display the image in jpg format.
Jessy
I guess it depends on how much control you want over the drawing. Fair point though.
Seth
+1  A: 

The problem looks as if it might be in this unnecessary code:

Point p = e.getPoint();
if((panel.getBounds().contains(p))

The mouse listener is on the panel, so the mouse coordinates will be relative to the panel top left. panel.getBounds() gets the bounds of the panel relative to whatever its parent container is.

It's worth noting the mouse event behave very strangely. They "bubble up" until they hit a component with a mouse listener attached. So, adding a mouse listener actually changes the behaviour of a component. Adding the listener to a parent will potentially miss events depending upon the exact way the component is set up (which may change arbitrarily). There are a number of ways around this, none of them good.

Tom Hawtin - tackline