Wednesday, August 17, 2011

Copy Selected Text From JTextArea to JTextField

Design Free Website with Free Internet Marketing Tools

Program Description:

The program below is a java code that lets you copy the selected text you want from JTextArea to JTextField by just highlighting the text or string you want.

Output:
Code:

/**
 * File: copyJTextAreaSelectedText.java
 * Tiltle: Copy Selected Text From JTextArea to JTextField
 * Author: http://java-program-sample.blogspot.com
 */

//Java Core Package
import javax.swing.*;
//Java Extension Package
import java.awt.*;
import java.awt.event.*;

public class copyJTextAreaSelectedText extends JFrame {
	
	//Initializing JTextArea, JButton, and JTextField
	private JTextArea txtArea;
	private JButton copy;
	private JTextField display;

	//Setting up GUI
    public copyJTextAreaSelectedText() {
    	
    	//Setting up the Title of the Window
    	super("Copy Selected Text From JTextArea to JTextField");

    	//Set Size of the Window (WIDTH, HEIGHT)
    	setSize(380,255);

    	//Exit Property of the Window
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	   	
    	//Constructing JTextArea, JButton, and JTextField
    	txtArea = new JTextArea("Type the Text you want here. . . .", 10,31);
    	copy = new JButton("Copy the Selected Text >>");
    	display = new JTextField(32);
    	
    	//JTextArea Property
    	txtArea.setLineWrap(true); //Disable the Horizontal Scrollbar
    	
    	//Constructing a JScrollPane for JTextArea's Horizontal and Vertical Scroll Bar
    	JScrollPane scroller = new JScrollPane(txtArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    	
    	//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 JButton copy
		copy.addActionListener(
		new ActionListener() {
			
			//Handle JButton event if it is clicked
			public void actionPerformed(ActionEvent event) {
				
				//This will display the selected text from JTextArea to JTextField by highlighting the text or string you want to copy.
				display.setText(txtArea.getSelectedText());
			}
		}
		);

		//Adding the JScrollPane to the Container with JTextArea on it
		//Adding JButton and JTextField to the container
		pane.add(scroller);
		pane.add(copy);
		pane.add(display);
		
    	/**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) {
    	copyJTextAreaSelectedText pjtf = new copyJTextAreaSelectedText();
	}
}

Important Part of the Program:

//Implemeting Even-Listener on JButton copy
		copy.addActionListener(
		new ActionListener() {
			
			//Handle JButton event if it is clicked
			public void actionPerformed(ActionEvent event) {
				
				//This will display the selected text from JTextArea to JTextField by highlighting the text or string you want to copy.
				display.setText(txtArea.getSelectedText());
			}
		}
		);

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Hostgator Discount Code