Reputation: 8787
I have added ComponentListener
to JTextField
parent like this:
parent.addComponentListener(new ComponentAdapter() {
@Override
public void componentHidden(ComponentEvent e) {
setText("");
}
});
So after parent becomes invisible, textField
text is set to "". Everything works, but the problem is when I set parent to visible - for some milliseconds previous text of textField
is displayed, and then field becomes empty. So it's not very nice..
Upvotes: 2
Views: 397
Reputation: 8787
Before setting the parent to invisible, textField
field is set textField.setText(null);
(no need to repaint), then the problem is with focus - it must be set to some initial component like panel.requestFocusInWindow();
. But the focus is not always set right in time. So Timer
class solved the problem:
textField.setText(null);
panel.requestFocusInWindow();
final int timeout = 5;
Timer timer = new Timer(timeout, new ActionListener() {
int a = 0;
@Override
public void actionPerformed(ActionEvent e) {
if (panel.isFocusOwner() || a > 500) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
setVisible(false);
}
});
((Timer) e.getSource()).stop();
}
a += timeout;
}
});
timer.setRepeats(true);
timer.start();
Now Dialog window (parent) if setVisible(true);
shows as newly created - without blinking textFields and focused right. Finally.. :)
Upvotes: 1