bindText of textField in Kotlin UI DSL Version 2 not working
Answered
@ApiStatus.Internal
internal data class DomainGeneratorModel(
var sql: String,
var domainName: String,
var targetFolder: String
)
internal class DomainGeneratorDialogWrapper constructor(
project: Project
) : DialogWrapper(project, true) {
init {
title = "Domain Generator"
init()
}
private val project = project
private val model = DomainGeneratorModel("11111", "", "")
/**
* Factory method. It creates panel with dialog options. Options panel is located at the
* center of the dialog's content pane. The implementation can return `null`
* value. In this case there will be no options panel.
*/
override fun createCenterPanel() = panel {
row {
textArea().applyToComponent { columns = 56; rows = 10 }.align(AlignX.FILL)
.label("Query SQL Script:", LabelPosition.TOP)
.bindText({
println(model.sql)
model.sql
}, { model.sql = it })
}
row {
textField().label("Domain Name:", LabelPosition.TOP).align(AlignX.FILL)
.bindText(model::domainName)
}
row {
textFieldWithBrowseButton(
"Choose Folder:", project, FileChooserDescriptorFactory.createSingleFolderDescriptor()
).label("Target Folder:", LabelPosition.TOP).align(AlignX.FILL)
.bindText(model::targetFolder)
.comment("The folder where the domain file will be generated")
}
}
/**
* ok action handler
*/
override fun doOKAction() {
println("querySqlScript: ${model.sql}")
println("domainName: $model.domainName")
println("targetFolder: $model.targetFolder")
// super.doOKAction()
}
Please sign in to leave a comment.
Please be more specific. What does it mean that it's not working? What are the expected and actual behaviors?
Please also use code snippets and format the code.
Karol Lewandowski
I refer to the code in DomoBinding.kt to make Reactivity state. But it always prompts that the Model.getSql() method does not exist. thanks for your answerI refer to the code in DomoBinding.kt to make Reactivity state. But it always prompts that the Model.getSql() method does not exist. thanks for your answer.
Karol Lewandowski Hey bro I'm waiting for you!!!
2023-04-20 15:44:29,984 [ 160290] SEVERE - #c.i.i.p.PluginManager - Cannot invoke "dev.zhaox.core.ui.DomainGeneratorModel.getSql()" because the return value of "dev.zhaox.core.ui.DomainGeneratorDialogWrapper.access$getModel$p(dev.zhaox.core.ui.DomainGeneratorDialogWrapper)" is null
java.lang.NullPointerException: Cannot invoke "dev.zhaox.core.ui.DomainGeneratorModel.getSql()" because the return value of "dev.zhaox.core.ui.DomainGeneratorDialogWrapper.access$getModel$p(dev.zhaox.core.ui.DomainGeneratorDialogWrapper)" is null
It looks like you need to execute DialogPanel.apply() method inside doOkAction.
Because according to the documentation:
A Vorobev Thanks very much, I gave it a try. It seems there is still a problem
2023-04-20 16:55:37,506 [ 13560] SEVERE - #c.i.i.p.PluginManager - Cannot invoke "dev.zhaox.core.ui.DomainGeneratorModel.getSql()" because the return value of "dev.zhaox.core.ui.DomainGeneratorDialogWrapper.access$getModel$p(dev.zhaox.core.ui.DomainGeneratorDialogWrapper)" is null
java.lang.NullPointerException: Cannot invoke "dev.zhaox.core.ui.DomainGeneratorModel.getSql()" because the return value of "dev.zhaox.core.ui.DomainGeneratorDialogWrapper.access$getModel$p(dev.zhaox.core.ui.DomainGeneratorDialogWrapper)" is null
I reproduced almost the same logic as yours and after entering text into the fields and executing "mainPanel.apply()" I see the expected values in my model.
Try to debug and pay attention to the place where you get your error.
Try to replace init block under all your class properties, i.e. under the private lateinit var panel: DialogPanel
Please do not ping us after 15 minutes of waiting. We have a lot of questions to handle, and you must wait in the queue if no one from the community handles it. If you want your question to be handled faster, spend more time providing precise information and details. I'm still unsure what the issue is. The errors you pasted are unclear. I don't know what lines throw the exceptions, etc.
Form fields like textField() are bound when the dialog is applied, and it is applied when the OK button is clicked. You don't need to override `doOKAction()`. Probably, your overridden method overrides the fields binding, so calling super.doOKAction() before you print the values may be the solution.
If you expect field values to be bound while typing, then it doesn't work this way.
If it doesn't help, then share the minimal reproducible example and describe the steps to reproduce the issue (the exact actions I have to do to reproduce it).
Thank you everyone, I hope the documentation will be more and more perfect.