JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy

Customizing Dragging and Dropping for Swing Components

October 26, 2007 by Krishna Srinivasan Leave a Comment

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:

  • Java Tutorials
  • Java EE Tutorials
  • Design Patterns Tutorials
  • Java File IO Tutorials

SimpleDragAndDrop.java
[code lang=”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);
}
}
[/code]

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
[code lang=”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);
}
}
[/code]

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.

Filed Under: Java Tagged With: Swing

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.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved