Question

I'm having some problems I just can't figure out... I'm writing a Swing Java application with a JList that accepts drag-and-drops. I want to change the cursor while dragging a file or folder from my system over the Java application.

Was it helpful?

Solution

I've found it myself... Thanks Clinton for answering though. Here's what I've used:

first create the JList... You all know how to do that... Then I've added a setDropTarget:

lstFiles.setDropTarget(new DropTarget()
{
    @Override
    public synchronized void drop(DropTargetDropEvent dtde) 
    {
        this.changeToNormal();
        //handle the drop... [...]
    }

    @Override
    public synchronized void dragEnter(DropTargetDragEvent dtde) 
    {
        //Change cursor...
        Cursor cursor = new Cursor(Cursor.HAND_CURSOR);
        setCursor(cursor);

        //Change JList background...
        lstFiles.setBackground(Color.LIGHT_GRAY);
    }

    @Override
    public synchronized void dragExit(DropTargetEvent dtde) 
    {
        this.changeToNormal();
    }

    private void changeToNormal()
    {
        //Set cursor to default.
        Cursor cursor = new Cursor(Cursor.DEFAULT_CURSOR);
        setCursor(cursor);

        //Set background to normal...
        lstFiles.setBackground(Color.WHITE);
    }
});

OTHER TIPS

The following will only change the cursor when the user has moved the mouse over your JList.

You can change the cursor when you mouse over a component (i.e. your JList) by using a mouse listener and the setCursor method.

Essentially just add the mouse listener to your JList and use setCursor to change the cursor when the user mouses over the component in your application (mouseEntered and mouseExit). You may also need to do a little inquiry on your drag and drop code to only change the cursor when something valid is being dragged into your JList.

Hope this helps a bit.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top