Friday, July 29, 2011

Copy Text from JTextField to JTextField

Output:
Code:

/**

* File: textFieldToTextField.java

* Tiltle: Copy Text from JTextField to JTextField

* Author: http://java-code-complete.blogspot.com

*/



//Java Core Package

import javax.swing.*;

//Java Extension Package

import java.awt.*;

import java.awt.event.*;



public class textFieldToTextField extends JFrame {



//Initializing JTextField

private JTextField field1, field2;



//Setting up GUI

public textFieldToTextField() {



//Setting up the Title of the Window

super("Copy Text from JTextField to JTextField");



//Set Size of the Window (WIDTH, HEIGHT)

setSize(320,100);



//Exit Property of the Window

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



//Constructing JTextField with a size of 20

field1 = new JTextField(20);

field2 = new JTextField(20);



//Setting up the container ready for the components to be added.

Container pane = getContentPane();

setContentPane(pane);



//Setting up the container layout

FlowLayout flow = new FlowLayout(FlowLayout.LEFT);

pane.setLayout(flow);



//Implemeting Even-Listener on JTextField's reference name "field1" using ActionListener

field1.addActionListener(

new ActionListener() {



//Handle JTextField event if Enter key is pressed

public void actionPerformed(ActionEvent event) {



//Copy Text from JTextField field1 to field2

field2.setText(field1.getText());



//The JTextField will be empty after Enter key is pressed ready for the next input.

field1.setText(null);

}

}

);



//Implemeting Even-Listener on JTextField's reference name "field2" using ActionListener

field2.addActionListener(

new ActionListener() {



//Handle JTextField event if Enter key is pressed

public void actionPerformed(ActionEvent event) {



//Copy Text from JTextField field2 to field1

field1.setText(field2.getText());



//The JTextField will be empty after Enter key is pressed ready for the next input.

field2.setText(null);

}

}

);



//Adding the JTextField components to the container

pane.add(field1);

pane.add(field2);



/**Set all the Components Visible.

* If it is set to "false", the components in the container will not be visible.

*/

setVisible(true);

}



//Main Method

public static void main (String[] args) {

textFieldToTextField jtff = new textFieldToTextField();

}

}


Important Part of the Program:

field1.addActionListener(

new ActionListener() {



//Handle JTextField event if Enter key is pressed

public void actionPerformed(ActionEvent event) {



//Copy Text from JTextField field1 to field2

field2.setText(field1.getText());



//The JTextField will be empty after Enter key is pressed ready for the next input.

field1.setText(null);

}

}

);



//Implemeting Even-Listener on JTextField's reference name "field2" using ActionListener

field2.addActionListener(

new ActionListener() {



//Handle JTextField event if Enter key is pressed

public void actionPerformed(ActionEvent event) {



//Copy Text from JTextField field2 to field1

field1.setText(field2.getText());



//The JTextField will be empty after Enter key is pressed ready for the next input.

field2.setText(null);

}

}

);

No comments:

Post a Comment