Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-04-06
8
Medium Priority
?
564 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

783 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