当前位置: 动力学知识库 > 问答 > 编程问答 >

scala, swing : thread issue with the Event Dispatch Thread(actors)

问题描述:

I have a scala class inheriting from SimpleSwingApplication.This class defines a window (with def top = new MainFrame) and instanciates an actor. the actor's code is simple:

class Deselectionneur extends Actor {

def act() {

while (true) {

receive {

case a:sTable => {

Thread.sleep(3000)

a.peer.changeSelection(0,0,false,false)

a.peer.changeSelection(0,0,true,false)

}

}

}

}

}

and the main class uses also "substance", a API allowing gui customization(there's no more ugly swing controls with it!).

the actor is called when I leaves a given swing table with my mouse; then the actor is called & deselects all the rows of the table.

the actor behaves very well, but when I launch my program, each times the actor is called, I get this error message:

org.pushingpixels.substance.api.UiThreadingViolationException: State tracking must be done on Event Dispatch Thread

do you know how I can remove this error message?

网友答案:

You need to move the gui update onto the EDT

Something like (I haven't compiled this)

case a:sTable => {
  scala.swing.Swing.onEDT {
    Thread.sleep(3000)  // this will stop GUI updates
    a.peer.changeSelection(0,0,false,false)
    a.peer.changeSelection(0,0,true,false)
  }
}

Some background on EDT can be found here: http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html

分享给朋友:
您可能感兴趣的文章:
随机阅读: