how to drag my component and place it anywhere on the panel

D

dxuranus

i make a dragable component :
//class: DragAbleComponent.the father of my component
import java.awt.Graphics;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.accessibility.Accessible;
import javax.swing.JComponent;

public abstract class DragAbleComponent extends JComponent implements
MouseListener,
FocusListener, Accessible ,MouseMotionListener{
private int X;

private int Y;


/**
*
*/
private static final long serialVersionUID = 1L;



public void mouseClicked(MouseEvent e) {
this.requestFocusInWindow();

}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mousePressed(MouseEvent e) {}

public void mouseReleased(MouseEvent e) {}

public void focusGained(FocusEvent e) {
this.repaint();}

public void focusLost(FocusEvent e) {
this.repaint();}

protected abstract void paintComponent(Graphics graphics);

public int getX() {
return X;
}

public void setX(int x) {
X = x;
}

public int getY() {
return Y;
}

public void setY(int y) {
Y = y;
}
}

//class:DragableIcon the component

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;

import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.KeyStroke;
import javax.swing.TransferHandler;

public class DragableIcon extends DragAbleComponent {

private Image image;

private MouseEvent firstMouseEvent = null;

private static boolean installKeyBordMapings = true;
/**
*
*/
private static final long serialVersionUID = 1L;

public DragableIcon(Image image) {
super();
this.image = image;
this.setFocusable(true);
this.addMouseListener(this);
this.addFocusListener(this);
this.addMouseMotionListener(this);
this.initKeyBordMaping();

}

private void initKeyBordMaping(){
if (installKeyBordMapings) {
InputMap imap = this.getInputMap();
// imap.put(KeyStroke.getKeyStroke("ctrl X"),
// TransferHandler.getCutAction().getValue(Action.NAME));
imap.put(KeyStroke.getKeyStroke("ctrl C"),
TransferHandler.getCopyAction().getValue(Action.NAME));
imap.put(KeyStroke.getKeyStroke("ctrl V"),
TransferHandler.getPasteAction().getValue(Action.NAME));
}

ActionMap map = this.getActionMap();
// map.put(TransferHandler.getCutAction().getValue(Action.NAME),
// TransferHandler.getCutAction());
map.put(TransferHandler.getCopyAction().getValue(Action.NAME),
TransferHandler.getCopyAction());
map.put(TransferHandler.getPasteAction().getValue(Action.NAME),
TransferHandler.getPasteAction());
}
@Override
protected void paintComponent(Graphics graphics) {
Graphics g = graphics.create();

//Draw in our entire space, even if isOpaque is false.
g.setColor(Color.WHITE);
g.fillRect(0, 0, image == null ? 125 : image.getWidth(this),
image == null ? 125 : image.getHeight(this));

if (image != null) {
//Draw image at its natural size of 125x125.
g.drawImage(image, 0, 0, this);
}

//Add a border, red if picture currently has focus
if (isFocusOwner()) {
g.setColor(Color.RED);
} else {
g.setColor(Color.BLACK);
}
g.drawRect(0, 0, image == null ? 125 : image.getWidth(this),
image == null ? 125 : image.getHeight(this));
// g.drawImage(image, 0, 0, this);
g.dispose();

}

public void mouseDragged(MouseEvent e) {

//Don't bother to drag if the component displays no image.
if (image == null) return;

if (firstMouseEvent != null) {
e.consume();

//If they are holding down the control key, COPY rather than MOVE
int ctrlMask = InputEvent.CTRL_DOWN_MASK;
int action = ((e.getModifiersEx() & ctrlMask) == ctrlMask) ?
TransferHandler.COPY : TransferHandler.MOVE;

int dx = Math.abs(e.getX() - firstMouseEvent.getX());
int dy = Math.abs(e.getY() - firstMouseEvent.getY());
//Arbitrarily define a 5-pixel shift as the
//official beginning of a drag.
if (dx > 5 || dy > 5) {
//This is a drag, not a click.
JComponent c = (JComponent)e.getSource();
TransferHandler handler = c.getTransferHandler();
//Tell the transfer handler to initiate the drag.
handler.exportAsDrag(c, firstMouseEvent, action);
firstMouseEvent = null;
}
}

}

public void mouseMoved(MouseEvent e) {}


public void mousePressed(MouseEvent e) {
//Don't bother to drag if there is no image.
if (image == null) return;

firstMouseEvent = e;
e.consume();
}

public void mouseReleased(MouseEvent e) {
firstMouseEvent = null;
}

public static boolean isInstallKeyBordMapings() {
return installKeyBordMapings;
}

public static void setInstallKeyBordMapings(boolean
installKeyBordMapings) {
DragableIcon.installKeyBordMapings = installKeyBordMapings;
}

public Image getImage() {
return image;
}

public void setImage(Image image) {
this.image = image;
}
}

//here is my handler
import java.awt.Image;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

