Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically update JTextArea

Completely new to Java and I'm at a complete brick wall.

I have a JTextArea on my system that I'd like to have a live update, so when something is added to table2 (in my database), my server pulls the new values from the database and then updates the JTextArea.

I have absolutely no idea how to do this, though I have worked out that I need to use Thread to get it to work.

Any/all help is greatly appreciated (I'm a little pressed for time with this)

like image 481
Krath Avatar asked Mar 28 '26 21:03

Krath


1 Answers

What you can do is have your thread poll your database at given periods of time, or else, have the process which is updating the database fire some kind of event which your GUI class can pick up.

Once this happens, you can then use the SwingUtilities.invokeLater() to update your JTextArea. Something like this should do:

if (eventIsFired)
{
    final String jtextAreaText = ...
    SwingUtilities.invokeLater(new Runnable()
    {
        @Override
        public void run()
        {
            jTextArea.setText(jTextAreaText);
        }            
    });
}

The assumption is that jTextArea is your actual JTextArea which is declared as a global variable. jTextAreaText will need to be declared final so that it can be accessed through the inner class.

like image 110
npinti Avatar answered Mar 30 '26 09:03

npinti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!