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

StackOverflow https://stackoverflow.com/questions/22698435

  •  22-06-2023
  •  | 
  •  

Frage

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?

War es hilfreich?

Lösung

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();
         }
      });
   }
}

Andere Tipps

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();
    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top