gkurian
gkurian

Reputation: 65

How do i know if a mouse click is done on a custom component in a panel?

I have a JPanel and some custom components in it. There are lots of such panels in the Frame. I need to know if a mouse click is on a custom component(which take for example is a JLabel)..... I wrote the following code to achieve this:

public object getxxx(MouseEvent pEvent)
        {                                
       Point localPoint = SwingUtilities.convertPoint(pEvent.getComponent(), pEvent.getPoint(), aPanel  );
       if (SwingUtilities.getLocalBounds(aPanel).contains(localPoint)) // This is where im facing problem, its always false in never goes in…
          {
           ///if clicked on the aPanel then do something                                            
          }
        }

The if condition is always false, even if i click on the desired panel

Upvotes: 0

Views: 1398

Answers (3)

camickr
camickr

Reputation: 324128

You can add the MouseListener to your panel. Then you can use the findComponentAt(...) method of the Container class to return the component that was clicked.

Upvotes: 1

user1803551
user1803551

Reputation: 13427

It is not clear what you already have and what you have left to do, but this seems to answer your needs:

public class MyFrame extends JFrame {

    JPanel panel = new JPanel();

    MyFrame() {

        JLabel l1 = new JLabel("111");
        l1.setName("111");
        l1.setOpaque(true);
        l1.setBackground(Color.MAGENTA);

        JLabel l2 = new JLabel("222");
        l2.setName("222");
        l2.setOpaque(true);
        l2.setBackground(Color.CYAN);

        JLabel l3 = new JLabel("333");
        l3.setName("333");
        l3.setOpaque(true);
        l3.setBackground(Color.YELLOW);

        panel.add(l1);
        panel.add(l2);
        panel.add(l3);
        panel.addMouseListener(new MyMouseListener());
        panel.setName("panel");

        getContentPane().add(panel);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    class MyMouseListener extends MouseAdapter {

        @Override
        public void mouseClicked(MouseEvent e) {

            if (e.getButton() == MouseEvent.BUTTON3) { // BUTTON3 = right button
                Component c = panel.getComponentAt(e.getPoint()); // compare with panel.findComponentAt
                System.out.println("Open context menu for " + c.getName());
            }
        }
    }

    public static void main(String[] args) {

        new MyFrame();
    }
}

Explanation:

  • I created 3 JLabels, gave them background colors to visualize the component's area on screen and names for identification. You will use your own components here instead.
  • The JPanel containing the labels registers the MouseListener, which checks for a right-click. If it is, it finds the component inside the panel on which the event was generated1. You can then open your context menu there.

1 Initially I had my way of getting the component at the event location, but the answer by camickr proved to be shorter. (+1)

Upvotes: 1

Marco
Marco

Reputation: 508

You can use the MouseListener interface. See this reference.

Try adding a Mouse Listener to your custom component.

This code snippet could help you:

JPanel panel = new JPanel();
panel.addMouseListener(new MouseAdapter() {     
        @Override
        public void mouseClicked(MouseEvent arg0) {
            // **your code here**

        }
    });

Upvotes: 0

Related Questions