Satellite Internet QuickBooks Advice international calling cards calling cards
JavaBeat Certifications Certifications Kits Articles Tips QNA Interview Questions SCJP 1.5 SCBCD 5.0 Java/J2EE Feeds
Feedback Request New Tips Print Email

Customizing Dragging and Dropping for Swing Components

Author : Christy
Date : Fri Oct 26th, 2007
Topic : java
Add to: Digg Add to: Del.icio.us Add to: Reddit Add to: StumbleUpon Add to: Slashdot Add to: Yahoo Add to: Google Add to: Blinklist Add to: Technorati Information

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,

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.

Feedback Request New Tips Print Email

Favorites
C# problem error
Free Newsgroups
Latest QnA
SCJD Tips
When we start a thread by applying start() method on it ,how does it knows that to execute run()method?
About Wrapper class in Java
How to configure weblogic 7.0 in MyEclipse?
Static Block and Static Initializer in Java

JavaBeat Website (2004-2009), India
javabeat | about us | useful resources
Copyright (2004 - 2009), JavaBeat