Solved

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

Posted on 2004-04-06
8
497 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
Comment Utility
on the mouse move, change the labels location.
0
 
LVL 9

Expert Comment

by:mmuruganandam
Comment Utility
use the MouseMotionListener's processMouseMotionEvent .
0
 

Expert Comment

by:hapciu
Comment Utility
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
 
LVL 13

Accepted Solution

by:
Webstorm earned 50 total points
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Expert Comment

by:RobCSP
Comment Utility
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
Comment Utility

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
Comment Utility
>> http://java.sun.com/developer/JDCTechTips/2003/tt0318.html
It's about Drag & Drop, not moving component

0
 
LVL 13

Expert Comment

by:Webstorm
Comment Utility
:-)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now