Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » How I can actualize label in a thread?
How I can actualize label in a thread? [message #446295] Tue, 23 November 2004 09:54 Go to next message
David is currently offline DavidFriend
Messages: 30
Registered: July 2009
Member
Hi!

I'm using SWT and I've a problem.

I need to connect to a database.
The time that the user must wait for the query is very long.
Because this, I open a Dialog with a label (named labelPorcentaje).
This label might change during the time that the database is processing
the petition.

To do this, I created a thread that change the text.

The problem is that with this thread the dialog not open or (in my tests)
it produces a thread error.

I try lines as:

dgpl.getShell().pack();
dgpl.getShell().redraw();

but the problem persists.


The Dialog class code has this lines:

labelPorcentaje.setLayoutData(gridData);
labelPorcentaje.setText("Iniciando proceso generación PL...");

Hilo hilo = new Hilo();
hilo.run(labelPorcentaje);


And the Thread class (named Hilo) is here:


public class Hilo extends Thread{

public void run(Label dgpl){


for (int y=0;y<99;y++){
System.out.println("Hello " + y);

dgpl.setText("Number: "+y);


try {
sleep(100);
} catch (InterruptedException e) {
// TODO Bloque catch generado automáticamente
e.printStackTrace();
}
}
Re: How I can actualize label in a thread? [message #446297 is a reply to message #446295] Tue, 23 November 2004 10:36 Go to previous messageGo to next message
Horst Dehmer is currently offline Horst DehmerFriend
Messages: 18
Registered: July 2009
Junior Member
Hi David!

As far as I know, calls to widgets must be made from within the swt ui
thread.
This might fix your problem:

Display.getDefault().syncExec(new Runnable() {
public void run() {
label.setText("some text...");
}
});

Hope this helps,
Horst

"David D
It works but... [message #446298 is a reply to message #446297] Tue, 23 November 2004 11:33 Go to previous messageGo to next message
David is currently offline DavidFriend
Messages: 30
Registered: July 2009
Member
Hi!

Many thanks for your answer.

Your code works fine but now I've other problem when
I iterate in the run method (the label is named messageLabel).
I wrote this lines:

boolean continuar = true;

public void run() {

boolean flag = false;
while(continuar){
messageLabel.setText("" + (flag?"/":"\""));
System.out.println(""+flag);
flag =!flag;

try{
Thread.sleep(100);
}catch(Exception e){};
}
}



During iterate I see the actualized flag variable value in the console but
the
dialog not actualices (Really it doesn't visible ¿?¿?).

If I eliminate the iteration the dialog se OK and actualices the text
of the label.

can you help me?

Many thanks again!!


Horst Dehmer wrote:

> Hi David!

> As far as I know, calls to widgets must be made from within the swt ui
> thread.
> This might fix your problem:

> Display.getDefault().syncExec(new Runnable() {
> public void run() {
> label.setText("some text...");
> }
> });

> Hope this helps,
> Horst

> "David Díaz" <davidd@mapfre.com> schrieb im Newsbeitrag
> news:cnv1c1$3q7$1@www.eclipse.org...
>> Hi!
>>
>> I'm using SWT and I've a problem.
>>
>> I need to connect to a database.
>> The time that the user must wait for the query is very long.
>> Because this, I open a Dialog with a label (named labelPorcentaje).
>> This label might change during the time that the database is processing
>> the petition.
>>
>> To do this, I created a thread that change the text.
>>
>> The problem is that with this thread the dialog not open or (in my tests)
>> it produces a thread error.
>>
>> I try lines as:
>>
>> dgpl.getShell().pack();
>> dgpl.getShell().redraw();
>>
>> but the problem persists.
>>
>>
>> The Dialog class code has this lines:
>>
>> labelPorcentaje.setLayoutData(gridData);
>> labelPorcentaje.setText("Iniciando proceso generación PL...");
>>
>> Hilo hilo = new Hilo();
>> hilo.run(labelPorcentaje);
>>
>>
>> And the Thread class (named Hilo) is here:
>>
>>
>> public class Hilo extends Thread{
>>
>> public void run(Label dgpl){
>>
>>
>> for (int y=0;y<99;y++){
>> System.out.println("Hello " + y);
>>
>> dgpl.setText("Number: "+y);
>>
>>
>> try {
>> sleep(100);
>> } catch (InterruptedException e) {
>> // TODO Bloque catch generado automáticamente
>> e.printStackTrace();
>> }
>> }
>>
Re: It works but... [message #446301 is a reply to message #446298] Tue, 23 November 2004 12:38 Go to previous messageGo to next message
Horst Dehmer is currently offline Horst DehmerFriend
Messages: 18
Registered: July 2009
Junior Member
Hi,

if run() is the sync'd run() the problem might be the update of flag in this
run() method. The following
works for me. Check out the separation of label update and flag/step
calculation in the BusyTimer
class. Don't let populateShellHook() bother you. Just copy/use the
BusyTimer.

package org.eclipse.swt.samples;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class BusyThreadSample extends BasicSample {
private Label label;

protected void populateShellHook(Shell shell) {
shell.setLayout(new FillLayout());
Composite pane = new Composite(shell, SWT.NONE);
label = new Label(pane, SWT.NONE);
label.setSize(100, 20);
label.setText("busy...");
new BusyTimer().start();
}

class BusyTimer {
Thread worker;
boolean interrupted = false;
int step = 0;
String[] gimmicks = new String[] {"-", "/", "|", "\\"};

private void updateText() {
step = (step + 1) % 4;
Display.getDefault().syncExec(new Runnable() {
public void run() {
if(!label.isDisposed()) {
label.setText("busy " + gimmicks[step]);
}
}
});
}

public void start() {
worker = new Thread() {
public void run() {
while(!interrupted) {
updateText();
try {
Thread.sleep(250);
}
catch(InterruptedException dontCare) {
interrupted = true;
}
}
}
};

interrupted = false;
worker.setDaemon(true);
worker.start();
}

public void stop() {
interrupted = true;
worker.interrupt();
worker = null;
}
}

public static void main(String[] args) {
new BusyThreadSample();
}
}



"David D
wonderful!!!!!!!!!!!!! [message #446302 is a reply to message #446301] Tue, 23 November 2004 13:06 Go to previous messageGo to next message
David is currently offline DavidFriend
Messages: 30
Registered: July 2009
Member
Many many thanks!!!!!

This class example works perfectly.

¡¡Many thanks again!!

Now I've the base class to actualize all my graphics components.


Thank you very much.







Horst Dehmer wrote:

> Hi,

> if run() is the sync'd run() the problem might be the update of flag in this
> run() method. The following
> works for me. Check out the separation of label update and flag/step
> calculation in the BusyTimer
> class. Don't let populateShellHook() bother you. Just copy/use the
> BusyTimer.

> package org.eclipse.swt.samples;

> import org.eclipse.swt.SWT;
> import org.eclipse.swt.layout.FillLayout;
> import org.eclipse.swt.widgets.Composite;
> import org.eclipse.swt.widgets.Display;
> import org.eclipse.swt.widgets.Label;
> import org.eclipse.swt.widgets.Shell;

> public class BusyThreadSample extends BasicSample {
> private Label label;

> protected void populateShellHook(Shell shell) {
> shell.setLayout(new FillLayout());
> Composite pane = new Composite(shell, SWT.NONE);
> label = new Label(pane, SWT.NONE);
> label.setSize(100, 20);
> label.setText("busy...");
> new BusyTimer().start();
> }

> class BusyTimer {
> Thread worker;
> boolean interrupted = false;
> int step = 0;
> String[] gimmicks = new String[] {"-", "/", "|", "\"};

> private void updateText() {
> step = (step + 1) % 4;
> Display.getDefault().syncExec(new Runnable() {
> public void run() {
> if(!label.isDisposed()) {
> label.setText("busy " + gimmicks[step]);
> }
> }
> });
> }

> public void start() {
> worker = new Thread() {
> public void run() {
> while(!interrupted) {
> updateText();
> try {
> Thread.sleep(250);
> }
> catch(InterruptedException dontCare) {
> interrupted = true;
> }
> }
> }
> };

> interrupted = false;
> worker.setDaemon(true);
> worker.start();
> }

> public void stop() {
> interrupted = true;
> worker.interrupt();
> worker = null;
> }
> }

> public static void main(String[] args) {
> new BusyThreadSample();
> }
> }



> "David Díaz" <davidd@mapfre.com> schrieb im Newsbeitrag
> news:cnv75j$g3l$1@www.eclipse.org...
>> Hi!
>>
>> Many thanks for your answer.
>>
>> Your code works fine but now I've other problem when
>> I iterate in the run method (the label is named messageLabel).
>> I wrote this lines:
>>
>> boolean continuar = true;
>>
>> public void run() {
>>
>> boolean flag = false;
>> while(continuar){
>> messageLabel.setText("" + (flag?"/":"""));
>> System.out.println(""+flag);
>> flag =!flag;
>>
>> try{
>> Thread.sleep(100);
>> }catch(Exception e){};
>> }
>> }
>>
>>
>>
>> During iterate I see the actualized flag variable value in the console but
>> the
>> dialog not actualices (Really it doesn't visible ¿?¿?).
>>
>> If I eliminate the iteration the dialog se OK and actualices the text
>> of the label.
>>
>> can you help me?
>>
>> Many thanks again!!
>>
>>
>> Horst Dehmer wrote:
>>
>> > Hi David!
>>
>> > As far as I know, calls to widgets must be made from within the swt ui
>> > thread.
>> > This might fix your problem:
>>
>> > Display.getDefault().syncExec(new Runnable() {
>> > public void run() {
>> > label.setText("some text...");
>> > }
>> > });
>>
>> > Hope this helps,
>> > Horst
>>
>> > "David Díaz" <davidd@mapfre.com> schrieb im Newsbeitrag
>> > news:cnv1c1$3q7$1@www.eclipse.org...
>> >> Hi!
>> >>
>> >> I'm using SWT and I've a problem.
>> >>
>> >> I need to connect to a database.
>> >> The time that the user must wait for the query is very long.
>> >> Because this, I open a Dialog with a label (named labelPorcentaje).
>> >> This label might change during the time that the database is processing
>> >> the petition.
>> >>
>> >> To do this, I created a thread that change the text.
>> >>
>> >> The problem is that with this thread the dialog not open or (in my
> tests)
>> >> it produces a thread error.
>> >>
>> >> I try lines as:
>> >>
>> >> dgpl.getShell().pack();
>> >> dgpl.getShell().redraw();
>> >>
>> >> but the problem persists.
>> >>
>> >>
>> >> The Dialog class code has this lines:
>> >>
>> >> labelPorcentaje.setLayoutData(gridData);
>> >> labelPorcentaje.setText("Iniciando proceso generación PL...");
>> >>
>> >> Hilo hilo = new Hilo();
>> >> hilo.run(labelPorcentaje);
>> >>
>> >>
>> >> And the Thread class (named Hilo) is here:
>> >>
>> >>
>> >> public class Hilo extends Thread{
>> >>
>> >> public void run(Label dgpl){
>> >>
>> >>
>> >> for (int y=0;y<99;y++){
>> >> System.out.println("Hello " + y);
>> >>
>> >> dgpl.setText("Number: "+y);
>> >>
>> >>
>> >> try {
>> >> sleep(100);
>> >> } catch (InterruptedException e) {
>> >> // TODO Bloque catch generado automáticamente
>> >> e.printStackTrace();
>> >> }
>> >> }
>> >>
>>
>>
Re: wonderful!!!!!!!!!!!!! [message #446303 is a reply to message #446302] Tue, 23 November 2004 13:12 Go to previous messageGo to next message
Horst Dehmer is currently offline Horst DehmerFriend
Messages: 18
Registered: July 2009
Junior Member
de nada, Horst

"David D
Hi again. Another problem... [message #446388 is a reply to message #446303] Wed, 24 November 2004 10:45 Go to previous messageGo to next message
David is currently offline DavidFriend
Messages: 30
Registered: July 2009
Member
Hi again.

I'm writting some code using your code examples and I've other problem.


I'm writting a code for Eclipse.
In this code, I create two threads using your code.
One of then shows the characters '-', '\', '-' ...
The other thread has a database access. When it finalices the connection
changes a class variable that is accesible with a method.

In the plugin code, I use a while sentence to wait that the second thread
ends:


while (myThread.connectionOpen()){
// Do nothing
}

//destroy thread 2 (connection)
//destroy thread 1 (windows with characters)

// Continues with the code plugin

The problems is that this iteration causes that visual components not
actulices.

(if I eliminate this iteration and not destroy the threads they execute
concurrently ok).

Where I can stop the principal code to wait for the second thread?

Many thanks!




Horst Dehmer wrote:

> de nada, Horst

> "David Díaz" <davidd@mapfre.com> schrieb im Newsbeitrag
> news:cnvcle$rbk$1@www.eclipse.org...
>> Many many thanks!!!!!
>>
>> This class example works perfectly.
>>
>> ¡¡Many thanks again!!
>>
>> Now I've the base class to actualize all my graphics components.
>>
>>
>> Thank you very much.
>>
>>
>>
>>
>>
>>
>>
>> Horst Dehmer wrote:
>>
>> > Hi,
>>
>> > if run() is the sync'd run() the problem might be the update of flag in
> this
>> > run() method. The following
>> > works for me. Check out the separation of label update and flag/step
>> > calculation in the BusyTimer
>> > class. Don't let populateShellHook() bother you. Just copy/use the
>> > BusyTimer.
>>
>> > package org.eclipse.swt.samples;
>>
>> > import org.eclipse.swt.SWT;
>> > import org.eclipse.swt.layout.FillLayout;
>> > import org.eclipse.swt.widgets.Composite;
>> > import org.eclipse.swt.widgets.Display;
>> > import org.eclipse.swt.widgets.Label;
>> > import org.eclipse.swt.widgets.Shell;
>>
>> > public class BusyThreadSample extends BasicSample {
>> > private Label label;
>>
>> > protected void populateShellHook(Shell shell) {
>> > shell.setLayout(new FillLayout());
>> > Composite pane = new Composite(shell, SWT.NONE);
>> > label = new Label(pane, SWT.NONE);
>> > label.setSize(100, 20);
>> > label.setText("busy...");
>> > new BusyTimer().start();
>> > }
>>
>> > class BusyTimer {
>> > Thread worker;
>> > boolean interrupted = false;
>> > int step = 0;
>> > String[] gimmicks = new String[] {"-", "/", "|", ""};
>>
>> > private void updateText() {
>> > step = (step + 1) % 4;
>> > Display.getDefault().syncExec(new Runnable() {
>> > public void run() {
>> > if(!label.isDisposed()) {
>> > label.setText("busy " + gimmicks[step]);
>> > }
>> > }
>> > });
>> > }
>>
>> > public void start() {
>> > worker = new Thread() {
>> > public void run() {
>> > while(!interrupted) {
>> > updateText();
>> > try {
>> > Thread.sleep(250);
>> > }
>> > catch(InterruptedException dontCare) {
>> > interrupted = true;
>> > }
>> > }
>> > }
>> > };
>>
>> > interrupted = false;
>> > worker.setDaemon(true);
>> > worker.start();
>> > }
>>
>> > public void stop() {
>> > interrupted = true;
>> > worker.interrupt();
>> > worker = null;
>> > }
>> > }
>>
>> > public static void main(String[] args) {
>> > new BusyThreadSample();
>> > }
>> > }
>>
>>
>>
>> > "David Díaz" <davidd@mapfre.com> schrieb im Newsbeitrag
>> > news:cnv75j$g3l$1@www.eclipse.org...
>> >> Hi!
>> >>
>> >> Many thanks for your answer.
>> >>
>> >> Your code works fine but now I've other problem when
>> >> I iterate in the run method (the label is named messageLabel).
>> >> I wrote this lines:
>> >>
>> >> boolean continuar = true;
>> >>
>> >> public void run() {
>> >>
>> >> boolean flag = false;
>> >> while(continuar){
>> >> messageLabel.setText("" + (flag?"/":"""));
>> >> System.out.println(""+flag);
>> >> flag =!flag;
>> >>
>> >> try{
>> >> Thread.sleep(100);
>> >> }catch(Exception e){};
>> >> }
>> >> }
>> >>
>> >>
>> >>
>> >> During iterate I see the actualized flag variable value in the console
> but
>> >> the
>> >> dialog not actualices (Really it doesn't visible ¿?¿?).
>> >>
>> >> If I eliminate the iteration the dialog se OK and actualices the text
>> >> of the label.
>> >>
>> >> can you help me?
>> >>
>> >> Many thanks again!!
>> >>
>> >>
>> >> Horst Dehmer wrote:
>> >>
>> >> > Hi David!
>> >>
>> >> > As far as I know, calls to widgets must be made from within the swt
> ui
>> >> > thread.
>> >> > This might fix your problem:
>> >>
>> >> > Display.getDefault().syncExec(new Runnable() {
>> >> > public void run() {
>> >> > label.setText("some text...");
>> >> > }
>> >> > });
>> >>
>> >> > Hope this helps,
>> >> > Horst
>> >>
>> >> > "David Díaz" <davidd@mapfre.com> schrieb im Newsbeitrag
>> >> > news:cnv1c1$3q7$1@www.eclipse.org...
>> >> >> Hi!
>> >> >>
>> >> >> I'm using SWT and I've a problem.
>> >> >>
>> >> >> I need to connect to a database.
>> >> >> The time that the user must wait for the query is very long.
>> >> >> Because this, I open a Dialog with a label (named labelPorcentaje).
>> >> >> This label might change during the time that the database is
> processing
>> >> >> the petition.
>> >> >>
>> >> >> To do this, I created a thread that change the text.
>> >> >>
>> >> >> The problem is that with this thread the dialog not open or (in my
>> > tests)
>> >> >> it produces a thread error.
>> >> >>
>> >> >> I try lines as:
>> >> >>
>> >> >> dgpl.getShell().pack();
>> >> >> dgpl.getShell().redraw();
>> >> >>
>> >> >> but the problem persists.
>> >> >>
>> >> >>
>> >> >> The Dialog class code has this lines:
>> >> >>
>> >> >> labelPorcentaje.setLayoutData(gridData);
>> >> >> labelPorcentaje.setText("Iniciando proceso generación PL...");
>> >> >>
>> >> >> Hilo hilo = new Hilo();
>> >> >> hilo.run(labelPorcentaje);
>> >> >>
>> >> >>
>> >> >> And the Thread class (named Hilo) is here:
>> >> >>
>> >> >>
>> >> >> public class Hilo extends Thread{
>> >> >>
>> >> >> public void run(Label dgpl){
>> >> >>
>> >> >>
>> >> >> for (int y=0;y<99;y++){
>> >> >> System.out.println("Hello " + y);
>> >> >>
>> >> >> dgpl.setText("Number: "+y);
>> >> >>
>> >> >>
>> >> >> try {
>> >> >> sleep(100);
>> >> >> } catch (InterruptedException e) {
>> >> >> // TODO Bloque catch generado automáticamente
>> >> >> e.printStackTrace();
>> >> >> }
>> >> >> }
>> >> >>
>> >>
>> >>
>>
>>
Re: Hi again. Another problem... [message #446390 is a reply to message #446388] Wed, 24 November 2004 11:27 Go to previous messageGo to next message
Horst Dehmer is currently offline Horst DehmerFriend
Messages: 18
Registered: July 2009
Junior Member
Hi David!

As far as I understand your goal you want to
show a busy indicator while some other thread
is accessing the db, right?

Before I would code this myself, I'd check
the Eclipse API's busy indicator for long lasting
operations (don't know how it's actually called).
But there are may examples in the Eclipse code
itself. I have to look at this in a couple of days,
too.

If you have to do it yourself, another thing is, that
you should gain deeper insight in coding with threads,
especially synchronizing concurrent execution. Have
a look at Thread.join(), Thread.wait(), Thread.notify()
and Thread.notifyAll() and above all the
synchronized(monitor) {} construct.

Concerning your code, the while-loop

while (myThread.connectionOpen()){
// Do nothing
}

will block the thread which executes it.

When I come up with something new, I'll let you
know.

Cheers,
Horst
Re: Hi again. Another problem... [message #446392 is a reply to message #446390] Wed, 24 November 2004 12:08 Go to previous messageGo to next message
David is currently offline DavidFriend
Messages: 30
Registered: July 2009
Member
Hi!

I'm trying to discover the method to do it too.

Ah! and many thanks for yout cooperation.






Horst Dehmer wrote:

> Hi David!

> As far as I understand your goal you want to
> show a busy indicator while some other thread
> is accessing the db, right?

> Before I would code this myself, I'd check
> the Eclipse API's busy indicator for long lasting
> operations (don't know how it's actually called).
> But there are may examples in the Eclipse code
> itself. I have to look at this in a couple of days,
> too.

> If you have to do it yourself, another thing is, that
> you should gain deeper insight in coding with threads,
> especially synchronizing concurrent execution. Have
> a look at Thread.join(), Thread.wait(), Thread.notify()
> and Thread.notifyAll() and above all the
> synchronized(monitor) {} construct.

> Concerning your code, the while-loop

> while (myThread.connectionOpen()){
> // Do nothing
> }

> will block the thread which executes it.

> When I come up with something new, I'll let you
> know.

> Cheers,
> Horst
Re: Hi again. Another problem... [message #446397 is a reply to message #446390] Wed, 24 November 2004 15:05 Go to previous message
Veronika Irvine is currently offline Veronika IrvineFriend
Messages: 1272
Registered: July 2009
Senior Member
For an example of using the BusyIndicator for a long running task, see:

http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet130.java?rev=HEAD& amp;content-type=text/vnd.viewcvs-markup

"Horst Dehmer" <horst.dehmer@onlinehome.de> wrote in message
news:co1r20$al3$1@www.eclipse.org...
> Hi David!
>
> As far as I understand your goal you want to
> show a busy indicator while some other thread
> is accessing the db, right?
>
> Before I would code this myself, I'd check
> the Eclipse API's busy indicator for long lasting
> operations (don't know how it's actually called).
> But there are may examples in the Eclipse code
> itself. I have to look at this in a couple of days,
> too.
>
> If you have to do it yourself, another thing is, that
> you should gain deeper insight in coding with threads,
> especially synchronizing concurrent execution. Have
> a look at Thread.join(), Thread.wait(), Thread.notify()
> and Thread.notifyAll() and above all the
> synchronized(monitor) {} construct.
>
> Concerning your code, the while-loop
>
> while (myThread.connectionOpen()){
> // Do nothing
> }
>
> will block the thread which executes it.
>
> When I come up with something new, I'll let you
> know.
>
> Cheers,
> Horst
Previous Topic:Text wrap in Tooltip for multiple line text
Next Topic:Rotating text
Goto Forum:
  


Current Time: Thu Mar 28 18:12:13 GMT 2024

Powered by FUDForum. Page generated in 0.05857 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top