Zach Santiago
Zach Santiago

Reputation: 381

Java statement causes a mouseClick to be a mouseRelease for MouseListener in Jpanel

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 .

Upvotes: 2

Views: 104

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285401

I'm not sure if this is the source of your problems, since I'm not sure what your all code does, but it looks like a potential problem:

You should probably use the mousePressed method override and not mouseClicked especially if it is paired with an activity that is tied to mouseReleased. The mouseClicked method means that the mouse has been pressed and released on the same spot, and is touchy and won't be called if you move the mouse, even slightly, between clicking and releasing. The mousePressed on the other hands responds immediately on mousePressed, which sounds like what you want.

so perhaps:

private class MouseHandler extends MouseAdapter {
    @Override
    public void mousePressed(MouseEvent event) {

        statusBar.setText( String.format( "Pressed 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
    }

    @Override
    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

    }

    // since this extends MouseAdapter, all the other methods are not needed
}

Upvotes: 3

Related Questions