0

I have a JPanel with many JButton components inside. Is there a way to get the panel mouse event when a button is pressed? Another point of view: How to make button transparent to panel mouse event? I specifically need to capture mousePressed() event of panel.

EDIT

Context: I'm dragging the panel content through a JScrollPane (actually working), to accomplish that I needed to capture the point where mouse is pressed, so both panel and buttons have MouseListener and MouseMotionListener to capture the point and do other stuff.

Issue: When I press -> drag -> release the mouse button, if the mouse is still over the button it executes whatever the button does. So I want the mouse listener of the panel to be 'independent' of the button, to remove the mouse listener from the buttons.

EDIT 2

I just realize reading my own issue... that it will make no difference removing MouseListener to the JButton. When pressing the button if the mouse it stil over it, the actionPerformed will be executed anyway...What can I do?

EDIT 3 Edited question title, according to the solution.

tec
  • 999
  • 3
  • 18
  • 40
  • Could be a possible duplicate of: http://stackoverflow.com/questions/16431455/addmouselistener-for-a-jpanel – Manish Sakpal Dec 18 '16 at 11:52
  • 1
    Why you need it? What you want to achive? – Sergiy Medvynskyy Dec 18 '16 at 12:24
  • @SergiyMedvynskyy asks a very good couple of questions. See also [What is the XY problem?](http://meta.stackexchange.com/q/66377) – Andrew Thompson Dec 18 '16 at 12:38
  • *"I'm dragging the panel content through a `JScrollPane`"* A `JScrollBar` is better for that. It represents the 'path of least surprise` for the end user and would make the rest redundant. – Andrew Thompson Dec 18 '16 at 12:42
  • @Andrew Thompson Im working with touch screens, and for accessibility it would be more easy dragg the content, – tec Dec 18 '16 at 12:44
  • @tomyforever all what you need is the ONE mouse listener which is added to all buttons and to the panel. If user triggers the method for a button you need to transform the button coordinates to panel coordinates. If for panel - you alrerady have the correct coordinates. – Sergiy Medvynskyy Dec 18 '16 at 12:52
  • @SergiyMedvynskyy I already have that. What I'm trying to accomplish is to drag the content without execute the button action event – tec Dec 18 '16 at 12:55
  • I mean, difference between drag and click. – tec Dec 18 '16 at 13:00

1 Answers1

1

Kipping in mind that the event execution order in this case is: mousePressed->mouseDragged->actionPerformed->mouseReleased , I get it working at the moment, adding a boolean:

@Override
public void mousePressed(MouseEvent e) {
        origin = new Point(e.getPoint());
}
//each time the user stops dragging set dragged to false
@Override
public void mouseReleased(MouseEvent arg0) {
     dragged = false;
}

@Override
public void mouseDragged(MouseEvent e) {

        dragged=true;
        if(((Component) e.getSource()).getParent().equals(myPanel)
                || e.getSource().equals(myPanel)){
          if (origin != null) {
            JViewport viewPort = (JViewport) SwingUtilities.getAncestorOfClass(JViewport.class, myPanel);
            if (viewPort != null) {
                int deltaX = origin.x - e.getX();
                int deltaY = origin.y - e.getY();

                Rectangle view = viewPort.getViewRect();
                view.x += deltaX;
                view.y += deltaY;
                myPanel.scrollRectToVisible(view);
            }
        }
}
@Override
public void actionPerformed(ActionEvent e){

    //stuff do detect the button...
    //..in case there is more than one panel, if the component belong to myPanel and dragg is false 
    if(((Component) e.getSource()).getParent().equals(myPanel)&&  dragged==false ){
    //do stuff
    }
}
tec
  • 999
  • 3
  • 18
  • 40