aspose file tools*
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 Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Focus Events" Watch "Focus Events" New topic
Author

Focus Events

Jerry Kilpatrick
Greenhorn

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?

Thanks,

Jerry

/*
* FocusDemo.java
*
* 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() {
displayIt();
}
});
}

static void displayIt() {
displayPanel.display();
}
}

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

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

tf1 = new JTextField("Text Field 1", 25);
tf1.addFocusListener(this);
tf2 = new JTextField("Text Field 2", 25);
tf2.addFocusListener(this);
this.add(tf1);
this.add(tf2);

frame.getContentPane().add(this, BorderLayout.PAGE_START);
frame.pack();
frame.setVisible(true);
}

public void focusGained(FocusEvent e) {
System.out.println("Focus Gained " + i++);
System.out.println(e.isTemporary());
System.out.println(e.getSource());
System.out.println(e.getOppositeComponent());
System.out.println(" ");
}
public void focusLost(FocusEvent e) {
System.out.println("Focus Lost " + i++);
System.out.println(e.isTemporary());
System.out.println(e.getSource());
System.out.println(e.getOppositeComponent());
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);
tf1.requestFocusInWindow();

}
}

}

}
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
Greenhorn

Joined: Jan 02, 2005
Posts: 6
Craig,

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

Jerry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Focus Events