我正在开发基本上完成的Java应用程序:
我已经阅读了一些关于这个问题的线程(关于从另一个线程更新SWT),并且我试图在收到消息时创建一个由我的服务器调用的方法:
public static void doPaint(final Canvas canvas, final Draw draw){
if (canvas != null && draw != null){
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
canvas.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {
System.out.println("Printing line....");
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_WHITE));
e.gc.setForeground(e.display.getSystemColor(SWT.COLOR_BLACK));
e.gc.setLineWidth(3);
e.gc.drawLine((int)draw.getStartX(),(int)draw.getEndX(),(int)draw.getStartY(),(int)draw.getEndY());
}
});
}
});
}
}
修改
这是等待套接字消息并触发操作的循环:
while(isConnected()){
try{
System.out.println("Waiting for connection...");
Socket server = getServerSocket().accept();
BufferedReader in = new BufferedReader(new InputStreamReader(server.getInputStream(), Const.ENC_UTF8));
String inputLine;
while(((inputLine = in.readLine()) != null)){
processInput(inputLine, canvas);
}
}catch(IOException ioex){
System.out.println(ioex.getMessage());
ioex.printStackTrace();
}catch (Exception ex) {
System.out.println(ex.getMessage());
ex.printStackTrace();
}
}
我的processInput
方法是:
protected void processInput(String inputLine, Canvas canvas){
//get draw from input stream
Draw draw = DrawUtil.getDrawFromInput(inputLine);
//draw points on live-mode
BBCanvas.doPaint(canvas, draw);
//append draw node to XML file
DrawUtil.writeDrawToFile(draw);
}
...但是doPaint
方法中的runnable永远不会被执行。任何提示?
编辑2
这是创建主shell的代码:
public MainWindow(BBServer server) {
this.server = server;
Display display = new Display();
this.mainShell = createShell(display);
getMainShell().open();
while (!getMainShell().isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}