2

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..

Ernestas Gruodis
  • 8,567
  • 14
  • 55
  • 117
  • 1
    [common issue JComponents must be reseted or replaced before its container is hidden, see and to test](http://stackoverflow.com/a/18253888/714968) – mKorbel Aug 20 '13 at 18:17
  • Hmm.., so no good use of `ComponentListener` in this situation. So now I manually set `textField.setText(null);` for every component in my dialog window before it's set to invisible. – Ernestas Gruodis Aug 20 '13 at 18:28
  • have to calculating that some, espacially low level listeners can be asynchronous, there container loose Focus, Focus is one of most .... – mKorbel Aug 20 '13 at 18:30
  • 1
    out of the record textField.setText(null); != JDialod.revalidate() & JDialog.repaint(), everything its about repaint() – mKorbel Aug 20 '13 at 18:33
  • Yes, I had also to do `textField.repaint();` - it is not enough just to clear the text before setting it's parent to invisible. – Ernestas Gruodis Aug 20 '13 at 21:19
  • I noticed some problems again.. But found the solution - below is the answer. – Ernestas Gruodis Aug 21 '13 at 23:32

1 Answers1

1

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.. :)

Ernestas Gruodis
  • 8,567
  • 14
  • 55
  • 117
  • right it could be, then didn't you thinking about animation ---> fade out durrng one second/half of second, because how much event will be executed during this short period, my question whats Java (target) version??? – mKorbel Aug 22 '13 at 06:19
  • Java version is 1.7.0_25-b16. Yes, fade out is a good idea, but in this particular design I simply do not want to do it. But on my custom popup menu - menu items on mouse entered/exited behave so - very nice result. – Ernestas Gruodis Aug 22 '13 at 08:13
  • I see that (.Net, C++) by replacing form with an image simulating the form, and thenimage is fade out, illusion, illusion, nobody can catch that – mKorbel Aug 22 '13 at 08:21