update then update textfield

Hey, I’m trying to use a textfield to display a value, and then I’m trying to change the value in 6 seconds

here’s the code I have so far:

                String password;
                password = Encrypter.staticDecrypt(user.getUserPass());
                
                pass.setValue(password);
                
                decryptTricker.setPasswordDecrypted(true);
                pass.commit();
                
                pass.valueChange(null);
               
                int timer = 0;
                while(timer < 6)
                {
                    try {
                        TimeUnit.SECONDS.sleep(1);
                        timer += 1;
                    } catch (InterruptedException ex) {
                        Logger.getLogger(UserTab.class.getName()).log(Level.SEVERE, null, ex);
                }
               pass.setValue(EncryptedPW);
            }

Thanks!

First of all, what are you trying to do, I mean, not technically, but from a usability perspective? To be honest, the behavior looks a bit weird.

Then to answer the technical problem. Your question goes deep down into how web applications and HTTP requests work. First we need some basic understanding, I’ll try to give a brief explanation. When you do an action in the browser, such as clicking on a button, it triggers an HTTP request to the server. On the server, there is a thread that processes the HTTP request. For example, you receive a click event, you have some application specific logic in the listener, you do some stuff, the thread continues processing the request and when
everything
has been processed, then an HTTP response is sent to the browser. The HTTP response contains the instructions for the browser to change the UI - this is the point when you see changes in the browser.

In practice, what this means, is that your code does change the value of the TextField twice (in the beginning and six seconds later), but since it’s all in the thread that handles the HTTP request. The browser will not get the TextField’s value until the end of the HTTP request, meaning, when the value has already been changed back to the encrypted value.

You need to take a bit different approach in order to show one value in the browser, then wait six seconds, then show another value. What you need to do is have the timer in a separate thread that is executed outside the HTTP request. This allows the HTTP request to finish and the textfield’s value to change to the decrypted value.

If you do UI modifications in a non-HTTP request thread, then you still have the problem of how do you get any UI modifications automatically to the browser. For that, you’ll need to use, for example,
push
.

Another completely different approach is to create a client-side widget/extension that receives both the encrypted and decrypted values, and then executes a client-side timer (basically, timer is ran in javascript, not java) which triggers the value change.

That said, I’d still focus on the first line of this post. I’d be more curious about
why
you are doing that rather than
how
.