Solved

How to move the Label/Jlabel with a mouseDragged event.

Posted on 2004-04-06
8
508 Views
Last Modified: 2008-01-16
How can the label be moved When you drag the mouse the label in the drawing canvas. The label should move at every point the mouse is dragged to (i.e the current location of the mouse).
0
Comment
Question by:amba2004
8 Comments
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10764079
on the mouse move, change the labels location.
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10764086
use the MouseMotionListener's processMouseMotionEvent .
0
 

Expert Comment

by:hapciu
ID: 10764121
more precisely:

in your drawing canvas (usually a JPanel) add a mouseMotionListener (or MouseMotionAdapter). in this, you implement mouseDragged or MouseMoved like this:

class YourPanel {
.....
JLabel yourLabel;
.....
this.addMouseMotionListener( new MouseMotionAdapter() {
  public void mouseDragged(MouseMotionEvent e) {
    yourLabel.setLocation(e.getX(), e.getY());
    YourPanel.this.repaint();
  }
});
......
}
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 13

Accepted Solution

by:
Webstorm earned 50 total points
ID: 10764132
Hi amba2004,

    label.addMouseListener(new MouseAdapter(){
           public void mousePressed(MouseEvent ev)
           {startMove(ev.getX(),ev.getY());}
           public void mouseReleased(MouseEvent ev)
           {endMove(ev.getX(),ev.getY());}
       });


    label.addMouseMotionListener(new MouseMotionAdapter(){
           public void mouseDragged(MouseEvent ev)
           {doMove(ev.getX(),ev.getY());}
       });

    int mx,my;
    void startMove(int x,int y)
    {
        mx=x;
        my=y;
    }

    void endMove(int x,int y)
    {
       doMove(x,y);
    }

    void doMove(int x,int y)
    {
        Point pt=label.getLocation();
        label.setLocation(pt.x+x-mx,pt.y+y-my);
    }
0
 

Expert Comment

by:RobCSP
ID: 10764183
Look at:
Technical Articles and Tips
Dragging Text and Images with Swing and Discovering the Calling Method Name in

http://java.sun.com/developer/JDCTechTips/2003/tt0318.html
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 10764191

You can also add a mousemotionlistener on the label container :

    cont.addMouseMotionListener(new MouseMotionAdapter(){
           public void mouseDragged(MouseEvent ev)
           {contdoMove(ev.getX(),ev.getY());}
       });

    void contdoMove(int x,int y)
    {
        label.setLocation(x-mx,y-my);
    }

0
 
LVL 13

Expert Comment

by:Webstorm
ID: 10764198
>> http://java.sun.com/developer/JDCTechTips/2003/tt0318.html
It's about Drag & Drop, not moving component

0
 
LVL 13

Expert Comment

by:Webstorm
ID: 10772867
:-)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Spring Framework HTTPSession management 1 45
difference of if loops 23 60
SequenceInputStream example 3 19
How to configure empty element in XML Document parser? 15 35
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question