update lable from timertask [message #444307] |
Mon, 11 October 2004 19:37 |
Aaron Mondelblatt 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 |
Eclipse User |
|
|
|
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
|
|
|
|
Powered by
FUDForum. Page generated in 0.02689 seconds