add more swing helper methods for creating menus
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
package kotlin.swing
|
||||
|
||||
import java.awt.event.ActionEvent
|
||||
import javax.swing.AbstractAction
|
||||
import javax.swing.Action
|
||||
import javax.swing.Icon
|
||||
|
||||
/**
|
||||
* Helper method to create an action from a function
|
||||
*/
|
||||
fun action(text: String, icon: Icon? = null, fn: (ActionEvent) -> Unit): Action {
|
||||
return object: AbstractAction(text, icon) {
|
||||
public override fun actionPerformed(e: ActionEvent?) {
|
||||
if (e != null) {
|
||||
(fn)(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package kotlin.swing
|
||||
|
||||
import javax.swing.JComponent
|
||||
|
||||
var JComponent.description: String?
|
||||
get() {
|
||||
return getAccessibleContext()?.getAccessibleDescription()
|
||||
}
|
||||
set(value) {
|
||||
getAccessibleContext()?.setAccessibleDescription(value)
|
||||
}
|
||||
@@ -47,3 +47,10 @@ var JFrame.width: Int
|
||||
set(w) {
|
||||
setSize(height, w)
|
||||
}
|
||||
|
||||
var JFrame.jmenuBar: JMenuBar?
|
||||
get() = getJMenuBar()
|
||||
set(value) {
|
||||
setJMenuBar(value)
|
||||
println("Set the menu bar to $value")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
package kotlin.swing
|
||||
|
||||
import javax.swing.*
|
||||
|
||||
public fun JMenuBar.menu(text: String, init: JMenu.() -> Unit): JMenu {
|
||||
val answer = JMenu(text)
|
||||
answer.init()
|
||||
add(answer)
|
||||
return answer;
|
||||
}
|
||||
|
||||
public fun JPopupMenu.menu(text: String, init: JMenu.() -> Unit): JMenu {
|
||||
val answer = JMenu(text)
|
||||
answer.init()
|
||||
add(answer)
|
||||
return answer;
|
||||
}
|
||||
|
||||
var JMenu.mnemonic: Int
|
||||
get() {
|
||||
return getMnemonic()
|
||||
}
|
||||
set(value) {
|
||||
setMnemonic(value)
|
||||
}
|
||||
|
||||
var JMenu.accelerator: KeyStroke?
|
||||
get() {
|
||||
return getAccelerator()
|
||||
}
|
||||
set(value) {
|
||||
setAccelerator(value)
|
||||
}
|
||||
|
||||
fun JMenu.add(vararg actions: Action): Unit {
|
||||
for (action in actions) {
|
||||
val answer = JMenuItem(action)
|
||||
add(answer)
|
||||
}
|
||||
}
|
||||
|
||||
fun JMenu.item(text: String, description: String? = null, mnemonic: Char? = null, accelerator: KeyStroke? = null): JMenuItem {
|
||||
val answer = menuItem(text, description, mnemonic, accelerator)
|
||||
add(answer)
|
||||
return answer
|
||||
}
|
||||
|
||||
fun JMenu.checkBoxItem(text: String, description: String? = null, mnemonic: Char? = null, accelerator: KeyStroke? = null): JCheckBoxMenuItem {
|
||||
val answer = checkBoxMenuItem(text, description, mnemonic, accelerator)
|
||||
add(answer)
|
||||
return answer
|
||||
}
|
||||
|
||||
fun JMenu.radioButtonItem(text: String, description: String? = null, mnemonic: Char? = null, accelerator: KeyStroke? = null): JRadioButtonMenuItem {
|
||||
val answer = radioButtonMenuItem(text, description, mnemonic, accelerator)
|
||||
add(answer)
|
||||
return answer
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to create a new [JMenuItem]
|
||||
*/
|
||||
public fun menuItem(text: String, description: String? = null, mnemonic: Char? = null, accelerator: KeyStroke? = null): JMenuItem {
|
||||
return configureMenuItem(JMenuItem(text), description, mnemonic, accelerator)
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to create a new [JCheckBoxMenuItem]
|
||||
*/
|
||||
public fun checkBoxMenuItem(text: String, description: String? = null, mnemonic: Char? = null, accelerator: KeyStroke? = null): JCheckBoxMenuItem {
|
||||
return configureMenuItem(JCheckBoxMenuItem(text), description, mnemonic, accelerator)
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to create a new [JRadioButtonMenuItem]
|
||||
*/
|
||||
public fun radioButtonMenuItem(text: String, description: String? = null, mnemonic: Char? = null, accelerator: KeyStroke? = null): JRadioButtonMenuItem {
|
||||
return configureMenuItem(JRadioButtonMenuItem(text), description, mnemonic, accelerator)
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to create a new [JMenuItem]
|
||||
*/
|
||||
fun <T: JMenuItem> configureMenuItem(answer: T, description: String?, mnemonic: Char?, accelerator: KeyStroke?): T {
|
||||
if (description != null) {
|
||||
answer.getAccessibleContext()?.setAccessibleDescription(description)
|
||||
}
|
||||
if (mnemonic != null) {
|
||||
answer.setMnemonic(mnemonic)
|
||||
}
|
||||
if (accelerator != null) {
|
||||
answer.setAccelerator(accelerator)
|
||||
}
|
||||
return answer
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to create a [KeyStroke]
|
||||
*/
|
||||
public fun keyStroke(keyChar: Char, modifiers: Int?): KeyStroke? {
|
||||
return if (modifiers != null) {
|
||||
KeyStroke.getKeyStroke(keyChar, modifiers)
|
||||
} else {
|
||||
KeyStroke.getKeyStroke(keyChar)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a [JMenuBar] from a list of [JMenu] objects
|
||||
*/
|
||||
public fun menuBar(init: JMenuBar.() -> Unit): JMenuBar {
|
||||
val answer = JMenuBar()
|
||||
answer.init()
|
||||
return answer
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a [JPopupMenu] from a list of [JMenu] objects
|
||||
*/
|
||||
public fun popupMenu(init: JPopupMenu.() -> Unit): JPopupMenu {
|
||||
val answer = JPopupMenu()
|
||||
answer.init()
|
||||
return answer
|
||||
}
|
||||
@@ -13,7 +13,23 @@ Enter some text here!
|
||||
|
||||
val f = frame("Kool Kotlin Swing Demo") {
|
||||
exitOnClose()
|
||||
val test = 12
|
||||
|
||||
val aboutAction = action("About") {
|
||||
println("Show about window")
|
||||
}
|
||||
val propertiesAction = action("Properties") {
|
||||
println("Show Properties window")
|
||||
}
|
||||
|
||||
jmenuBar = menuBar{
|
||||
menu("File") {
|
||||
add(aboutAction)
|
||||
}
|
||||
menu("Help") {
|
||||
add(propertiesAction)
|
||||
}
|
||||
}
|
||||
|
||||
size = Pair(500, 300)
|
||||
|
||||
val textArea = JTextArea(greeting)
|
||||
@@ -29,6 +45,7 @@ Enter some text here!
|
||||
println("Restored text!")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
f.setLocationRelativeTo(null)
|
||||
|
||||
Reference in New Issue
Block a user