Search code examples
javaintellij-ideajavafxfxml

Setup .fxml file programmatically


I am used to writing code in JavaFx like the one given below because that's how I learn a few years ago.

import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.stage.*;

public class test extends Application {
    int c=0;
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("Demo");
        Button b = new Button("Click me");
        Label l = new Label("Count=" + c);
        VBox v = new VBox(20);
        v.getChildren().addAll(b, l);
        b.setOnAction(event -> {
            c++;
            l.setText("Count=" +c);
        });
        primaryStage.setScene(new Scene(v, 300, 300));
        primaryStage.show();
    }
}

I have recently installed JetBrains IntelliJ IDEA Ultimate Edition 2018.1.6 and I am seeing that to open up the primaryStage window, I need to have an fxml file.

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.GridPane?>
<GridPane alignment="center" hgap="10" vgap="10" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Main">
</GridPane>

And I have to do,

primaryStage.setScene(new Scene(FXMLLoader.load(getClass().getResource("sample.fxml")), 300, 300));

If I run the first code snippet, nothing shows up. But if I replace VBox v with FXMLLoader.load(getClass().getResources("sample.fxml")), Just a window shows up without the Button and Label. Is there a way in which I can either ignore the usage of the fxml file? Or can I, using the standard way of coding(first code snippet) and let the compiler change that into fxml format for me?

That's because I want to have 100x100 labels in a grid layout, when each Label is one pixel. Hence, in code, it's easy to run nested for loops to initiatialize and address each Label using 2-d Array. But initializing 1000 labels inside the fxml is too lengthy, exhausting and illogical.

I never had to do such things in JetBrains IntelliJ IDEA Community Edition 2017.0.2. When I upgraded to Ultimate Edition 2018.1.6, I'm getting these errors.


Solution

  • You can definitely use IntelliJ with JavaFX without FXML.

    Q: Is there a way in which I can either ignore the usage of the fxml file?

    A: Considering your code snippet you refer on default setup you get from IntelliJ when you create new JavaFX project, yes their default setup uses .fxml but it isn't required to use it. You can delete that sample.fxml file and rewrite start as you want.

    Side note: Nothing prevents you to use both fxml and code approach in same project, in some cases using fxml is better approach while in some other situations it is better to use code.

    Q: Or can I, using the standard way of coding(first code snippet) and let the compiler change that into fxml format for me?

    A: There is no way to convert Java code to fxml, but as I already said it is up to you to decide which one to use, and you can even uses both of them in same project.