How to change cursor when dropping into Java application

早过忘川 提交于 2019-12-01 07:01:20

问题


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.


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/1942916/how-to-change-cursor-when-dropping-into-java-application

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