Skip to content

Listeners

Thosea edited this page Jun 22, 2025 · 1 revision

Listeners

KSwing provides wrappers for most Swing listeners. Sometimes, you only wnat to implement one function of a listener. Normally, you'd have to extend the adapter for that class, which can be verbose:

addKeyListener(object: KeyAdapter() {
	override fun keyPressed(event: KeyEvent) {
		// do something crazy
	}
})

With KSwing, you can shorten this to

kKeyPressedListener { event ->
	// do something nuts
}

// or maybe
kFocusGainedListener { foreground = Color.WHITE }
kFocusLostListener { foreground = Color.GRAY }

Clone this wiki locally