Home > Java > javaTutorial > body text

Does MousePressed Method Inhibit MouseReleased from Triggering in JLabel Drag and Drop?

Mary-Kate Olsen
Release: 2024-10-24 02:41:29
Original
763 people have browsed it

Does MousePressed Method Inhibit MouseReleased from Triggering in JLabel Drag and Drop?

JLabel Mouse Events for Drag and Drop

Question:

Implementing drag and drop for a JLabel via mouse event overrides, the mouseReleased event fails to trigger upon mouse release when defined within the mousePressed event. Is there something am I doing incorrectly?"

Code:

<code class="java">Thumbnails[I_Loop].addMouseListener( new MouseAdapter() {
    public void  mouseReleased(MouseEvent me) {
           System.out.println("here mouse released");
      }
    public void mousePressed(MouseEvent me) {
        System.out.println("here mouse pressed");
        JComponent comp = (JComponent) me.getSource();
        TransferHandler handler = comp.getTransferHandler();
        handler.exportAsDrag(comp, me, TransferHandler.COPY);
    });</code>
Copy after login

Answer:

While @Thomas's solution is correct, here are two alternative approaches to consider:

1. Using JLayeredPane:

This example demonstrates dragging a component using JLayeredPane. This variation expands on the same concept, while a more recent example uses a similar approach.

Code:

<code class="java">// ...
private Point mousePt;
private Point dragPt;
private Rectangle bounds;

public MouseDragTest() {
    this.addMouseMotionListener(new MouseMotionAdapter() {

        @Override
        public void mouseDragged(MouseEvent e) {
            int dx = e.getX() - mousePt.x;
            int dy = e.getY() - mousePt.y;
            dragPt.setLocation(dragPt.x + dx, dragPt.y + dy);
            super.mouseDragged(e);
        }
    });
    this.addMouseListener(new MouseAdapter() {

        @Override
        public void mousePressed(MouseEvent e) {
            bounds = getBounds();
            Point p = SwingUtilities.convertPoint(this, e.getPoint(), Bounds.Parent);
            if (bounds.contains(p)) {
                dragPt = p;
                mousePt = e.getPoint();
            }
        }
    });
    // ...
}</code>
Copy after login

2. Using MouseMotionListener:

This code demonstrates using a MouseMotionListener. This more complex example uses the same principle.

Code:

<code class="java">// ...
private Point offset;

public MouseDragTest() {
    this.addMouseMotionListener(new MouseMotionAdapter() {

        @Override
        public void mouseDragged(MouseEvent e) {
            Point delta = e.getPoint();
            Point pt = new Point(getLocation().x + delta.x - offset.x,
                    getLocation().y + delta.y - offset.y);
            setLocation(pt);
        }
    });
    this.addMouseListener(new MouseAdapter() {

        @Override
        public void mousePressed(MouseEvent e) {
            offset = e.getPoint();
        }
    });
    // ...
}</code>
Copy after login

The above is the detailed content of Does MousePressed Method Inhibit MouseReleased from Triggering in JLabel Drag and Drop?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!