Drag and Drop in Java
Submitted by donbermoy on Thursday, December 11, 2014 - 19:56.
This tutorial will teach you how to create a drag and drop program in two textfields in java.
So, now let's start this tutorial!
1. Open JCreator or NetBeans and make a java program with a file name of dragDrop.java.
2. Import the following package library:
3. We will initialize variables in our Main, variable frame as JFrame, and variable textfield1 and textfield2 as JTextField.
4. We will have a text for the two textfields using the setText method.
To have a drag and drop between the two textfields, we will use the setDragEnabled method returns to True.
5. Now, we will have its layout into Grid Layout using the setLayout method of the frame.
Add all the textfields to the frame using the add method.
Lastly, pack the frame, set visibility to true, and the close operation of the frame. Have this code below:
Output:
Here's the full code of this tutorial:
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.
Best Regards,
Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]
Add and Follow me on Facebook: https://www.facebook.com/donzzsky
Visit and like my page on Facebook at: https://www.facebook.com/BermzISware
- import javax.swing.*; //used to access JFrame and JTextArea class
- import java.awt.*; //use to access the GridLayout class
- textField1.setText("Drag this text to the textfield below.");
- textField2.setText("You can also drag this text above.");
- textField2.setDragEnabled(true);
- textField1.setDragEnabled(true);
- frame.getContentPane().add(textField2);
- frame.getContentPane().add(textField1);
- frame.pack();
- frame.setVisible(true);
- import javax.swing.*; //used to access JFrame and JTextArea class
- import java.awt.*; //use to access the GridLayout class
- public class dragDrop {
- textField1.setText("Drag this text to the textfield below.");
- textField2.setText("You can also drag this text above.");
- textField2.setDragEnabled(true);
- textField1.setDragEnabled(true);
- frame.getContentPane().add(textField2);
- frame.getContentPane().add(textField1);
- frame.pack();
- frame.setVisible(true);
- }
- }
Add new comment
- 103 views