如何隐藏Mac / OSX拖放JTable选择框 [英] How to hide Mac/OSX Drag and Drop JTable selection frame
问题描述
在JTable上执行拖放操作时,会拖曳出现所选单元格(选择框)的轮廓。如何覆盖该行为,但不能显示任何东西,但也可能显示特殊光标?
When performing a drag and drop on a JTable there is an outline of the selected cell (selection frame) that appears while dragging. How can I override that behavior and not show anything but perhaps a special cursor?
在Windows和OSX中运行以下代码显示了我想要覆盖的行为! / p>
Running the following code in Windows and OSX shows the behavior I'd like to override!
import java.awt.BorderLayout;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragSource;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
public class GestureTest
extends
JFrame
implements
DragGestureListener {
private final DragSource ds;
private final JTable jl;
private static final Object[][] ITEMS = { { "Java" }, { "C" }, { "C++" }, { "Lisp" },
{ "Perl" }, { "Python" } };
public GestureTest() {
super("Gesture Test");
setSize(200, 150);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(final WindowEvent we) {
System.exit(0);
}
});
jl = new JTable(ITEMS, new Object[] { "Langs" });
jl.setDragEnabled(true);
jl.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
getContentPane().add(new JScrollPane(jl), BorderLayout.CENTER);
ds = new DragSource();
// final DragGestureRecognizer dgr =
ds.createDefaultDragGestureRecognizer(
jl, DnDConstants.ACTION_MOVE, this);
setVisible(true);
}
@Override
public void dragGestureRecognized(final DragGestureEvent dge) {
System.out.print("dragGestureRecognized");
}
public static void main(final String args[]) {
new GestureTest();
}
}
推荐答案
我找到了一个解决方案,虽然你将不得不再多一点点处理自己。
I found a solution, although you will have to write a little bit more of the dnd handling yourself.
首先禁用JTable自己的dnd处理: jl.setDragEnabled(false);
First disable JTable's own dnd handling: jl.setDragEnabled(false);
然后,您可以使用以下代码进行 dragGestureRecognized
方法调用 startDrag
自己。
Then you can use the following code for the dragGestureRecognized
method to call startDrag
yourself.
重要的是空
图像。在这里使用 null
将导致操作系统显示操作系统特定的映像(Windows没有,OS X的灰色框),在此显式设置图像将导致该映像为显示(而在我们的例子中为空的1x1像素图像)。
The important bit is the empty
image. Using null
here will cause the OS to display an OS specific image (none for Windows, a grey box for OS X), explicitly setting an image here will cause that image to be displayed instead (an empty 1x1 pixel image in our case).
@Override
public void dragGestureRecognized(final DragGestureEvent dge) {
System.out.print("dragGestureRecognized");
Transferable transferable = new Transferable() {
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return true;
}
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[] { DataFlavor.stringFlavor };
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
return "Hello world!";
}
};
BufferedImage empty = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
dge.startDrag(null, empty, new Point(), transferable, null);
}
这篇关于如何隐藏Mac / OSX拖放JTable选择框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!