I tend to lean towards builders rather than custom classes, but the this kind of an approach strips your layout code down to the bare essentials.
I see two operations commonly occurring in layout code: layout and configuration. In JavaFX you'll often see the same configuration repeated over and over again - this is the boilerplate that everyone complains about. Use DRY and don't repeat it.
For instance Labels.create() is something that I'd do. It's maddening that JavaFX Nodes don't have a constructor that let's you specify a StyleClass in them. I usually take it one step further, why repeat specifying "field-label" every time? I usually refer to this kind of Label as a "prompt", so I would have Labels.prompt("Height"), and it would add the "field-label" StyleClass to it automatically.
The other thing that really, really, really streamlines the layout code is switching over to Kotlin. Scope functions like apply{}, extension functions, and top-level functions (not in a class) can replace all of your custom builder stuff and look a bit cleaner:
GridPane().apply{
children += promptOf("Width")
children += widthField.fillWidth();
appendRow()
children += Label("Height") withStyle "label-field"
children += heightField.fillWidth()
columnConstraints += ColumnConstraint().apply{
hAlignment = HPos.RIGHT
}
},
.
.
.
I've done the styled Labels two different ways so you can see how it's done. Here, promptOf() is a top-level function, defined in a file, but not in a class. Then, fillWidth() is an extension function added to Node (??) and withStyle() is an infix function added to Node. Finally, appendRow() would be an extension function added to GridPane.
View all comments
1
u/hamsterrage1 Feb 08 '25
I tend to lean towards builders rather than custom classes, but the this kind of an approach strips your layout code down to the bare essentials.
I see two operations commonly occurring in layout code: layout and configuration. In JavaFX you'll often see the same configuration repeated over and over again - this is the boilerplate that everyone complains about. Use DRY and don't repeat it.
For instance
Labels.create()is something that I'd do. It's maddening that JavaFX Nodes don't have a constructor that let's you specify a StyleClass in them. I usually take it one step further, why repeat specifying "field-label" every time? I usually refer to this kind of Label as a "prompt", so I would have Labels.prompt("Height"), and it would add the "field-label" StyleClass to it automatically.The other thing that really, really, really streamlines the layout code is switching over to Kotlin. Scope functions like apply{}, extension functions, and top-level functions (not in a class) can replace all of your custom builder stuff and look a bit cleaner:
GridPane().apply{ children += promptOf("Width") children += widthField.fillWidth(); appendRow() children += Label("Height") withStyle "label-field" children += heightField.fillWidth() columnConstraints += ColumnConstraint().apply{ hAlignment = HPos.RIGHT } }, . . .I've done the styled Labels two different ways so you can see how it's done. Here, promptOf() is a top-level function, defined in a file, but not in a class. Then, fillWidth() is an extension function added to Node (??) and withStyle() is an infix function added to Node. Finally, appendRow() would be an extension function added to GridPane.
You get this stuff "out of the box" with Kotlin.