Customizing Dragging and Dropping for Swing Components

Swing’s Drop and Drop API can be used for customizing Drag and Drop support. For most of the commonly used components like Text Components, Color Chooser, File Chooser etc, the dropping support is enabled by default. We have to explicitly enable the dragging support by calling the setDragEnabled() method. Before getting into customizing them, let us see a simple example,

also read:

SimpleDragAndDrop.java

package tips.swing.dnd;

import java.awt.FlowLayout;

import javax.swing.*;

public class SimpleDragAndDrop {

    public static void main(String[] args) {

        JFrame frame = new JFrame('Drag and Drop Demo');
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JTextField source = new JTextField('Type some text here');
        source.setDragEnabled(true);
        JTextField destination = new JTextField(18);
        destination.setDragEnabled(true);

        frame.setLayout(new FlowLayout());

        frame.getContentPane().add(source);
        frame.getContentPane().add(destination);

        frame.setSize(500, 500);
        frame.pack();
        frame.setVisible(true);
    }
}

When you run the above program, two text-fields will be presented, and it is now possible to drag the text from one text-field to the other text-field and vice-versa. Now, let us see how this can be customized. By default, an implicit transfer handler is set for the components that provides this drag and drop functionality. For text components, this implicit transfer handler transfers the text from the source to the destination. Now, have a look at the following code that replaces the implicit transfer handler with a custom one.

CustomizedDragAndDrop.java

package tips.swing.dnd;

import java.awt.*;

import javax.swing.*;

public class CustomizedDragAndDrop {

    public static void main(String[] args) {

        JFrame frame = new JFrame("Drag and Drop Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JTextField source = new JTextField("Some text here");
        source.setDragEnabled(true);
        source.setBackground(Color.RED);
        source.setForeground(Color.BLUE);
        source.setTransferHandler(new TransferHandler("foreground"));

        JTextField destination = new JTextField("Some text here");
        destination.setDragEnabled(true);
        destination.setBackground(Color.RED);
        destination.setForeground(Color.BLUE);
        destination.setTransferHandler(new TransferHandler('background'));

        frame.setLayout(new FlowLayout());

        frame.getContentPane().add(source);
        frame.getContentPane().add(destination);

        frame.setSize(500, 500);
        frame.pack();
        frame.setVisible(true);
    }
}

In the above code, we have explicitly called the setTransferHandler() method, by passing an new TransferHandler object along with a java bean property name. For the source text field, a java bean property called foreground is passed and for the second one, the java bean property is background. Now, when an attempt is made for transferring the content, instead of the text being transferred, the color is transferred between the text components.

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*