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:
import javax.swing.*; //used to access JFrame and JTextArea class
import java.awt.*; //use to access the GridLayout class
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.
textField1.setText("Drag this text to the textfield below.");
textField2.setText("You can also drag this text above.");
To have a drag and drop between the two textfields, we will use the
setDragEnabled method returns to True.
textField2.setDragEnabled(true);
textField1.setDragEnabled(true);
5. Now, we will have its layout into Grid Layout using the setLayout method of the frame.
frame.
getContentPane().
setLayout(new GridLayout(0,
1));
Add all the textfields to the frame using the add method.
frame.getContentPane().add(textField2);
frame.getContentPane().add(textField1);
Lastly, pack the frame, set visibility to true, and the close operation of the frame. Have this code below:
frame.pack();
frame.setVisible(true);
frame.
setDefaultCloseOperation(JFrame.
EXIT_ON_CLOSE);
Output:
Here's the full code of this tutorial:
import javax.swing.*; //used to access JFrame and JTextArea class
import java.awt.*; //use to access the GridLayout class
public class dragDrop {
public static void main
(String[] args
) {
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().
setLayout(new GridLayout(0,
1)); frame.getContentPane().add(textField2);
frame.getContentPane().add(textField1);
frame.pack();
frame.setVisible(true);
frame.
setDefaultCloseOperation(JFrame.
EXIT_ON_CLOSE);
}
}
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