How to hide Mac/OSX Drag and Drop JTable selection frame

ぃ、小莉子 提交于 2019-12-23 17:13:27

问题


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?

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();
    }

}

回答1:


I found a solution, although you will have to write a little bit more of the dnd handling yourself.

First disable JTable's own dnd handling: jl.setDragEnabled(false);

Then you can use the following code for the dragGestureRecognized method to call startDrag yourself.

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);
}


来源:https://stackoverflow.com/questions/12939846/how-to-hide-mac-osx-drag-and-drop-jtable-selection-frame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!