Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run SimpleSwingApplication from console main method?

I wrote my first console app in Scala, and I wrote my first Swing app in Scala -- in case of the latter, the entry point is top method in my object extending SimpleSwingApplication.

However I would like to still go through main method, and from there call top -- or perform other equivalent actions (like creating a window and "running" it).

How to do it?

Just in case if you are curious why, the GUI is optional, so I would like to parse the command line arguments and then decide to show (or not) the app window.

like image 644
greenoldman Avatar asked Oct 26 '11 18:10

greenoldman


3 Answers

If you have something like:

object MySimpleApp extends SimpleSwingApplication {
  // ...
}

You can just call MySimpleApp.main to start it from the console. A main method is added when you mix SimpleSwingApplication trait. Have a look at the scaladoc.

like image 93
paradigmatic Avatar answered Nov 04 '22 05:11

paradigmatic


Here is the most basic example:

import swing._

object MainApplication {
  def main(args: Array[String]) = {
    GUI.main(args)
 }

  object GUI extends SimpleSwingApplication {
    def top = new MainFrame {
      title = "Hello, World!"
    }
  }
}

Execute scala MainApplication.scala from the command line to start the Swing application.

like image 43
Garrett Hall Avatar answered Nov 04 '22 05:11

Garrett Hall


If you override the main method that you inherit from SimpleSwingApplication, you can do whatever you want:

object ApplicationWithOptionalGUI extends SimpleSwingApplication {

  override def main(args: Array[String]) =
    if (parseCommandLine(args))
      super.main(args) // Starts GUI
    else
      runWithoutGUI(args)

...

}
like image 2
QFH Avatar answered Nov 04 '22 06:11

QFH