I need to use Kotlin for a simple GUI and Tornadofx says they don’t support my version of JAVA. So I am just using Swing because it doesn’t need to look nice, just something better than console. I stumbled into an issue. I was doing fine converting the other things from JAVA to Kotlin, but then I came into an event. I had barely learned any JAVA, so I was clueless as how to use a button in Swing even in JAVA. How would I make an event for when the button is clicked in Kotlin?
Here is an example in Java and the same example in Kotlin:
Java:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class SwingJ {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
public SwingJ() {
prepareGUI();
}
public static void main(String[] args) {
SwingJ swingControlDemo = new SwingJ();
swingControlDemo.showEventDemo();
}
private void prepareGUI() {
mainFrame = new JFrame("Java SWING Examples");
mainFrame.setSize(400, 400);
mainFrame.setLayout(new GridLayout(3, 1));
headerLabel = new JLabel("", JLabel.CENTER);
statusLabel = new JLabel("", JLabel.CENTER);
statusLabel.setSize(350, 100);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent) {
System.exit(0);
}
});
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showEventDemo() {
headerLabel.setText("Control in action: Button");
JButton okButton = new JButton("OK");
JButton submitButton = new JButton("Submit");
JButton cancelButton = new JButton("Cancel");
okButton.setActionCommand("OK");
submitButton.setActionCommand("Submit");
cancelButton.setActionCommand("Cancel");
okButton.addActionListener(new ButtonClickListener());
submitButton.addActionListener(new ButtonClickListener());
cancelButton.addActionListener(new ButtonClickListener());
controlPanel.add(okButton);
controlPanel.add(submitButton);
controlPanel.add(cancelButton);
mainFrame.setVisible(true);
}
private class ButtonClickListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("OK")) {
statusLabel.setText("Ok Button clicked.");
} else if (command.equals("Submit")) {
statusLabel.setText("Submit Button clicked.");
} else {
statusLabel.setText("Cancel Button clicked.");
}
}
}
}
Kotlin:
import java.awt.FlowLayout
import java.awt.GridLayout
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import java.awt.event.WindowAdapter
import java.awt.event.WindowEvent
import javax.swing.JButton
import javax.swing.JFrame
import javax.swing.JLabel
import javax.swing.JPanel
class SwingK {
private lateinit var mainFrame: JFrame
private lateinit var headerLabel: JLabel
private lateinit var statusLabel: JLabel
private lateinit var controlPanel: JPanel
init {
prepareGUI()
}
private fun prepareGUI() {
headerLabel = JLabel("", JLabel.CENTER)
statusLabel = JLabel("", JLabel.CENTER).apply { setSize(350, 100) }
controlPanel = JPanel().apply { layout = FlowLayout() }
mainFrame = JFrame("Java SWING Examples").apply {
setSize(400, 400)
layout = GridLayout(3, 1)
addWindowListener(object : WindowAdapter() {
override fun windowClosing(windowEvent: WindowEvent?) {
System.exit(0)
}
})
add(headerLabel)
add(controlPanel)
add(statusLabel)
isVisible = true
}
}
internal fun showEventDemo() {
headerLabel.text = "Control in action: Button"
val okButton = JButton("OK").apply {
actionCommand = "OK"
addActionListener(ButtonClickListener())
}
val submitButton = JButton("Submit").apply {
actionCommand = "Submit"
addActionListener(ButtonClickListener())
}
val cancelButton = JButton("Cancel").apply {
actionCommand = "Cancel"
addActionListener(ButtonClickListener())
}
controlPanel.add(okButton)
controlPanel.add(submitButton)
controlPanel.add(cancelButton)
mainFrame.isVisible = true
}
private inner class ButtonClickListener : ActionListener {
override fun actionPerformed(e: ActionEvent) {
statusLabel.text = when (e.actionCommand) {
"OK" -> "Ok Button clicked."
"Submit" -> "Submit Button clicked."
else -> "Cancel Button clicked."
}
}
}
}
fun main(args: Array<String>) {
val swingControlDemo = SwingK()
swingControlDemo.showEventDemo()
}
Note: The Java example was extracted from TutorialsPoint, and the Kotlin example was translated by me from the Java one
1 Like
Ok, thank you. I have been wondering and thought of using the JAVA converter that Kotlin provides. But your answer helped me a lot more.