import javax.swing.JComponent;
import javax.swing.TransferHandler;

import cn.com.ynld.bms.applet.gui.component.DragableIcon;

;

public class IconTransferHandler extends TransferHandler {

/**
*
*/
private static final long serialVersionUID = 1L;


DataFlavor pictureFlavor = DataFlavor.imageFlavor;
DragableIcon sourceIcon;
boolean shouldRemove;

public boolean importData(JComponent c, Transferable t) {
System.out.println("begin to import image----->");
Image image;
if (canImport(c, t.getTransferDataFlavors())) {
DragableIcon icon = (DragableIcon)c;
//Don't drop on myself.
if (sourceIcon == icon) {
shouldRemove = false;
return true;
}
try {
image = (Image)t.getTransferData(pictureFlavor);
//Set the component to the new picture.
icon.setImage(image);
return true;
} catch (UnsupportedFlavorException ufe) {
System.out.println("importData: unsupported data
flavor");
} catch (IOException ioe) {
System.out.println("importData: I/O exception");
}
}
return false;
}

protected Transferable createTransferable(JComponent c) {
sourceIcon = (DragableIcon)c;
shouldRemove = true;
return new IconTransferable(sourceIcon);
}

public int getSourceActions(JComponent c) {
return COPY;
}

protected void exportDone(JComponent c, Transferable data, int
action) {
// if (shouldRemove && (action == MOVE)) {
// sourceIcon.setImage(null);
// }
// sourceIcon = null;
}

public boolean canImport(JComponent c, DataFlavor[] flavors) {
// for (int i = 0; i < flavors.length; i++) {
// if (pictureFlavor.equals(flavors)) {
// return true;
// }
// }
return true;
}

class IconTransferable implements Transferable {
private Image image;

IconTransferable(DragableIcon icon) {
image = icon.getImage();
}

public Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException {
if (!isDataFlavorSupported(flavor)) {
throw new UnsupportedFlavorException(flavor);
}
return image;
}

public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[] { pictureFlavor };
}

public boolean isDataFlavorSupported(DataFlavor flavor) {
return pictureFlavor.equals(flavor);
}
}

}

i add a handler to my DragableIcon and pane both(copy DragableIcon to
my pane);
the draging seem worked(drag icon) but place dose't (he even did't
invoke the method importData) )
what' can i do?
thanks for any help!
 
T

Tom Cole

I'll be honest with you, I didn't go over all that code, but...assuming
that it's sound...

Did you remember to set the LayoutManager for the JPanel to null or to
something that uses XY location versus relative location? If you've got
a LayoutManager running (like FlowLayout or GridBagLayout or
GridLayout, etc.) it's going to place the component for you. That's
what they do.
i make a dragable component :
//class: DragAbleComponent.the father of my component
import java.awt.Graphics;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.accessibility.Accessible;
import javax.swing.JComponent;

public abstract class DragAbleComponent extends JComponent implements
MouseListener,
FocusListener, Accessible ,MouseMotionListener{
private int X;

private int Y;


/**
*
*/
private static final long serialVersionUID = 1L;



public void mouseClicked(MouseEvent e) {
this.requestFocusInWindow();

}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mousePressed(MouseEvent e) {}

public void mouseReleased(MouseEvent e) {}

public void focusGained(FocusEvent e) {
this.repaint();}

public void focusLost(FocusEvent e) {
this.repaint();}

protected abstract void paintComponent(Graphics graphics);

public int getX() {
return X;
}

public void setX(int x) {
X = x;
}

public int getY() {
return Y;
}

public void setY(int y) {
Y = y;
}
}

//class:DragableIcon the component

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;

import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.KeyStroke;
import javax.swing.TransferHandler;

