0

This is my program which is suppose to make the image draggable in JFrame .

package testing;


import java.io.*;
import java.util.*;
import java.security.*;
import javax.xml.bind.DatatypeConverter;
import java.lang.*;
import java.awt.*;
import javax.swing.*;
import  java.awt.event.*;

public class Testing 
{

    static class CPanel extends JPanel
    {
       CPanel()
       {
           this.setPreferredSize(new Dimension(200,100));

           icon = new ImageIcon("./src/testing/cal.png");

           jl = new JLabel(icon);

           this.add(jl);

       }

       class CMotionListener implements MouseMotionListener
       {
           public void mouseMoved(MouseEvent event)
           {

           }

           public void mouseDragged(MouseEvent event)
           {

           }



       }



       JLabel jl ;
       ImageIcon icon;
    }


    public static void main(String[] args) 
    {
        JFrame jf = new JFrame();
        jf.setPreferredSize(new Dimension(1000,1000));

        CPanel cp = new CPanel();



        jf.add(cp);
        jf.pack();
        jf.setVisible(true);


    }


}

CMotionListener implements MouseMotionListener and the method mouseDragged is supposed to be overridden so that when i clicked on the image and dragged it , it will be dragged along with my mouse .

My problem is I am unsure how to go about overriding the method mouseDragged so that the image can be dragged along with the mouse .

Computernerd
  • 7,378
  • 18
  • 66
  • 95
  • Some examples are shown [here](http://stackoverflow.com/search?q=user%3A230513+drag+me). – trashgod Apr 11 '14 at 08:13
  • 1
    [Don't use `setPreferredSize()` when you really mean to override `getPreferredSize()`](http://stackoverflow.com/q/7229226/230513). – trashgod Apr 11 '14 at 08:14
  • You're label is under the control of a layout manager, that's going to make live fun for you – MadProgrammer Apr 11 '14 at 09:51
  • As a [possible example](http://stackoverflow.com/questions/13130257/drag-and-move-a-picture-inside-a-jlabel-with-mouseclick/13131402#13131402) – MadProgrammer Apr 11 '14 at 10:01

0 Answers0