Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Lostfocus and JDialog

Status
Not open for further replies.

SSJ

Programmer
Sep 26, 2002
54
PT
Hi. I'm in a middle of a situation I'm not sure how should I solve it. I'm using swing to build some forms (using JFrames in this case), and I use the lostFocus event on one JTextField to trigger some actions according to the text on that textfield. If the text entered on that text field is invalid I pop up a JDialog showing an error message. So far so good.

The only problem is if the user enters an invalid text into the JTextField and just after that presses the close button on the JFrame, the lostFocus event will trigger the JDialog to show, but since the original JFrame is closing my application will stall completly.

This behaviour happens all the time (I'm not sure it's a bug on jdk or if this is the way it should be...), so I suppose anyone might happened to face it before me, if so how should I solve it? I think the only thing I need is to avoid showing the JDialog if the JFrame is closing, but is there any way I can check JFrame's state?

Any help is appreciated

TIA
 
Sorry, but when I mentioned JDialog I want to say JOptionPane, that's what I'm using to show the messages (actually is a subclass of it).

Here is a quick example of the behaviour I'm speaking:
(Just try to click the close button of the JFrame)

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class testJFrame extends JFrame {
JPanel jPanel1 = new JPanel();
JTextField jTextField1 = new JTextField();

public testJFrame() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}

private void jbInit() throws Exception {
jTextField1.setText("jTextField1");
jTextField1.addFocusListener(new testJFrame_jTextField1_focusAdapter(this));
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jTextField1, null);
}

void jTextField1_focusLost(FocusEvent e) {
JOptionPane ddd = new JOptionPane();
ddd.showConfirmDialog(this,"TEST");
}

// main method
public static void main(String[] args) {
// Set the look and feel.
try {
UIManager.setLookAndFeel(
UIManager.getCrossPlatformLookAndFeelClassName());
} catch(Exception e) {}

testJFrame ttt = new testJFrame();
ttt.show();
}


}

class testJFrame_jTextField1_focusAdapter extends java.awt.event.FocusAdapter {
testJFrame adaptee;

testJFrame_jTextField1_focusAdapter(testJFrame adaptee) {
this.adaptee = adaptee;
}
public void focusLost(FocusEvent e) {
adaptee.jTextField1_focusLost(e);
}
}
 
ok... I think I solved it. What I did is set a boolean var on my class that subclasses JFrame that tells if the JFrame is closing or not (I update it in the WindowClosing event), then at the lostFocus event I check that var and I only execute the code if it's false.
Anyway although things are working now if anyone knows any other way to go around it (better?) I'd like to hear.
 
Just found out that this is a bug with jdk1.4, this kind of behaviour doesn't happen if you run this code with jdk1.3.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top