3

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)

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Krath
  • 101
  • 1
  • 1
  • 10
  • Possible duplicate of [Update JTextFields text every change of variable](https://stackoverflow.com/questions/21095225/update-jtextfields-text-every-change-of-variable) – simonalexander2005 Jun 26 '18 at 13:51

1 Answers1

6

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.

npinti
  • 51,780
  • 5
  • 72
  • 96
  • thanks for that - I think I understand it - how would I get the database to run an event that tells the server to update? – Krath Feb 17 '12 at 07:59
  • @Krath: To my knowledge, you cannot get the database to do that, but if you have control on the application which updates the database, then, it can fire some event, like writing to a file, or maybe even setting a flag column in the database. You can also read from the database and determine if the `JTextArea` needs updating from your own application. – npinti Feb 17 '12 at 08:27