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);

}

}

);

Copy Text from JTextField to JTextArea

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);

}

}

);

Add Items on JList using JTextField

Output:
Code:

/**

* File: jtextfieldToJList.java

* Tiltle: Add Items on JList using JTextField

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

*/



//Java Extension Packages

import javax.swing.*;

//Java Core Packages

import java.awt.*;

import java.awt.event.*;



public class jtextfieldToJList extends JFrame {



//Initializing JTextField, JList, and DefaultListModel class

private DefaultListModel model;

private JList list;

private JTextField input;



//Setting up GUI

public jtextfieldToJList() {



//Setting up the Title of the Window

super("Add Item on JList using JTextField");



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

setSize(280,170);



//Exit Property of the Window

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



//Constructing JTextField, JList, and DefaultListModel

model = new DefaultListModel();

input = new JTextField("Type your Inputs Here and Press Enter");

list = new JList(model);



//Setting JList Properties

list.setVisibleRowCount(8); //Number of Itmes to be displayed. If greater than 8, Vertical ScrollBar will be displayed.

list.setFixedCellHeight(15); //Fix width of the JList

list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);



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

Container pane = getContentPane();

setContentPane(pane);



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

input.addActionListener(

new ActionListener() {



//Handle JTextField event if Enter key is pressed

public void actionPerformed(ActionEvent event) {



//Store the input from JTextField to the variable "message"

String message = "The input <"+input.getText()+"> is Successfully Added!";



//Adding items or elements from JTextField to JList

model.addElement(input.getText());



//Display the input from JTextField to JOptionPane using the variable "message"

JOptionPane.showMessageDialog(null, message,"Successfully Added!",JOptionPane.INFORMATION_MESSAGE);



//The JTextField will be empty after JOptionPane is closed ready for the next input.

input.setText(null);

}

}

);



//Adding JTextField in the container with a BorderLayout of NORTH

pane.add(input,BorderLayout.NORTH);



//Adding the JList in the container with a component JScrollPane that's automatically creates Vertical and Horizontal Scroll Bar

//if the items or elements are greater than the specified "VisibleRowCount" in line 38.

pane.add(new JScrollPane(list),BorderLayout.SOUTH);



/**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) {

jtextfieldToJList aijl = new jtextfieldToJList();

}

}


Important Part of the Program:

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

input.addActionListener(

new ActionListener() {



//Handle JTextField event if Enter key is pressed

public void actionPerformed(ActionEvent event) {



//Store the input from JTextField to the variable "message"

String message = "The input <"+input.getText()+"> is Successfully Added!";



//Adding items or elements from JTextField to JList

model.addElement(input.getText());



//Display the input from JTextField to JOptionPane using the variable "message"

JOptionPane.showMessageDialog(null, message,"Successfully Added!",JOptionPane.INFORMATION_MESSAGE);



//The JTextField will be empty after JOptionPane is closed ready for the next input.

input.setText(null);

}

}

);

Set JRadioButton Label using JTextField

Program Description:

The program below is a simple Java Code that lets you customize you own JRadioButton label using JTextField so in this way, your JRadioButton label is changeable. It gives you the capability to change its label easily without going in to codes.

Output:
Code:

/**

* File: changeJRadioButtonLabel.java

* Tiltle: Set JRadioButton Label using 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 changeJRadioButtonLabel extends JFrame {



//Initializing JTextField and JRadioButton

private JTextField field;

private JRadioButton button;



//Setting up GUI

public changeJRadioButtonLabel() {



//Setting up the Title of the Window

super("Set JRadioButton Label using JTextField");



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

setSize(310,90);



//Exit Property of the Window

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



//Constructing JTextField and JRadioButton

field = new JTextField("Enter Text Here...",25);

button = new JRadioButton("Default Label",true);



//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) {



//Setting JRadioButton label using JTextField

button.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 JRadioButton components to the container

pane.add(field);

pane.add(button);



/**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) {

changeJRadioButtonLabel jtrl = new changeJRadioButtonLabel();

}

}


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) {



//Setting JRadioButton label using JTextField

button.setText(field.getText());



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

field.setText(null);

}

}

);

Set JCheckBox Label using JTextField

Output:
Code:

/**

* File: changeJCheckBoxLabel.java

* Tiltle: Set JCheckBox Label using 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 changeJCheckBoxLabel extends JFrame {



//Initializing JTextField and JCheckBox

private JTextField field;

private JCheckBox box;



//Setting up GUI

public changeJCheckBoxLabel() {



//Setting up the Title of the Window

super("Set JCheckBox Label using JTextField");



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

setSize(310,90);



//Exit Property of the Window

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



//Constructing JTextField and JCheckBox

field = new JTextField("Enter Text Here...",25);

box = new JCheckBox("Default Label",true);



//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) {



//Setting JCheckBox label using JTextField

box.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 JCheckBox components to the container

pane.add(field);

pane.add(box);



/**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) {

changeJCheckBoxLabel cjcb = new changeJCheckBoxLabel();

}

}


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) {



//Setting JCheckBox label using JTextField

box.setText(field.getText());



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

field.setText(null);

}

}

);