**FXML** # Declarative versus Imperative Syntax ## Imperative Syntax In English grammar, an **imperative sentence** is a sentence that gives instructions that expresses a command. With imperative programming you give step-by-step instructions to the compiler for what it should do. The GUI layout created in [EventHandlingFX](EventHandlingFX) uses an imperative syntax. For example, ~~~~ Java @Override public void start(Stage primaryStage) { Pane root = new StackPane(); label = new Label("Here is some text that can be manipulated with the button above."); Button clickMe = new Button("Click Me"); clickMe.setOnAction(this::handleClickMe); root.getChildren().addAll(label, clickMe); primaryStage.setTitle("Simple GUI"); primaryStage.setScene(new Scene(root, 400, 300)); primaryStage.show(); } ~~~~ Here we tell the compiler to: 1. Create a `StackPane` object 1. Create a `Label` object 1. Create a `Button` object 1. Attach the `handleClickMe()` method as a listener to the button 1. Add the label and button to the stack pane 1. Set the title for the window 1. Set the scene for the stage to a newly created `Scene` object with the stack pane in it 1. Show the stage The compiler is required to generate code that completes the tasks described in exactly this order. This is a powerful way to tell the computer what I want to have happen. However, the above instructions provide more specificity than I actually care about. For example, the order of the first three steps do not really matter. In fact, the order of most of these steps is arbitrary. With an imperative programming approach, I must specify **how** to accomplish the desired task(s). ## Declarative Syntax In English grammar, a **declarative sentence** is a sentence in the form of a statement. Declarative sentences describe what is true. Declarative sentences are the most common form of sentence since they communicate what currently is efficiently. Similarly, with declarative programming we write code that describes what we want but not necessarily how to get it. JavaFX provides a tool for declaratively describing GUI layouts. For example, the GUI layout above could be described using JavaFX's FXML as shown below: ~~~~ Java