Thursday, July 28, 2011

Implementing Event-Listener to JTextField

Design Free Website with Free Internet Marketing Tools

Program Description:

The program below is a simple Java Code that demonstrates on how to implement an Event-Listener on JTextField in order to create an interaction to the user if enter key is pressed. If the event is successfully implemented, a popup message will appear after the enter key is pressed.


Output:
Code:

/**
 * File: jtextfieldEvent.java
 * Tiltle: Implementing Event-Listener 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 jtextfieldEvent extends JFrame {
 
 //Initializing JTextField
 private JTextField field;

 //Setting up GUI
    public jtextfieldEvent() {
     
     //Setting up the Title of the Window
     super("Implementing Event-Listener");

     //Set Size of the Window (WIDTH, HEIGHT)
     setSize(250,100);

     //Exit Property of the Window
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     //Constructing JTextField with a size of 20 with a specified string "Enter Text Here..."
     field = new JTextField("Enter Text Here...",20);

     //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" using ActionListener
  field.addActionListener(
  new ActionListener() {
   
   //Handle JTextField event if Enter key is pressed
   public void actionPerformed(ActionEvent event) {
    
    String message = field.getText(); //Store the input from JTextField to the variable "message"
    
    //Display the input from JTextField to JOptionPane using the variable "message"
    JOptionPane.showMessageDialog(null, message,"Action Successfully Implemented!",JOptionPane.INFORMATION_MESSAGE);
    
    //The JTextField will be empty after JOptionPane is closed ready for the next input.
    field.setText(null);

   }
  }
  );

     //Adding the JTextField component to the container
     pane.add(field, BorderLayout.NORTH);

     /**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) {
     jtextfieldEvent jtfe = new jtextfieldEvent();
 }
}

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) {
    
    String message = field.getText(); //Store the input from JTextField to the variable "message"
    
    //Display the input from JTextField to JOptionPane using the variable "message"
    JOptionPane.showMessageDialog(null, message,"Action Successfully Implemented!",JOptionPane.INFORMATION_MESSAGE);
    
    //The JTextField will be empty after JOptionPane is closed ready for the next input.
    field.setText(null);

   }
  }
  );

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