All Downloads are FREE. Search and download functionalities are using the official Maven repository.

kotlin.swing.Actions.kt Maven / Gradle / Ivy

There is a newer version: 0.14.451
Show newest version
package kotlin.swing

import java.awt.event.ActionEvent
import javax.swing.AbstractAction
import javax.swing.Action
import javax.swing.Action.*
import javax.swing.Icon

/**
 * Helper method to create an action from a function
 */
fun action(text: String, description: String? = null, mnemonic: Int? = null, icon: Icon? = null, fn: (ActionEvent) -> Unit): Action {
    val answer = object: AbstractAction(text, icon) {
        public override fun actionPerformed(e: ActionEvent) {
            val event: ActionEvent = if (e != null) {
                e
            } else {
                // lets create a dummy event
                ActionEvent(this, ActionEvent.ACTION_PERFORMED, text)
            }
            (fn)(event)
        }
    }
    if (description != null) answer.putValue(SHORT_DESCRIPTION, description)
    if (mnemonic != null) answer.putValue(MNEMONIC_KEY, mnemonic)
    return answer
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy