In: Computer Science
The program is written in Java 8 using Eclipse IDE and Scene Builder for UI design
Main
package test;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
// loading the
FXML File
Parent root =
FXMLLoader.load(getClass().getResource("FXML.fxml"));
Scene scene =
new Scene(root);
primaryStage.setScene(scene);
primaryStage.setResizable(false);// seting resizable false
primaryStage.setTitle("");// set title
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
Controller
package test;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
public class Controller {
@FXML
private BorderPane borderPane;//borderpane control
@FXML
private GridPane gridPane;//gridpane control
@FXML
private VBox vbox;//vbox control
@FXML
private Text text1;//text control
@FXML
private TextField textfield;//textfield control
@FXML
private Label label;//label control
@FXML
private Button button;//button control
@FXML//perform button operation
void buttonOperation(ActionEvent event) {
//set the textfiled text to label on button
click
label.setText(textfield.getText());
}
}
FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Text?>
<BorderPane fx:id="borderPane" maxHeight="-Infinity"
maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
prefHeight="400.0" prefWidth="600.0"
xmlns="http://javafx.com/javafx/8.0.171"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="test.Controller">
<center>
<GridPane fx:id="gridPane"
BorderPane.alignment="CENTER">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0"
prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0"
prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0"
vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0"
vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0"
vgrow="SOMETIMES" />
</rowConstraints>
<children>
<VBox fx:id="vbox" prefHeight="69.0" prefWidth="225.0">
<children>
<Text fx:id="text1" strokeType="OUTSIDE" strokeWidth="0.0"
text="Text" />
<TextField fx:id="textfield" promptText="TextField" />
<Label fx:id="label" prefHeight="24.0" prefWidth="300.0"
text="Label" />
<Button fx:id="button" mnemonicParsing="false"
onAction="#buttonOperation" prefHeight="33.0" prefWidth="70.0"
text="Click Me" />
</children>
</VBox>
</children>
</GridPane>
</center>
</BorderPane>
Output