How does JavaFX work?

The primary reason for learning JavaFX is to demonstrate the concepts of Object Oriented Programming that you learned at the beginning of the semester; primarily things like inheritance. So far, everything that you've learned has probably been procedural programming - that is that one thing happens, then another, then another, until the program hits the last line of code and the closes. Graphical User Interfaces (GUIs) are different because they introduce something known as "Event-Driven Programming".

Event-Driven Programming now says, run a main program that only ends when the window closes (like how your web browser can stay open until you click the big red X) and handle the user's actions; then, and only then, do some programming code. For example, let's say you want to have a program that acknowledges the user presses a button:

Pane pane = new Pane();
Button btnOK = new Button("OK");
pane.getChildren().add(btnOK);

This block of code creates a Button, which is just another Class the Java has prebuilt for you, like the Math class, or the Random class. Someone once upon a time got paid (hopefully) to say "Hey, wouldn't it be cool if programs had buttons?!" Well a Button by itself is nothing. It does nothing and exists no where. We have to place it on a canvas - like paint to a real life canvas (I can have paint colors, but I have to add them to the canvas to make art).

Pane acts as our painting canvas. Except just like a real canvas, there are different kinds of canvases (like for water colors, acrylics, etc). Pane is actually a generic class that a lot of other classes inherit from, like StackPane, GridPane, or HBox - each are a canvas, just a little different.

Now I want my program to do something if the Button gets clicked

btnOK.setOnAction(e -> { System.out.println("OK Button Clicked!" });

You'll learn about the lambda expression a little later (Chapter 15), but understand all it does is take all that stuff you've had to type to do things and shorten it.

Now, the code still isn't appearing... why?!? God Java sucks! Or, have we forgotten that computers are incredibly stupid machines that need to be handheld to do anything?

Scene scene = new Scene(pane, 250, 250);

A screen can be thought of as a container of the Panes/Buttons/Circles/Whatever! Think about your web browser - you can have multiple tabs that all have separate web pages on them. That's where scene's come into play. For the sake of your class, you probably won't get into multiple scenes on a Stage... jeez, another new word...

public class ButtonDemo extends Application {
    @Override
    public void start (Stage primaryStage) {
        Button btnOK - new Button("OK");
        btnOK.setOnAction(e -> { System.out.println("OK Button Clicked!" });
        Pane pane = new Pane();
        pane.getChildren().add(btnOK);
        Scene scene = new Scene(pane, 250, 250);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

Stage is just like a theatre stage. You can have a play with multiple scenes happen on a single stage or one massive one-man monologue play. Stage does the same thing, acting as the platform for everything to play out.

The big focus with JavaFX is to understand that there are tons of classes prebuilt by Java to make your life harder easier; but it all comes from the fact that everything in Java is an Object.

/r/javahelp Thread