#2705 Fantom and JavaFX - Is it possible?

Ilove:= Thu 21 Jun 2018

I'm a hobbyist. Main job is a farmer. I learn programming in my free time for fun. I love this language. It enable me to get along so far that with other language like Java/C++ I can't, although my code skill sucks. (I didn't know to write fandoc, pardon me, I just want me code to be run, that's fine)

Recently I want to try to make some GUI. The built-in swt based could be used to write full featured apps, I know but I like Swing/JavaFX more because of the intensive of their documents and tutorials. Their is a swing.fan in examples folder of fantom-1.0.71 that I modified and so far it served me well despite it's very slow to startup when I use fan swing.fan on a Command Prompt. When running inside F4 it's much faster, I didn't know which magic F4 was doing but I don't care much. In attempt to do the same thing with JavaFX I failed, it's always: "Invalid args <ctor>([java]javafx.scene.shape::Line)". I've check the docs on Oracle site many times, I've written it alright. Please help.

Code:

using [java] javafx.application::Application as JFXApp using [java] javafx.scene::Group as JFXGroup using [java] javafx.scene::Scene as JFXScene using [java] javafx.scene.shape::Line as JFXLine using [java] javafx.stage::Stage as JFXStage using concurrent

class JFXTest : JFXApp {

override Void start(JFXStage? stage) {

l := JFXLine(100.0f, 150.0f, 500.0f, 150.0f) // Append the prefix JFX to avoid collision with default fantom classes
root := JFXGroup(l)
//root := JFXGroup()
//root.getChildren.add(l) it seemed fantom can't reach add method, it's only deep to getChildren
scene := JFXScene(root, 600, 300)
stage.setTitle("Sample application")
stage.setScene(scene)
stage.show()

}

static Void main(Str[] args) {

launch(args)
 Actor.sleep(Duration.maxVal) // Fantom launcher exits if this thread exits

} }

SlimerDude Thu 21 Jun 2018

Cool, I'd never heard of JavaFX until today! I had a lot of trouble downloading the jfxrt.jar as it doesn't seem to be part of my OpenJDK JRE installs.

The javadoc for javafx.scene::Group show that it takes an array of Line objects (because Varargs in Java just syntactic sugar for passing arrays) - so following the JavaFFI we can just pass in a list of Lines.

I also noted that the Scene ctor takes Java doubles (Fantom floats) not Ints.

Also, you'll need to tell Application.launch() about your Fantom JFXTest class.

In all, this creates a window with a line:

using [java] javafx.application::Application as JFXApp 
using [java] javafx.scene::Group as JFXGroup 
using [java] javafx.scene::Scene as JFXScene 
using [java] javafx.scene.shape::Line as JFXLine 
using [java] javafx.stage::Stage as JFXStage 

class JFXTest : JFXApp {
    override Void start(JFXStage? stage) {
        line  := JFXLine(100.0f, 150.0f, 500.0f, 150.0f)
        root  := JFXGroup( [line] )          // note the list of [line]
        scene := JFXScene(root, 600f, 300f)  // note the 'f' suffix
        stage.setTitle("Sample application")
        stage.setScene(scene)
        stage.show()    
    }

    static Void main(Str[] args) {
        launch(JFXTest#->toClass, args)
    }
}

I hope this helps!

JavaFX example screenshot

Ilove:= Fri 22 Jun 2018

Thanks. It worked well.

p/s: JFXTest->Class, not Jex :)

Login or Signup to reply.