Friday, August 5, 2011

Construct a JTextField using Array with Event Listener

Output:
Code:

/**

* File: arrayJTextField.java

* Tiltle: Construct a JTextField using Array with Event Listener

* 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 arrayJTextField extends JFrame {



//Initializing JTextField, JPanel, JLabel

private JTextField field[];

private JPanel panel;

private JLabel caption;



//Setting up GUI

public arrayJTextField() {



//Setting up the Title of the Window

super("Construct a JTextField using Array with Event Listener");



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

setSize(400,250);



//Exit Property of the Window

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



//Constructing JTextField with an array size of 5

field = new JTextField[5];



//Constructing JLabel which is used to display text if the action is successfully implemented.

caption = new JLabel("No Action Yet...");

caption.setHorizontalAlignment(SwingConstants.CENTER); //Align the JLabel to Center



//Constructing JPanel for group of JTextFields with a GridLayout of 6 rows and 1 column

panel = new JPanel();

panel.setLayout(new GridLayout(6,1)); //Set JPanel's Layout

panel.add(caption); //Adding the JLabel in the JPanel placing at the very top



//Constructing all 5 JTextFields with a size of 20 using "for loop"

for(int count=0; count<field.length; count++) {

field[count] = new JTextField("Field "+(count+1),20);

panel.add(field[count]);

}



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

Container pane = getContentPane();

setContentPane(pane);



//Implemeting Even-Listener on JTextField's reference name "field[0]" using ActionListener

field[0].addActionListener(

new ActionListener() {



//Handle JTextField event if Enter key is pressed

public void actionPerformed(ActionEvent event) {



/**Display this message using JLabel if the action

is Successfully Implemented using the method setText()

*/

caption.setText("Field 1 Event Listener Successfully Implemented");

}

}

);



//Implemeting Even-Listener on JTextField's reference name "field[1]" using ActionListener

field[1].addActionListener(

new ActionListener() {



//Handle JTextField event if Enter key is pressed

public void actionPerformed(ActionEvent event) {



/**Display this message using JLabel if the action

is Successfully Implemented using the method setText()

*/

caption.setText("Field 2 Event Listener Successfully Implemented");

}

}

);



//Implemeting Even-Listener on JTextField's reference name "field[2]" using ActionListener

field[2].addActionListener(

new ActionListener() {



//Handle JTextField event if Enter key is pressed

public void actionPerformed(ActionEvent event) {



/**Display this message using JLabel if the action

is Successfully Implemented using the method setText()

*/

caption.setText("Field 3 Event Listener Successfully Implemented");

}

}

);



//Implemeting Even-Listener on JTextField's reference name "field[3]" using ActionListener

field[3].addActionListener(

new ActionListener() {



//Handle JTextField event if Enter key is pressed

public void actionPerformed(ActionEvent event) {



/**Display this message using JLabel if the action

is Successfully Implemented using the method setText()

*/

caption.setText("Field 4 Event Listener Successfully Implemented");

}

}

);



//Implemeting Even-Listener on JTextField's reference name "field[4]" using ActionListener

field[4].addActionListener(

new ActionListener() {



//Handle JTextField event if Enter key is pressed

public void actionPerformed(ActionEvent event) {



/**Display this message using JLabel if the action

is Successfully Implemented using the method setText()

*/

caption.setText("Field 5 Event Listener Successfully Implemented");

}

}

);



//Adding the JPanel to the container

pane.add(panel);



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

arrayJTextField pjtf = new arrayJTextField();

}

}


Important Part of the Program:

//Constructing JTextField with an array size of 5

JTextField field = new JTextField[5];



//Constructing JPanel for group of JTextFields with a GridLayout of 6 rows and 1 column

JPanel panel = new JPanel();

panel.setLayout(new GridLayout(6,1)); //Set JPanel's Layout

panel.add(caption); //Adding the JLabel in the JPanel placing at the very top



//Constructing all 5 JTextFields with a size of 20 using "for loop"

for(int count=0; count<field.length; count++) {

field[count] = new JTextField("Field "+(count+1),20);

panel.add(field[count]);

}



//Implemeting Even-Listener on JTextField's reference name "field[0]" using ActionListener

field[0].addActionListener(

new ActionListener() {



//Handle JTextField event if Enter key is pressed

public void actionPerformed(ActionEvent event) {



/**Display this message using JLabel if the action

is Successfully Implemented using the method setText()

*/

caption.setText("Field 1 Event Listener Successfully Implemented");

}

}

);



//Implemeting Even-Listener on JTextField's reference name "field[1]" using ActionListener

field[1].addActionListener(

new ActionListener() {



//Handle JTextField event if Enter key is pressed

public void actionPerformed(ActionEvent event) {



/**Display this message using JLabel if the action

is Successfully Implemented using the method setText()

*/

caption.setText("Field 2 Event Listener Successfully Implemented");

}

}

);



//Implemeting Even-Listener on JTextField's reference name "field[2]" using ActionListener

field[2].addActionListener(

new ActionListener() {



//Handle JTextField event if Enter key is pressed

public void actionPerformed(ActionEvent event) {



/**Display this message using JLabel if the action

is Successfully Implemented using the method setText()

*/

caption.setText("Field 3 Event Listener Successfully Implemented");

}

}

);



//Implemeting Even-Listener on JTextField's reference name "field[3]" using ActionListener

field[3].addActionListener(

new ActionListener() {



//Handle JTextField event if Enter key is pressed

public void actionPerformed(ActionEvent event) {



/**Display this message using JLabel if the action

is Successfully Implemented using the method setText()

*/

caption.setText("Field 4 Event Listener Successfully Implemented");

}

}

);



//Implemeting Even-Listener on JTextField's reference name "field[4]" using ActionListener

field[4].addActionListener(

new ActionListener() {



//Handle JTextField event if Enter key is pressed

public void actionPerformed(ActionEvent event) {



/**Display this message using JLabel if the action

is Successfully Implemented using the method setText()

*/

caption.setText("Field 5 Event Listener Successfully Implemented");

}

}

);



//Adding the JPanel to the container

pane.add(panel);

No comments:

Post a Comment