Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » update lable from timertask
update lable from timertask [message #444307] Mon, 11 October 2004 19:37 Go to next message
Aaron Mondelblatt is currently offline Aaron MondelblattFriend
Messages: 14
Registered: July 2009
Junior Member
I am trying to update a label from a timertask that runs every
second...however its says
Invalid thread access
at org.eclipse.swt.SWT.error(SWT.java:2691)
at org.eclipse.swt.SWT.error(SWT.java:2616)
at org.eclipse.swt.SWT.error(SWT.java:2587)
at org.eclipse.swt.widgets.Widget.error(Widget.java:381)
at org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:284)
at org.eclipse.swt.widgets.Label.setText(Label.java:406)
at org.ui.updateUI.updateLabel(updateUI.java:82)
at org.ui.updateUI.run(updateUI.java:73)
at java.util.TimerThread.mainLoop(Unknown Source)
at java.util.TimerThread.run(Unknown Source)

So here is what I have set up.

I would would basicly like to have it count from 5:00 minutes until zero
Normally the user would not be able to close window however I changed it
until I get the count down working. If anybody can help I would very
greatful please see the code below.


File #1

public class Shutdown
{



public static void main(String[] args)
{
Display display = new Display();

final Shell shell = new Shell(display);
shell.setText("Lab Close Window");
shell.addListener(SWT.Close, new Listener()
{
public void handleEvent(Event event)
{

int style = SWT.APPLICATION_MODAL | SWT.OK;
MessageBox messageBox = new MessageBox(shell, style);
messageBox.setText("Information");
messageBox.setMessage("This Window Can't Be Closed");
event.doit = messageBox.open() == SWT.OK;
}
});

Timer timer = new Timer();

timer.schedule(new updateUI(shell), 0, 1000);

while (!shell.isDisposed())
{
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();

}




}

File #2

public class updateUI extends TimerTask
{

private Calendar cal = new GregorianCalendar();

private Calendar calDown = new GregorianCalendar();

private Label l1;

private String shutdownMessage;

private SashForm mainSashForm;

private Shell shell;

public updateUI(Shell shell)
{

mainSashForm = new SashForm(shell, SWT.BORDER);
mainSashForm.setSize(400,400);

cal.set(Calendar.MINUTE, 5);
cal.set(Calendar.SECOND, 0);
calDown.set(Calendar.SECOND, 1);

l1 = new Label(mainSashForm, SWT.BOLD);

l1.setText("The Computer Labs will shutting down! Please Save and
Print your work");
String shutdownMessage = l1.getText();

l1.setText(shutdownMessage + " \n 5:00 Minutes");
mainSashForm.setWeights(mainSashForm.getWeights());

shell.setSize(400, 200);
shell.pack();
shell.open();

}

public void run()
{

cal.set(Calendar.SECOND, (cal.get(Calendar.SECOND) - calDown
.get(Calendar.SECOND)));
updateLabel();

}

private void updateLabel()
{

String mins = Integer.toString(cal.get(Calendar.MINUTE));
String sec = Integer.toString(cal.get(Calendar.SECOND));
l1.setText(shutdownMessage + " \n" + mins + ":" + sec);
mainSashForm.setWeights(mainSashForm.getWeights());

}
}


Thank you,
whatsgoingon
Re: update lable from timertask [message #444309 is a reply to message #444307] Mon, 11 October 2004 20:26 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: richkulp.us.NO_SPAM.ibm.com

You need to put the update of the label off into a Display.asyncExec().
You aren't allowed to touch any SWT widget from any thread other than
the display (UI) thread.


--
Thanks,
Rich Kulp
Re: update lable from timertask [message #444376 is a reply to message #444309] Tue, 12 October 2004 14:13 Go to previous message
Veronika Irvine is currently offline Veronika IrvineFriend
Messages: 1272
Registered: July 2009
Senior Member
See:

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

"Rich Kulp" <richkulp@us.NO_SPAM.ibm.com> wrote in message
news:ckepvd$2db$2@eclipse.org...
> You need to put the update of the label off into a Display.asyncExec().
> You aren't allowed to touch any SWT widget from any thread other than the
> display (UI) thread.
>
>
> --
> Thanks,
> Rich Kulp
Previous Topic:Scroll two tables at the same time
Next Topic:Resize a composite at runtime?
Goto Forum:
  


Current Time: Fri Apr 26 10:29:02 GMT 2024

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

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

Back to the top