Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-04-06
8
Medium Priority
?
541 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
LVL 13

Accepted Solution

by:
Webstorm earned 200 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

715 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