i have added a mouse listener to my JPanel in the following way. What is outputed to JPanel is a image painted on screen from an array of images.
public class NewFrame extend JFrame {
public New JFrame() {
statusBar = new JLabel("Mouse Outdide Panel"); //displays events for mouse
add(statusBar,BorderLayout.SOUTH); //add to JFrame
MouseHandler handler = new MouseHandler();
JPanelMap.addMouseListener(handler); //add both mouse listeners to JPanel
JPanelMap.addMouseMotionListener(handler);
}
private class MouseHandler implements MouseListener,
MouseMotionListener
{
public void mouseClicked(MouseEvent event) {
statusBar.setText( String.format( "Clicked at [%d, %d]",
event.getX() , event.getY() ) );
int x = event.getX();
int y = event.getY();
mapObject.updateDockOrShip(x, y,text); //updates selected icon on screen
}
public void mousePressed(MouseEvent event) {
}
public void mouseReleased(MouseEvent event) {
statusBar.setText( String.format( "Released at [%d, %d]",
event.getX() , event.getY() ) );
int x = event.getX();
int y = event.getY();
/*this is the troubleMaker, if i delete this line all "clicks"
work everywhere on my JPanel. but if i leave it in,
clicks work in some areas of JPanel, and other legitimate clicks
are recognized as mouseReleased in different area of JPanel
*/
mapbject.checkDocks(x,y,tempint); //TROUBLE
}
public void mouseEntered( MouseEvent event )
{
}
public void mouseExited( MouseEvent event )
{
}
public void mouseDragged( MouseEvent event )
{
}
public void mouseMoved( MouseEvent event )
{
}
}
}
i have highlighted the troubling statement in the code given. As soon as i implement that line some portions of the map are clickable with a click and other portions are not clickable even though i still do the same click. I would think that a mouse click is a mouse click no matter where you click to the mouse listener or am i assuming wrong or missing something. The behavior of the mouse click seems to change when that statement is implemented to a mouseReleased action but i dont understand why .