JavaFX UI Controls
We can make an advanced graphical user interface (GUI) for the JavaFX application using UI Controls and layouts. Various UI elements can be used b the programmer in their application to make the user interaction easier with the operating system. Proper use of UI elements and layout leads to proper information exchange. The UI element should be properly aligned for better understanding, this can be done using layouts in the JavaFX application. When an event occurs on the UI element, the behavior must be defined for handling it.
In JavaFX applications, javafx.scene.control package provides all the UI components. Various UI controls like checkbox, label, button are used in the JavaFX application. Every UI element has its own defined meaning and particular styling.
UI Components in JavaFX applications:
- Button: JavaFX UI component, the button is used to create a labeled button for better interaction with the user. The function of the application is controlled by using the button.
- Label: We can display the simple text on the screen using the label in the JavaFX application.
- CheckBox: When the user needs to choose among various choices such as languages known, the technology learned checkbox is used. It is mainly used to get a choice from the user.
- TextField: To use TextField in JavaFX application javafx.scene.control.TextField is used. When we want to input some text from the user in the JavaFX application form, TextField is used.
- RadioButton: When we want the user to select only one option among all the options available, RadioButton is used. We can either select or deselect the radio button. Users can choose only one option in the group using the radio button.
- PasswordField: When the user has to enter the password in the JavaFX application form, the password field is used. The password typed by the user in the PasswordField is not visible on the screen but it is shown as black field circles.
- HyperLink: It can be used in the JavaFX applications by using the class javafx.scene.control.HyperLink. Any webpage can be referred to in our JavaFX application using HyperLink.
- Slider: When the user wants to move around a various range of values and wants to select anyone from them, the slider is used with pane to move as per the user requirement.
- ProgressBar: It can be used in JavaFX application using javafx.scene.control.ProgressBar class. When we want to show the progress of a particular task to a user ProgressBar is used.
- ScrollBar: When there is a requirement to scroll among application pages scroll bar is used.
- Menu: Menus are generally used in JavaFX applications to provide various options in the application.
Layouts in JavaFX applications:
Layouts in the JavaFX application are used as a container for various UI controls. Layouts are considered as parent container. The layout is used to define how components are arranged. All the classes for setting the layout are present in the java.scene.layout package.
- BorderPane: The BorderPane in the JavaFX application is used to place the nodes in the left, right, top, bottom, and center of the screen.
- FlowPane: It is used to arrange the notes according to the horizontal spaces available. If the horizontal space is less than node width. It goes to the next line.
- GridPane: In the JavaFX application, GridPane is used to arrange the components in the form of rows and columns.
- StackPane: In the JavaFX application, nodes in the stack pane are arranged in the form of a stack.
- HBox: It is used to arrange nodes in one row.
- VBox: It is used to arrange nodes in one column.
- Pane: All the layout classes have the base class pane in the JavaFX application.
Setting the layout in JavaFX application:
1. We have to first instantiate a particular layout class.
StackPane pane = new StackPane();
2. We can set the properties for the layout
pane.setSpacing(16);
3. Then we can add nodes to this layout
pane.getChildren().addAll( <Node Objects> );