Java Swing - Change textField width, seems easy, right?

Written by Lyoneel on in programming, kotlin, user-interfaces
 2 mins

Java Swing - Change textField width, seems easy, right?

“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.

Image showing unpredictable behavior with different instantiations of a texfield and setters

When you read “extension” means an extension over library classes like:

fun Component.setPreferredWidth(width: Int){
    this.preferredSize = Dimension(width, this.preferredSize.height)
}

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.