Solved

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

Posted on 2004-04-06
8
514 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
tomcat administrtor 12 68
ejb entity bean example issue 2 24
Selenium docs api java index 3 63
swing controls 2 21
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

679 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