“Well yes, but actually no”
I was playing with Intellij Plugin SDK using Kotlin UI DSL v2, technically this is not pure Java Swing. I created this tab to get visually when width is applied and when is just ignored.
What is Kotlin UI DSL v2?
Kotlin UI DSL (Domain-specific language) is the second iteration, at the time or writing in experimental status, designed to create forms using declarative syntax inside Intellij platform, part of the Intellij Plugin SDKWhen you read “extension” means an extension over library classes like:
fun Component.setPreferredWidth(width: Int){
this.preferredSize = Dimension(width, this.preferredSize.height)
}
What is a Kotlin extension?
Kotlin provides the ability to extend a class or an interface with new functionality without having to inherit from the class or use design patterns such as Decorator. This is done via special declarations called extensions.This extension function was created because as you can see in the example, if Dimension
is not a new object, the size
is not applied, also the function is dealing with the old height
or width
when you are just changing one of them.
I hope you don’t waste the time like I did. I’m far from being an expert on Java Swing but IMO this is not a correct behavior.
I leave the tab source code here:
fun getTab(): JPanel {
val testWidth = 150
return panel {
indent {
row("textField from rowConstructor: ") {
textField()
}
row("textField from rowConstructor with RowLayout.PARENT_GRID: ") {
textField()
}.layout(RowLayout.PARENT_GRID)
row("textField from JBTextField object ") {
val jbTextField = JBTextField()
cell(jbTextField)
}
row("textField from JBTextField object with cell.horizontalAlign.FILL") {
val jbTextField = JBTextField()
cell(jbTextField).horizontalAlign(HorizontalAlign.FILL)
}
row("textField from JBTextField set $testWidth to width using size.width") {
val jbTextField = JBTextField()
jbTextField.size.width = testWidth
cell(jbTextField)
}
row("textField from JBTextField set $testWidth to width using size.setWidth extension") {
val jbTextField = JBTextField()
jbTextField.setWidth(testWidth)
cell(jbTextField)
}
row("textField from JBTextField set $testWidth to width using preferredSize.width") {
val jbTextField = JBTextField()
jbTextField.preferredSize.width = testWidth
cell(jbTextField)
}
row("textField from JBTextField set $testWidth to width using size.setPreferredWidth extension") {
val jbTextField = JBTextField()
jbTextField.setPreferredWidth(testWidth)
cell(jbTextField)
}
row("textField from JBTextField set $testWidth to width using minimumSize.width") {
val jbTextField = JBTextField()
jbTextField.minimumSize.width = testWidth
cell(jbTextField)
}
row("textField from JBTextField set $testWidth to width using size.setMinimumWidth extension") {
val jbTextField = JBTextField()
jbTextField.setMinimumWidth(testWidth)
cell(jbTextField)
}
}
}
}
Thanks for reading!
Namaste.