Output:
Code:
/**
* File: textFieldToTextArea.java
* Tiltle: Copy Text from JTextField to JTextArea
* 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 textFieldToTextArea extends JFrame {
//Initializing JTextField and JTextArea
private JTextField field;
private JTextArea area;
//Setting up GUI
public textFieldToTextArea() {
//Setting up the Title of the Window
super("Copy Text from JTextField to JTextArea");
//Set Size of the Window (WIDTH, HEIGHT)
setSize(310,225);
//Exit Property of the Window
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Constructing JTextField with a size of 26
field = new JTextField(26);
//Constructing JTextArea with a LENGTH:10 WIDTH:26
area = new JTextArea(10,26);
//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.CENTER);
pane.setLayout(flow);
//Implemeting Even-Listener on JTextField's reference name "field" using ActionListener
field.addActionListener(
new ActionListener() {
//Handle JTextField event if Enter key is pressed
public void actionPerformed(ActionEvent event) {
//Copy Text from JTextField to JTextArea
area.setText(field.getText());
//The JTextField will be empty after Enter key is pressed ready for the next input.
field.setText(null);
}
}
);
//Adding the JTextField and JTextArea to the container
pane.add(field);
pane.add(area);
/**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) {
textFieldToTextArea jtta = new textFieldToTextArea();
}
}
Important Part of the Program:
//Implemeting Even-Listener on JTextField's reference name "field" using ActionListener
field.addActionListener(
new ActionListener() {
//Handle JTextField event if Enter key is pressed
public void actionPerformed(ActionEvent event) {
//Copy Text from JTextField to JTextArea
area.setText(field.getText());
//The JTextField will be empty after Enter key is pressed ready for the next input.
field.setText(null);
}
}
);
No comments:
Post a Comment