Monday, December 28, 2009

Capture on exit event of a TextField/JTextField in Java

/*==============================================================================
* Author : Karthikeyan A - MaargaSystems pvt.;td
* Created : 04-May-2009
* Purpose : illustrate the usage of on FoucsListener Class by Capturing on exit event of a TextField/JTextField
*==============================================================================
*/

import java.awt.BorderLayout;
import java.awt.Panel;
import java.awt.TextField;

import java.awt.Frame;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

//import javax.swing.*;
//import java.awt.*;
//import java.awt.event.*;
public class TextFieldTask {

public void onExit_TextField(TextField tf) {
tf.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent arg0) {
System.out.println("gained focus");
}

public void focusLost(FocusEvent arg0) {
System.out.println("lost focus");
msgbox("lost focus","captured on exit");

}
});
}

void msgbox(String message,String title) {

Object[] options = {"Ok"};
javax.swing.JFrame frame=new javax.swing.JFrame();
int n = javax.swing.JOptionPane.showOptionDialog(frame,
message,
title,
javax.swing.JOptionPane.OK_OPTION,
0,
null,
options,
options[0]);
}

public static void main(String args[]){
final TextFieldTask tft=new TextFieldTask();
//JPanel jp=new JPanel();
//JTextField tf1=new JTextField();
//JTextField tf2=new JTextField();

Panel p=new Panel();
TextField tf1=new TextField();
TextField tf2=new TextField();

tft.onExit_TextField(tf1);

//jp.setLayout(new BorderLayout());
//jp.add(tf1,BorderLayout.NORTH);
//jp.add(tf2,BorderLayout.SOUTH);
p.setLayout(new BorderLayout());
p.add(tf1,BorderLayout.NORTH);
p.add(tf2,BorderLayout.SOUTH);

//JFrame jf= new JFrame("Test");
//jf.add(jp);
//jf.setSize(100,100);
//jf.setVisible(true);
//jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Frame f=new Frame("test");
f.add(p);
f.setSize(100,100);
f.setVisible(true);

}
}

No comments:

Post a Comment