I have a very simple scala swing app and I want to return a value to the command line app from where the Swing app was triggered. If I select value "b", I want the GUI to return value "b" to me as soon as the button is pressed. How do I make the app wait for the correct user input and how do I return the value to the calling app?
import swing._
import event.{ButtonClicked}
object GuiDemo extends App {
object GUI extends SimpleSwingApplication {
val button = new Button {
text = "Go!"
}
val comboBox = new ComboBox(List("a", "b", "c"))
def top = new MainFrame {
contents = new FlowPanel {
contents += comboBox
contents += button
}
}
listenTo(button)
reactions += {
case ButtonClicked(`button`) => {
val selection = comboBox.item
button.enabled_= (false)
}
}
}
println("Before starting the GUI")
GUI.main(args)
val myValue = GUI.comboBox.item
println("""Now I need to make a complicated transformation in scala
with the selected value: """ + myValue.map(_.toUpper) )
// how do I get the selected value from the GUI?
}
Thanks!
Edit: At the moment I am not packaging it into a jar. Just compiling it and then running it with scala...
I need to return the selected value from the "GUI" to the "GuiDemo" scala app to do some further processing in scala.
So question really is:
How to wait for the GUI part to finish and then return (hand over) the selected value to GuiDemo
.
Here's a basic JOptionPane example with no parent specified which results in a lingering JFrame that has to be closed:
based on the empty MainFrame from the "scala-swing-newbie" link I posed in the comment and your code. If no option is selected, the selection would be None.
I'm very new to Scala, so I don't know if it's possible or how to "cast" the MainFrame as the parent so the extra dialog isn't produced.
As soon as you access AWT/Swing, it will spin up the event dispatch thread and the application will keep running. So all you need to do to return to the terminal is quit the application, when the user has filled out the GUI from.
To "return a value to the command line app", that would be printing things into the console, I guess. So:
Note that there is no need to nest two objects (
GuiDemo
andGUI
), just use one:If you execute that through the interpreter, using
scala GuiDemo.scala
, you need to explicitly invoke the main method, by adding the following line at the end of the file:You may have gathered that the answer you will get is "don't do that". Instead, you can set up an
Reactor
to listen to the button and execute your code inside that.However, if you really want to return a value to your main method, you can do it using Futures and Promises in Scala 2.10:
Some imports:
In your main method: