如何为 NetBeans Visual Library Scene 启用 java.awt.dnd drop?
How to enable java.awt.dnd drop for NetBeans Visual Library Scene?
我使用带有 ImageIcon 的 JLabel 创建了简单的组件面板。
那是调色板项目的代码:
public class TransferableIcon extends JLabel implements Transferable {
private final Image icon;
public TransferableIcon(Image image) {
super(new ImageIcon(image));
this.icon = image;
setTransferHandler(ImageTransferHandler.getTransferHandler());
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
JComponent component = (JComponent)e.getSource();
getTransferHandler().exportAsDrag(component, e, TransferHandler.COPY);
}
});
}
private static final DataFlavor[] FLAVORS = new DataFlavor[]{ DataFlavor.imageFlavor };
@Override
public DataFlavor[] getTransferDataFlavors() {
return FLAVORS;
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
boolean result = false;
for(DataFlavor dataFlavor : FLAVORS) {
if(dataFlavor.equals(flavor))
result = true;
}
return result;
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
if(!isDataFlavorSupported(flavor))
throw new UnsupportedFlavorException(flavor);
else
return icon;
}
}
我自己的 TransferHandler class 覆盖了一些方法:
public class ImageTransferHandler extends TransferHandler {
private static final TransferHandler TRANSFER_HANDLER = new ImageTransferHandler();
@Override
public int getSourceActions(JComponent c) {
return TransferHandler.COPY;
}
@Override
protected Transferable createTransferable(JComponent c) {
Image image = null;
if(c instanceof JLabel) {
JLabel label = (JLabel)c;
Icon icon = label.getIcon();
image = ((ImageIcon)icon).getImage();
return new TransferableIcon(image);
}
else
throw new IllegalArgumentException("Can not transfer such a widget");
}
public static TransferHandler getTransferHandler() {
return TRANSFER_HANDLER;
}
}
我有一个 Visual Library 场景,通过 scene.createView()
方法添加到 JFrame。
我在场景中添加了一个 AcceptAction,创建了我自己的 AcceptProvider,但它的 accept()
方法从未被调用:当我尝试将 TransferableIcon
拖到场景中时,我看到“不允许”光标,但没有任何反应当我将图标放到场景中时。
那么,如何启用到场景的拖放?
问题并没有我想的那么复杂。
在 AcceptAction class 中我们看到,它仅接受 COPY_OR_MOVE 操作的拖放(以及放下)。因此,在您自己的 TransferHandler class 中,您必须设置完全相同的操作:
@Override
public int getSourceActions(JComponent c) {
return TransferHandler.COPY_OR_MOVE;
}
就是这样!
我使用带有 ImageIcon 的 JLabel 创建了简单的组件面板。 那是调色板项目的代码:
public class TransferableIcon extends JLabel implements Transferable {
private final Image icon;
public TransferableIcon(Image image) {
super(new ImageIcon(image));
this.icon = image;
setTransferHandler(ImageTransferHandler.getTransferHandler());
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
JComponent component = (JComponent)e.getSource();
getTransferHandler().exportAsDrag(component, e, TransferHandler.COPY);
}
});
}
private static final DataFlavor[] FLAVORS = new DataFlavor[]{ DataFlavor.imageFlavor };
@Override
public DataFlavor[] getTransferDataFlavors() {
return FLAVORS;
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
boolean result = false;
for(DataFlavor dataFlavor : FLAVORS) {
if(dataFlavor.equals(flavor))
result = true;
}
return result;
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
if(!isDataFlavorSupported(flavor))
throw new UnsupportedFlavorException(flavor);
else
return icon;
}
}
我自己的 TransferHandler class 覆盖了一些方法:
public class ImageTransferHandler extends TransferHandler {
private static final TransferHandler TRANSFER_HANDLER = new ImageTransferHandler();
@Override
public int getSourceActions(JComponent c) {
return TransferHandler.COPY;
}
@Override
protected Transferable createTransferable(JComponent c) {
Image image = null;
if(c instanceof JLabel) {
JLabel label = (JLabel)c;
Icon icon = label.getIcon();
image = ((ImageIcon)icon).getImage();
return new TransferableIcon(image);
}
else
throw new IllegalArgumentException("Can not transfer such a widget");
}
public static TransferHandler getTransferHandler() {
return TRANSFER_HANDLER;
}
}
我有一个 Visual Library 场景,通过 scene.createView()
方法添加到 JFrame。
我在场景中添加了一个 AcceptAction,创建了我自己的 AcceptProvider,但它的 accept()
方法从未被调用:当我尝试将 TransferableIcon
拖到场景中时,我看到“不允许”光标,但没有任何反应当我将图标放到场景中时。
那么,如何启用到场景的拖放?
问题并没有我想的那么复杂。 在 AcceptAction class 中我们看到,它仅接受 COPY_OR_MOVE 操作的拖放(以及放下)。因此,在您自己的 TransferHandler class 中,您必须设置完全相同的操作:
@Override
public int getSourceActions(JComponent c) {
return TransferHandler.COPY_OR_MOVE;
}
就是这样!