java source:
class jview implements Runnable{
public void run(){
for(int i=1;i<10;i++){
try {
Thread.sleep(1000);
System.out.println(i); // XXX show in UI
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args)throws Exception{
String[] roots = new String[] {"C:/Users/groovy"};
GroovyScriptEngine gse = new GroovyScriptEngine(roots);
Binding binding = new Binding();
gse.run("gview.groovy", binding);
jview j = new jview();
Thread t = new Thread(j);
t.start();
}
}
groovy来源:
def swingBuilder = new SwingBuilder()
swingBuilder.edt {
frame(title: 'ex', size: [200, 150], show: true) {
borderLayout(vgap: 5)
panel(constraints: BorderLayout.CENTER, border: emptyBorder(10)) {
label "java variable" // XXX value of `i`
}
}
}
如何在Swing UI(来自Groovy)的Java线程中显示变量i
。
答案 0 :(得分:0)
首先,创建一个在run
方法中更新的JTextField:
static JTextField textField = new JTextField("Init...", 5);
public void run() {
for(int i = 1; i < 10; i++){
try {
Thread.sleep(1000);
textField.setText(String.valueOf(i)); // Update UI
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
将其传递给脚本绑定:
GroovyScriptEngine gse = new GroovyScriptEngine(roots);
Binding binding = new Binding();
binding.setVariable("variableTextField", textField);
gse.run("gview.groovy", binding);
然后,在groovy代码中,您可以使用widget在构建器中“放置”textField
:
swingBuilder.edt {
frame(title: 'ex', size: [200, 150], show: true, defaultCloseOperation: EXIT_ON_CLOSE) {
borderLayout(vgap: 5)
panel(constraints: BorderLayout.CENTER, border: emptyBorder(10)) {
tableLayout(cellpadding: 5) {
tr {
td {
label 'Value'
}
td {
widget(variableTextField)
}
}
}
}
}
}
基本上,你用Java做doOutside
和setMessage
在Changing value in textfield with groovy and SwingBuilder中做的事情。