public class DragableIcon extends DragAbleComponent {

private Image image;

private MouseEvent firstMouseEvent = null;

private static boolean installKeyBordMapings = true;
/**
*
*/
private static final long serialVersionUID = 1L;

public DragableIcon(Image image) {
super();
this.image = image;
this.setFocusable(true);
this.addMouseListener(this);
this.addFocusListener(this);
this.addMouseMotionListener(this);
this.initKeyBordMaping();

}

private void initKeyBordMaping(){
if (installKeyBordMapings) {
InputMap imap = this.getInputMap();
// imap.put(KeyStroke.getKeyStroke("ctrl X"),
// TransferHandler.getCutAction().getValue(Action.NAME));
imap.put(KeyStroke.getKeyStroke("ctrl C"),
TransferHandler.getCopyAction().getValue(Action.NAME));
imap.put(KeyStroke.getKeyStroke("ctrl V"),
TransferHandler.getPasteAction().getValue(Action.NAME));
}

ActionMap map = this.getActionMap();
// map.put(TransferHandler.getCutAction().getValue(Action.NAME),
// TransferHandler.getCutAction());
map.put(TransferHandler.getCopyAction().getValue(Action.NAME),
TransferHandler.getCopyAction());
map.put(TransferHandler.getPasteAction().getValue(Action.NAME),
TransferHandler.getPasteAction());
}
@Override
protected void paintComponent(Graphics graphics) {
Graphics g = graphics.create();

//Draw in our entire space, even if isOpaque is false.
g.setColor(Color.WHITE);
g.fillRect(0, 0, image == null ? 125 : image.getWidth(this),
image == null ? 125 : image.getHeight(this));

if (image != null) {
//Draw image at its natural size of 125x125.
g.drawImage(image, 0, 0, this);
}

//Add a border, red if picture currently has focus
if (isFocusOwner()) {
g.setColor(Color.RED);
} else {
g.setColor(Color.BLACK);
}
g.drawRect(0, 0, image == null ? 125 : image.getWidth(this),
image == null ? 125 : image.getHeight(this));
// g.drawImage(image, 0, 0, this);
g.dispose();

}

public void mouseDragged(MouseEvent e) {

//Don't bother to drag if the component displays no image.
if (image == null) return;

if (firstMouseEvent != null) {
e.consume();

//If they are holding down the control key, COPY rather than MOVE
int ctrlMask = InputEvent.CTRL_DOWN_MASK;
int action = ((e.getModifiersEx() & ctrlMask) == ctrlMask) ?
TransferHandler.COPY : TransferHandler.MOVE;

int dx = Math.abs(e.getX() - firstMouseEvent.getX());
int dy = Math.abs(e.getY() - firstMouseEvent.getY());
//Arbitrarily define a 5-pixel shift as the
//official beginning of a drag.
if (dx > 5 || dy > 5) {
//This is a drag, not a click.
JComponent c = (JComponent)e.getSource();
TransferHandler handler = c.getTransferHandler();
//Tell the transfer handler to initiate the drag.
handler.exportAsDrag(c, firstMouseEvent, action);
firstMouseEvent = null;
}
}

}

public void mouseMoved(MouseEvent e) {}


public void mousePressed(MouseEvent e) {
//Don't bother to drag if there is no image.
if (image == null) return;

firstMouseEvent = e;
e.consume();
}

public void mouseReleased(MouseEvent e) {
firstMouseEvent = null;
}

public static boolean isInstallKeyBordMapings() {
return installKeyBordMapings;
}

public static void setInstallKeyBordMapings(boolean
installKeyBordMapings) {
DragableIcon.installKeyBordMapings = installKeyBordMapings;
}

public Image getImage() {
return image;
}

public void setImage(Image image) {
this.image = image;
}
}

//here is my handler
import java.awt.Image;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

import javax.swing.JComponent;
import javax.swing.TransferHandler;

import cn.com.ynld.bms.applet.gui.component.DragableIcon;

;

public class IconTransferHandler extends TransferHandler {

/**
*
*/
private static final long serialVersionUID = 1L;


DataFlavor pictureFlavor = DataFlavor.imageFlavor;
DragableIcon sourceIcon;
boolean shouldRemove;

public boolean importData(JComponent c, Transferable t) {
System.out.println("begin to import image----->");
Image image;
if (canImport(c, t.getTransferDataFlavors())) {
DragableIcon icon = (DragableIcon)c;
//Don't drop on myself.
if (sourceIcon == icon) {
shouldRemove = false;
return true;
}
try {
image = (Image)t.getTransferData(pictureFlavor);
//Set the component to the new picture.
icon.setImage(image);
return true;
} catch (UnsupportedFlavorException ufe) {
System.out.println("importData: unsupported data
flavor");
} catch (IOException ioe) {
System.out.println("importData: I/O exception");
}
}
return false;
}

protected Transferable createTransferable(JComponent c) {
sourceIcon = (DragableIcon)c;
shouldRemove = true;
return new IconTransferable(sourceIcon);
}

public int getSourceActions(JComponent c) {
return COPY;
}

protected void exportDone(JComponent c, Transferable data, int
action) {
// if (shouldRemove && (action == MOVE)) {
// sourceIcon.setImage(null);
// }
// sourceIcon = null;
}

public boolean canImport(JComponent c, DataFlavor[] flavors) {
// for (int i = 0; i < flavors.length; i++) {
// if (pictureFlavor.equals(flavors)) {
// return true;
// }
// }
return true;
}

class IconTransferable implements Transferable {
private Image image;

IconTransferable(DragableIcon icon) {
image = icon.getImage();
}

public Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException {
if (!isDataFlavorSupported(flavor)) {
throw new UnsupportedFlavorException(flavor);
}
return image;
}

public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[] { pictureFlavor };
}

public boolean isDataFlavorSupported(DataFlavor flavor) {
return pictureFlavor.equals(flavor);
}
}

}

i add a handler to my DragableIcon and pane both(copy DragableIcon to
my pane);
the draging seem worked(drag icon) but place dose't (he even did't
invoke the method importData) )
what' can i do?
thanks for any help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top