Simple problem updating GUI
Hello.
I have what looks like a simple problem, but I can't figure out a solution.
private Progress progress =new Progress();
final ProgressBar progressBar =new ProgressBar(composite, SWT.SMOOTH);
I have created a class (Progress) and a progress bar. This bar should be updated with this:
progressBar.setSelection(progress.get());
progress is updated correctly in another thread, and the only thing I can't do is make the progressBar display the current value o progress at all times. I can't do it in another thread, because that generates an invalid thread access exception, so what can I do?
I tried to do a paint listener and then constantly make the progress bar visible and invisible, but that looked ugly (lots of flashing) and wasn't efficient at all..
So, any suggestions? Seems like such an easy thing to do, but I can't figure it out...
[1041 byte] By [
joaomgcd1a] at [2007-10-1 23:34:38]

Thanks for your reply!
Sorry for that. I'll try posting more relevant code.
I have this class:
public class Progresso {
private int progresso = 0;
public synchronized int get() {
return progresso;
}
public synchronized void add() {
progresso=progresso+1024;
notifyAll();
}
And I have this progress bar:
final ProgressBar progressBar = new ProgressBar(composite, SWT.SMOOTH);
Everytime I want to update the progress bar this method is called:
public void byteWrite(int bytes)
{
progresso.add();
//MyEvent m = new MyEvent(this);
//fireMyEvent(m);
}
And the progress bar should be updated with this command:
progressBar.setSelection(progress.get());
I can't update the progress bar directly on the method above because SWT doesn't allow its GUI to be updated in a thread I created, apparentely, so that's why I created the "Progresso" class...
All this works well, save for the progressBar.setSelection(progress.get());
command. I don't know when or where I should invoke it, so the progress bar keeps updating.
I hope my problem is more understandable now. Thanks :)
SWT does not allow non-gui threads to update gui thread components.
So you have to make use of the "asyncExec(Runnable)" and "syncExec(Runnable)" calls in "Display".
The *Exec() call will allow the GUI thread to run whatever runnable object passed as a parameter and run the object in a separate gui thread.
For more detailed explanation take a look at the API. Here's a link http://download.eclipse.org/eclipse/downloads/documentation/2.0/html/plugins/org.eclipse.platform.doc.isv/reference/api/
I've included an example for clarity.
I've created two classes: SWTGuiExample, and GUIUpdater.
SWTGuiExample contains the GUI widgets and GUIUpdater updates the progress bar using SWTGuiExample's UpdateStatusBar method;
the interesting part for this discussion is in "public void UpdateProgressBar( final int selection_value )".
// -
// SWTGuiExample.java
// -
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Display;
import java.lang.Runnable;
public class SWTGuiExample
{
private Shellmain_shell;
private Displaymain_display;
private ProgressBar pBar_status;
private int pBar_selection;
public static final int PROGRESS_BY = 2;
public SWTGuiExample()
{
pBar_selection = 0;
}
public void BuildGUI()
{
// initialize display and shell
main_display = new Display();
main_shell= new Shell( main_display );
main_shell.setSize(500, 250);
// add progress bar
pBar_status = new ProgressBar( main_shell, SWT.SMOOTH );
pBar_status.setSize(400,20);
pBar_status.setMaximum( 100 );
pBar_status.setMinimum( 0 );
// open shell
main_shell.open();
}
public void DestroyGUI()
{
main_display.dispose();
}
public void UpdateProgressBar( final int selection_value )
{
if( pBar_status.isDisposed() ) return;
main_display.asyncExec
(
new Runnable()
{
public void run() { pBar_status.setSelection( selection_value ); }
}
);
}
public ShellGetShell(){ return main_shell;}
public Display GetDisplay() { return main_display; }
public static void main( String[] args )
{
SWTGuiExample gui = new SWTGuiExample();
gui.BuildGUI();
new Thread( new GUIUpdater(gui) ).start();
while( !gui.GetShell().isDisposed() )
{
if( !gui.GetDisplay().readAndDispatch() )
{
gui.GetDisplay().sleep();
}
}
gui.DestroyGUI();
}
}
// -
// GUIUpdater.java
// -
import java.lang.Thread;
public class GUIUpdater implements Runnable
{
private SWTGuiExample m_gui;
public GUIUpdater( SWTGuiExample gui )
{
m_gui = gui;
}
public void run()
{
int current_seleciton_value = 0;
while( current_seleciton_value <= 100 )
{
m_gui.UpdateProgressBar( current_seleciton_value );
current_seleciton_value += SWTGuiExample.PROGRESS_BY;
try
{
Thread.sleep(600); //0.6 sec
}
catch( Exception e )
{}
}
}
}
eksOra at 2007-7-15 15:24:07 >
