Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JPanel update/redraw

Status
Not open for further replies.

sparafucile17

Programmer
Apr 5, 2002
40
US
Ok, I have a class that extends a JPanel, that is pretty much my primary GUI interface. I have a JFrame that adds this inherited JPanel and uses it as the GUI display. The problem is that I have a JLabel: connStatus in the JPanel that needs to be updated depending on the remote connection status.

What I have is a method called setConnection Status that looks like this:

public void setConnectionStatus(String s)
{
connStatus.setText("Status: "+s);
}

So here's the problem: The text will not change (i.e. be redrawn) until the main thread finishes processing some database data. What I'm really looking for here is a method that will force the JPanel to redraw. I know in VC++ there was a function called update() for forcing the GUI to redraw.

Does anyone know of a Java equivalent for this? Also, keep in mind I'm really only looking for a single-threaded solutions. At this point I'm not looking to over-complicate things just so a Label will activate at the right time.

Regards,
Jeff
 
Hi Jeff,

Yeah, this is a common problem. Although repaint() will redraw the screen, System is not going to get a chance to do the update because you are doing your database processing.

Having said that, I notice there is a repaint() method that takes a long value to do the Component update within that many milliseconds. This is a new one on me and may do the job.

Otherwise you are looking at threading, where you create and run a new Thread or Runnable class that does your database processing. This frees up the main thread to do the repaint. Generally you would disable things like buttons on your GUI, and have the Thread re-enable them when it is done. You can get really fancy and have the Thread updating a JProgressBar as it does its processing.

Threaded Programs Code Samples:
Threads and Swing:
Best of luck,
scrat
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top