public static void main(String[] args){
        Display display;
       display = new Display()
final Shell shell = new Shell(display);
shell.setSize(500, 500);
shell.setText("会计实训服务管理器");
shell.setLayout(new FillLayout());
/**加入其它组件**/ shell.open();
         writeLog();  //加入此功能会出错
while(!shell.isDisposed()){
if (!display.readAndDispatch())
     display.sleep();
}
display.dispose();
}
public static void writeLog(){

System.out.println("writeLog");

SimpleLayout layout = new SimpleLayout(); 

WriterAppender appender = null; 
 try {             
appender = new WriterAppender();
appender = new WriterAppender(layout, new LogWriter(logText));
} catch(Exception e) {
   System.out.println("new WriterAppender layout is error!");

      rootLogger.setLevel(Level.INFO);             
      rootLogger.addAppender(appender);
}运行时出错:
org.eclipse.swt.SWTException: Invalid thread access
at org.eclipse.swt.SWT.error(SWT.java:3374)
at org.eclipse.swt.SWT.error(SWT.java:3297)
at org.eclipse.swt.SWT.error(SWT.java:3268)

解决方案 »

  1.   

    很简单
    swt的ui线程不允许做别的事情
    因为在这个线程里有他自己的事件
    如果你需要做别的事情
    就要用display中的两个方法来生成非ui线程
    具体名字去查doc
      

  2.   

    Display.getDefault().asyncExec(new Runnable()
    {
    public void run()
    {
    writeLog();//write log
    }
    });
      

  3.   

    swt不允许非ui线程操作ui控件, 我估计是你的WriterAppender或者rootLogger有另外的线程,然后要去操作ui控件,所以为出错。解决的方法就是在非ui线程需要操作ui控件时,先切回来:
    Display.getDefault().asyncExec(new Runnable()
    {
    public void run()
    {
       handleUIControl();
    }
    });