How to change the cursor when switching to a Java application

I'm having problems that I just can't understand ... I am writing a Swing Java application with a JList that accepts drag and drop. I want to change the cursor by dragging a file or folder from my system through a Java application.

+3
source share
2 answers

I found it myself ... Thanks Clinton for the answer. Here is what I used:

first create a JList ... you all know how to do this ... Then I added 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);
    }
});
+4
source

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

(.. JList) setCursor.

, JList setCursor , (mouseEntered mouseExit). , , - JList.

, .

+1

Source: https://habr.com/ru/post/1726290/


All Articles