Hi guys, so I'm starting with JavaFX using eclipse. I had a look at a few tutorials and the syntax seems quite different from other java projects I've done. Specifically, I'm a bit confused when it comes to add my code to the Eclipse project.
I've just started very soft, with a simple helloWorld, just to look at the syntax (this is the class I've created, all coming directly from eclipse ):

package myapp;
import javafx.application.Application;
import javafx.stage.Stage;
public class MyApplication extends Application {
    @Override
    public void start(Stage primaryStage) {     
    }
    public static void main(String[] args) {
        launch(args);
    }
}

Now, if I want to add just a statement, literally something as simple as like System.out.println("Hello world");, where is that supposed to go? Inside start presumably?
Also, there is another file in eclipse, called Main.java and that too has a main and a start methods?

Recommended Answers

All 4 Replies

Short answer:
javafx.application.Application contains all the code for initialising a JavaFX app.
the normal main method just calls Application's launch method to get JavaFX initialised. Once FX is ready it calls your start method, where you do whatever you want.
You can think of start being to JavaFX what main is to ordinary applications. You can put your "hello world" there if you want. More usually you wil use it to create some kind of GUI and most of yor code will be run as a result of the user clicking or typing or whatever.

Right, thanks for the explanation. The trouble is that I must have read about 5-6 tutorials and all of them are different...I've created a brand new project now - let's forget for a minute the code in my previous post - I haven't added absolute anything to it and when I run I get already what I'd refer to as an empty GUI, presumably that is the "Scene", all stored inside an application package and a Main.java file. This one alone generates the GUI, so I presume this is the absolute minimum you get when you create any project

package application;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;


public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            BorderPane root = new BorderPane();
            Scene scene = new Scene(root,400,400);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Now, if I want to add anything to this application, like a proper GUI with a button for the sake of agument, do I create another package and inside that package a xxx.java with essentially pretty much the same content as above but with the extra code needed for the button, or do I add all that directly inside Main.java?

You update that Main class start method to create the GUI you want (or to call another method that does that).

OK thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.