Creating a Form in JavaFX
Creating a Form in JavaFX
primaryStage.setTitle("JavaFX
Welcome");
primaryStage.show();
}
Tip: After you add sample code into a NetBeans project,
press Ctrl (or Cmd) + Shift + I to import the required
packages. When there is a choice of import statements,
choose the one that starts with javafx.
Create a GridPane Layout
For the login form, use a GridPane layout because it
enables you to create a flexible grid of rows and columns
in which to lay out controls. You can place controls in any
cell in the grid, and you can make controls span cells as
needed.
The code to create the GridPane layout is in Example
4-2. Add the code before the line
primaryStage.show();
Example 4-2 GridPane with Gap and Padding Properties
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25, 25, 25,
25));
@Override
public void handle(ActionEvent e) {
actiontarget.setFill(Color.FIREBRICK);
actiontarget.setText("Sign in
button pressed");
}
});
The setOnAction() method is used to register an
event handler that sets the actiontarget object to
Sign in button pressed when the user presses
the button. The color of the actiontarget object is set
to firebrick red.
Run the Application
Right-click the Login project node in the Projects
window, choose Run, and then click the Sign in button.
Figure 4-4 shows the results. If you run into problems,
then take a look at the code in the Login.java file that
is included in the downloadable Login.zip file.
Figure 4-4 Final Login Form
Description of "Figure 4-4 Final Login Form"