Builders can be defined as a set of extension functions taking lambda expressions with receivers as arguments. In this example, a menu of a JFrame
is being built:
import javax.swing.*
fun JFrame.menuBar(init: JMenuBar.() -> Unit) {
val menuBar = JMenuBar()
menuBar.init()
setJMenuBar(menuBar)
}
fun JMenuBar.menu(caption: String, init: JMenu.() -> Unit) {
val menu = JMenu(caption)
menu.init()
add(menu)
}
fun JMenu.menuItem(caption: String, init: JMenuItem.() -> Unit) {
val menuItem = JMenuItem(caption)
menuItem.init()
add(menuItem)
}
These functions can then be used to build a tree structure of objects in an easy way:
class MyFrame : JFrame() {
init {
menuBar {
menu("Menu1") {
menuItem("Item1") {
// Initialize MenuItem with some Action
}
menuItem("Item2") {}
}
menu("Menu2") {
menuItem("Item3") {}
menuItem("Item4") {}
}
}
}
}