UI components

In JavaFX, UI components are the building blocks that make up the graphical user interface of an application. These components, also known as nodes, can range from simple controls like buttons and text fields to more complex containers and graphics. Understanding and effectively using these UI components is crucial for creating interactive and visually appealing JavaFX applications. Here are some of the common UI components in JavaFX:

1. Basic Controls:

a. Button:

A button is a clickable control that triggers an action when pressed.

Button button = new Button("Click Me");

b. Label:

A label is used to display non-editable text.

Label label = new Label("Hello, JavaFX!");

c. TextField:

A text field allows users to input and edit text.

TextField textField = new TextField();

d. PasswordField:

Similar to a text field but specifically designed for password input.

PasswordField passwordField = new PasswordField();

e. CheckBox:

A checkbox is a control that represents a binary choice.

CheckBox checkBox = new CheckBox("Accept Terms");

f. RadioButton:

A radio button represents a choice in a group of mutually exclusive options.

RadioButton radioButton = new RadioButton("Option 1");

g. ToggleButton:

A toggle button can be in an “on” or “off” state.

ToggleButton toggleButton = new ToggleButton("Toggle");

h. ComboBox:

A combo box allows users to choose from a drop-down list of options.

ComboBox<String> comboBox = new ComboBox<>();
comboBox.getItems().addAll("Option 1", "Option 2", "Option 3");

2. Containers:

a. VBox:

A vertical box arranges its children in a single column.

VBox vBox = new VBox(button1, button2, button3);

b. HBox:

A horizontal box arranges its children in a single row.

HBox hBox = new HBox(button1, button2, button3);

c. BorderPane:

A border pane divides its area into five regions: top, bottom, left, right, and center.

BorderPane borderPane = new BorderPane();
borderPane.setTop(new Label("Top"));
borderPane.setLeft(new Button("Left"));
borderPane.setCenter(new TextArea());

d. GridPane:

A grid pane organizes its children in a grid format.

GridPane gridPane = new GridPane();
gridPane.add(new Button("Button 1"), 0, 0);
gridPane.add(new Button("Button 2"), 1, 0);
gridPane.add(new Button("Button 3"), 0, 1);

e. ScrollPane:

A scroll pane provides a scrollable view of its content.

ScrollPane scrollPane = new ScrollPane();
scrollPane.setContent(new TextArea());

3. Text and Graphics:

a. Text:

The text component displays styled text.

Text text = new Text("Hello, JavaFX!");

b. ImageView:

An image view displays an image.

Image image = new Image("path/to/image.png");
ImageView imageView = new ImageView(image);

c. Canvas:

A canvas provides a surface for drawing shapes and images.

Canvas canvas = new Canvas(200, 200);
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.fillRect(0, 0, 50, 50);

4. Dialogs:

a. Alert:

An alert displays important information to the user.

Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Information Dialog");
alert.setContentText("This is an information message.");

b. FileChooser:

A file chooser dialog allows users to choose a file.

FileChooser fileChooser = new FileChooser();
File selectedFile = fileChooser.showOpenDialog(stage);

These are just a few examples of the many UI components available in JavaFX. As you explore JavaFX further, you’ll discover additional controls and containers that cater to various application needs. Combining and customizing these components allows developers to create sophisticated and user-friendly interfaces for their JavaFX applications.