textField variable not updating

Answered

I'm trying to implement a tool window with a couple of text fields, but the variables bound to the fields are not updating after I type something.

 

class RegexTesterToolWindowFactory : ToolWindowFactory {
override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) {
val regexTesterToolWindow = RegexTesterToolWindow(toolWindow)
val contentFactory = ContentFactory.SERVICE.getInstance()
val content = contentFactory.createContent(regexTesterToolWindow.content, "", false)
toolWindow.contentManager.addContent(content)
}
}

 

private val LOG = logger<RegexTesterToolWindow>()

class RegexTesterToolWindow(toolWindow: ToolWindow) {
private var regex: String = ".*"
private var testText: String = ""
var content: JPanel

init {
content = panel {
row("Regex:") {
textField(::regex)
}
row {
button("Run") {
val pattern = java.util.regex.Pattern.compile(regex)
val matcher = pattern.matcher(testText)
LOG.info(regex)
LOG.info(testText)
}
}
}
}

}

regex is always ".*" no matter what I type in the text field. What am I missing?

3 comments
Comment actions Permalink

Hi Rodrigo,

Kotlin UI DSL is intended to create forms and the binding happens on the form apply action. See:

https://plugins.jetbrains.com/docs/intellij/kotlin-ui-dsl.html#integrating-panels-with-property-bindings

You may want to use graph property which should update property on text field change:

private val regexProperty = PropertyGraph().graphProperty(::regex)
...
textField(regexProperty)

 

1
Comment actions Permalink

Hi Karol. That worked, thanks! Is this PropertyGraph documented somewhere? I couldn't find anything.

On the other hand, I read in the documentation that Kotlin UI DSL is the recommended way to build UIs (https://plugins.jetbrains.com/docs/intellij/user-interface-components.html), it isn't? what should I be using when I'm not building a form, but rather displaying information? 

0
Comment actions Permalink

Hi Rodrigo,

The property graph is mentioned in: https://plugins.jetbrains.com/docs/intellij/kotlin-ui-dsl-version-2.html#cellgraphproperty

This is about Kotlin UI DSL 2, but most of the features apply to both versions.

Regarding the UI DSL intention, we will clarify it soon.

EDIT:
We've updated the docs, so it should be clear now that Kotlin UI DSL is intended to create UI forms with input elements bound to some state objects.

0

Please sign in to leave a comment.