Listen for mouse released event on component on which the mouse was not pressed in Swing

前提是你 提交于 2019-12-24 03:34:26

问题


Is it possible to listen for mouse released event on the component on which it was not pressed?

I know that when mouse is released MouseListener.mouseReleased()is invoked on the listeners for that component when mouse press originated even if the cursor is above other component.

How to inform a component or its listeners that the mouse was over it and it was released?


回答1:


If you add your MouseListener to the container that holds your components of interest, you can find out which component the mouse is over on press or drag. For instance in the code below, I've added my MouseAdapter (a combination MouseListener, MouseMotionListener, and MouseWheelListener) to the containing JPanel, and after getting the location of the mouse event on the container, I call getComponentAt(Point p) on my container to get the child component that the mouse was over:

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.*;

@SuppressWarnings("serial")
public class TestMouseRelease extends JPanel {
   private String[] panelNames = { "Panel A", "Panel B" };

   public TestMouseRelease() {
      setLayout(new GridLayout(1, 0));
      MouseAdapter mAdapter = new MyMouseAdapter();

      addMouseListener(mAdapter);
      addMouseMotionListener(mAdapter);

      for (String panelName : panelNames) {
         JPanel panel = new JPanel();
         panel.setName(panelName);
         // panel.addMouseListener(mAdapter);
         // panel.addMouseMotionListener(mAdapter);
         panel.setBorder(BorderFactory.createTitledBorder(panelName));
         panel.add(Box.createRigidArea(new Dimension(300, 300)));
         add(panel);
      }
   }

   private class MyMouseAdapter extends MouseAdapter {
      @Override
      public void mousePressed(MouseEvent e) {
         displayInfo(e, "mousePressed");
      }

      @Override
      public void mouseReleased(MouseEvent e) {
         displayInfo(e, "mouseReleased");
      }

      @Override
      public void mouseDragged(MouseEvent e) {
         displayInfo(e, "mouseDragged");
      }

      private void displayInfo(MouseEvent e, String info) {
         JComponent comp = (JComponent) e.getSource();
         Component childComp = comp.getComponentAt(e.getPoint());
         if (childComp != null) {
            String name = childComp.getName();
            System.out.println(name + ": " + info);
         }
      }
   }

   private static void createAndShowGui() {
      JFrame frame = new JFrame("TestMouseRelease");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new TestMouseRelease());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}



回答2:


Yeah, I handled something similar in my project. I use getBounds() on all component in the container and check if they contain the x,y coordinates of your mouse. Below is my code:

Component[] components = jPanel1.getComponents();
for (Component c : components) {

    if (c.getBounds().contains(ev.getXOnScreen(), ev.getYOnScreen())) {
        System.out.println(c.getClass());
        y = ch.getPosY();
        x = ch.getPosX();
    }
}


来源:https://stackoverflow.com/questions/22698435/listen-for-mouse-released-event-on-component-on-which-the-mouse-was-not-pressed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!