Changing value in textfield with groovy and SwingBuilder

跟風遠走 提交于 2019-12-12 02:27:00

问题


I want to put the value of a variable (==i) in a textfield, so that its value is shown in the textfield, i.e., changing from 1 to 10.

def sb = new SwingBuilder()
def th = Thread.start {
    for(i in 1..10) {
        sleep 2000
    }
}
def Pan = sb.panel(layout: new BorderLayout()) {
    sb.panel(constraints: BorderLayout.NORTH){
        gridLayout(cols: 2, rows: 3)
        textField id:'tf', text: ?
    }
}

回答1:


You can do that with the doOutside method of SwingBuilder, which allows to run a closure outside the EDT. The code below does what you are trying to do (with a table layout instead of a grid layout).

import groovy.swing.SwingBuilder  
import static javax.swing.JFrame.EXIT_ON_CLOSE  
import java.awt.*

def swingBuilder = new SwingBuilder()

swingBuilder.edt {   
    def message

    def setMessage = { String s -> message.setText(s) }

    frame(title: 'Example', size: [200, 150], show: true, locationRelativeTo: null, defaultCloseOperation: EXIT_ON_CLOSE) { 
        borderLayout(vgap: 5)

        panel(constraints: BorderLayout.CENTER, border: emptyBorder(10)) {
            tableLayout(cellpadding: 5) {
                tr {
                    td {
                        label 'Value'  // text property is default, so it is implicit.
                    }
                    td {
                        message = textField(id: 'tf', columns: 5, text: '0')
                    }
                }
            }           
        }
    }

    doOutside {
        for (i in 1..10) {
            sleep 1000
            edt { setMessage(String.valueOf(i)) }
        }
    }
}


来源:https://stackoverflow.com/questions/27415601/changing-value-in-textfield-with-groovy-and-swingbuilder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!