This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Focus Events Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Focus Events" Watch "Focus Events" New topic

Focus Events

Jerry Kilpatrick

Joined: Jan 02, 2005
Posts: 6
I am writing an application in which I want to verify the format of a JTextField whenever the user tabs out of the field. I am attaching a stripped down version that creates two text fields and a focus listener. When I tab out of the text field a JOptionPane is displayed to tell the user that there is an error in the text field. After that, I call requestFocusInWindow() to get the focus back into the original field.

My problem is that I have to click "ok" three times in the JOptionPane before I get back to the text field. It seems that I should get back on the first click.

Any suggestions?



* Created on January 14, 2005, 3:47 PM

package FocusDemo;

import java.awt.BorderLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class FocusDemo {
static DisplayPanel displayPanel;

public FocusDemo() {

public static void main(String[] args) {
FocusDemo budget = new FocusDemo();
displayPanel = new DisplayPanel();
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {

static void displayIt() {

class DisplayPanel extends JPanel implements FocusListener {
static JTextField tf1, tf2;
JFrame frame;
int i = 0;

void display() {
frame = new JFrame("FocusDemo");
this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

tf1 = new JTextField("Text Field 1", 25);
tf2 = new JTextField("Text Field 2", 25);

frame.getContentPane().add(this, BorderLayout.PAGE_START);

public void focusGained(FocusEvent e) {
System.out.println("Focus Gained " + i++);
System.out.println(" ");
public void focusLost(FocusEvent e) {
System.out.println("Focus Lost " + i++);
System.out.println(" ");

// The following code is attempting to get control when I tab out of a text field and
// determine if the field is correctly formatted. If not, a JOptionPane is used to
// display an error message.
if (!e.isTemporary()) {
if (e.getSource() == tf1) {
JOptionPane.showMessageDialog(frame, "Please Enter Valid Text",
"Text Format Error", JOptionPane.ERROR_MESSAGE);



Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
The InputVerifier class was designed to make this easy. Its api has an example showing how to use it.
Jerry Kilpatrick

Joined: Jan 02, 2005
Posts: 6

Thanks for you help. The InputVerifier class works like a charm.

I agree. Here's the link:
subject: Focus Events
Similar Threads
Question from Andrew's sample code - about SavedConfiguration class
regarding focus in swing
Capturing all type of inputs in a JTextField
Getting sql error [Microsoft ][odbc microsoft access driver]numeric index out of range
Tab Key Binding JTextField