Tobias Bredenberg Tobias Bredenberg - 1 year ago 63
Java Question

How to use my own class in main?

I got a school assignment that is driving me nuts. I can find the answer im looking for while searching the web, so I really would appriciate some advice on this one.

The assignment is to create a basic JavaFX Application that says "Hello World" but i should contain of the and the class that extends BorderPane. I have solved the assignment just using the helloMain but when I have tried to move some of the code (the root node and all it contains) into the the Scene does not seem to load when I run the application (just an empty window). So obviously I have done something wrong, but I cant figure out what.

in helloJava I got the following code (and Im pretty sure this is correct)

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

public class helloWorldMain extends Application {

public void start(Stage primaryStage) {
HelloWorld helloWorld = new HelloWorld();
Scene scene = new Scene(helloWorld, 300, 300);
primaryStage.setTitle("Hello World");

public static void main(String[] args) {



And in my i got this code

public class HelloWorld extends BorderPane {

public HeloWorld() {
final Text text = new Text(0, 130, "Hello World");
Pane txtPane = new Pane();
Pane txtPane2 = new Pane();

VBox root = new VBox();

root.getChildren().addAll(txtPane, txtPane2);



I think it is pretty safe to say that I dont really know what I am doing, but im trying to learn.

  1. What am I missing in the HelloWorld class? is it something in the constructor? or should I use some kind of fields? or is it something else?

  2. I have been searching for hours trying to solve this problem, but I dont find any search result that is related to what I am trying to do.. Where can I read about "this approach" (dont really know what it is called, and probably why I could not find anything) when you create your own class and trying to use it in main?

Lots of code love to the one who can help me understand this :)

Answer Source

In HelloWorld you add Nodes to a VBox that you don't add anywhere, so they are "hanging in the air".

Add this line to the end of constructor of HelloWorld ...


... and you are done.