Lesson 10

Executing a code

How to execute program's code when someone press the button? In order to achieve it we reference to a definition of view's elements.

I. Annotations


Task 0

The code below contains one correct and one wrong usage of @Override annotation.

Think about it, then copy it to your IDE and check if you are right.


class Person {
    public void sayHello() {
        System.out.println("Hello");
    }
}
class Terminator extends Person {
    
    @Override
    public void willBeBack() {
        System.out.println("I will be back");
    }

    public void sayHello() {
        System.out.println("Hasta la vista, baby");
    }

}
class DarthVader extends Person {
    
    @Override
    public void sayHello() {
        System.out.println("I am your father.");
    }
}

II. Preparing .fxml file


Task 1

Copy the code (fxml+java) and execute the program. Answer the following questions:

  1. Where a view is bound with its controller? (fxml)
  2. Which value and of which attribute assigns a method to an action? (fxml)
  3. Which notation allows to define an action? (java)

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane
    prefHeight="400.0"
    prefWidth="600.0"
    xmlns="http://javafx.com/javafx/8.0.121"
    xmlns:fx="http://javafx.com/fxml/1"
    fx:controller="MainController">

    <Button
        layoutX="20.0"
        layoutY="20.0"
        text="Kliknij mnie"
        onAction="#onButtonClick" />

</AnchorPane>

import javafx.fxml.FXML;

public class MainController {
    
    @FXML
    private void onButtonClick() {
        System.out.println("Button was pressed");
    }
    
}

III. Impact on other components


Task 2

Create a Text element with its unique identifier (fx:id).


<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane
    prefHeight="400.0"
    prefWidth="600.0"
    xmlns="http://javafx.com/javafx/8.0.121"
    xmlns:fx="http://javafx.com/fxml/1"
    fx:controller="MainController">

    <Button
        layoutX="20.0"
        layoutY="20.0"
        text="Click me"
        onAction="#onButtonClick" />

    <Text
        fx:id="mainText"
        layoutX="40.0"
        layoutY="40.0"
        text="Hello" />

</AnchorPane>

A variable's name in Java code is related with identifier in .fxml file. Now we can modify a text after pressing a button.


import javafx.fxml.FXML;
import javafx.scene.text.Text;

public class MainController {

    @FXML
    private Text mainText;

    @FXML
    private void onButtonClick() {
        mainText.setText("World!");
    }
}
Task 3

Modify code, the program will swap "Hello" to "World". Next button's click should swap "World" to "Hello".

Task 4

Write an application which takes 2 numbers from a user and shows their sum.

Task 5

Write an application which puts 1000 buttons in ScrollPane.

Task 6

Modify a program so after pressing a button, a program changes Text CSS class to its text is once red, once blue. (Hint: element has a list of active css classes getStyleClass()).