How to run SimpleSwingApplication from the main console method?

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

However, I would like to continue the main method , and from there call top - or perform other equivalent actions (for example, create a window and β€œlaunch” it).

How to do it?

Just in case, if you're interested, the GUI is optional, so I would like to parse the command line arguments, and then decided to show (or not) the application window.

+5
source share
4 answers

If you have something like:

object MySimpleApp extends SimpleSwingApplication {
  // ...
}

You can simply call MySimpleApp.mainto launch it from the console. When adding a property SimpleSwingApplication, a method is added main. Take a look at scaladoc .

+4
source

Here is an example:

import swing._

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

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

Run scala MainApplication.scalafrom the command line to start the Swing application.

+4
source

, SimpleSwingApplication, , :

object ApplicationWithOptionalGUI extends SimpleSwingApplication {

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

...

}
+2

main.args SimpleSwingApplication. CLI, JFileChooser , .

, SimpleSwingApplication, , demoapp.class:

class demoSSA(args: Array[String]) extends SimpleSwingApplication {
    ....
    var filename: String = null
    if (args.length > 0) {
        filename = args(0)
    } else {
        // use JFileChooser to select filename to be processed 
        // https://stackoverflow.com/questions/23627894/scala-class-running-a-file-chooser
    }
    ....
}

object demo {
  def main(args: Array[String]): Unit = {
     val demo = new demoSSA(args)
     demo.startup(args)
  }
}

demo [args], CLI , JFileChooser .

main() SimpleSwingApplication? 'filename' SimpleSwingApplication.main 'filename' , (val args: Array [String] = null;), (val args: Array [String];). , SSA, .

(Edit) We found another way: if the entire demoSSA is placed in the overridden main () or startup (), then the top MainFrame must be defined externally as top = null and re-declared from startup () as this.top:

object demo extends SimpleSwingApplication {  
  var top: MainFrame = null
  override def startup(args: Array[String]) {
    ...  // can use args here
    this.top = new MainFrame {
      ... // can use args here also
    };
    val t = this.top
    if (t.size == new Dimension(0,0)) t.pack()
      t.visible = true
  }
}

But I think I prefer the first method, with a shared main object. It is indented at least one level less than the last method.

0
source

All Articles