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

Written by Lyoneel on in programming, kotlin, user-interfaces
 3 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:

1
2
3
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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

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.