How to change cursor when dropping into Java application

后端 未结 2 1500
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-14 10:11

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 dragg

相关标签:
2条回答
  • 2021-01-14 10:51

    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.

    0 讨论(0)
  • 2021-01-14 11:14

    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);
        }
    });
    
    0 讨论(0)
提交回复
热议问题