[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.platform.swt] Timer and SWT?

I have a problem with the java.util.Timer and SWT.
I want to refresh my table content after a while but i'll
get an invalid thread access exception.
What can i do to refresh my table content?

This is an example for my code:

import java.util.TimerTask;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

/*
 * Created on Jul 23, 2003
 *
 * To change this generated comment go to 
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
/**
 * @author buhle
 *
 * To change this generated comment go to 
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class Timer
{
  private Table table = null;
  
  public Timer()
  {
    Display display = new Display();
    Shell shell = new Shell(display);
    GridLayout layout= new GridLayout();
    layout.marginWidth = 5;
    layout.marginHeight = 5;
    shell.setLayout(layout);
  
    table = new Table(shell, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
    table.setHeaderVisible(true);
    TableColumn titleColumn = new TableColumn(table, SWT.CENTER);
    titleColumn.setText("Title");
    TableColumn startColumn = new TableColumn(table, SWT.CENTER);
    startColumn.setText("Startdate");
    TableColumn locationColumn = new TableColumn(table, SWT.CENTER);
    locationColumn.setText("Location");
    
    TableItem item = new TableItem(table, SWT.NONE);
    String[] text = {"tester", "23/07/2003", "Home"};
    item.setText(text);
    
    java.util.Timer refreshTimer = new java.util.Timer();
    refreshTimer.schedule(new RefreshTask(), 30000);

    shell.pack();
    shell.open();

    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
      display.sleep();
    }
    
    shell.dispose();
    display.dispose();
    System.exit(0);
  }
  
  class RefreshTask extends TimerTask
  {
    public void run()
    {
      table.removeAll();
      TableItem item = new TableItem(table, SWT.NONE);
      String[] text = {"tester1", "12/12/2004", "Work"};
      item.setText(text);
    }
  }
  
  
  
  /**Startet die Anwendung.*/
  public static void main(String[] args)
  {
    Timer timer = new Timer();
  }
}