- Java code to display list of employees when button pressed in FXML controller gridpane
- Note: to open web links in a new window use: ctrl+click on link
- Code from Java Programming Masterclass Section 15-285 JavaFx background tasks in Java - see 👏 Inspiration below
- Progress bar shown below list with text detail of data added using simple for loop
- Employee Service now separated
- Progress bar and Progress label binding to
N/A
- Java v11
- JavaFX v11 - now a Java Plugin
- class Collections static methods that operate on or return collections.
- interface ObservableList - a list that allows you to track changes
- JavaFX FXML an XML format that enables you to compose JavaFX GUIs like web GUIs in HTML
- Open folder in an IDE such as IntelliJ. Run from
Main.java
Main.java
code to create a list observable then bind values to a FXML ListView
public class Controller {
private Task<ObservableList<String>> task;
@FXML
private ListView listView;
@FXML
private ProgressBar progressBar;
@FXML
private Label progressLabel;
public void initialize() {
task = new Task<ObservableList<String>>() {
@Override
protected ObservableList<String> call() throws Exception {
String[] names = {"Tim Buchalka",
"Bill Rogers",
"Jack Jill",
"Joan Andrews",
"Mary Johnson",
"Bob McDonald"};
ObservableList<String> employees = FXCollections.observableArrayList();
for(int i=0; i<6; i++) {
employees.add(names[i]);
updateMessage(names[i] + " added to the list");
updateProgress(i + 1, 6);
Thread.sleep(200);
updateMessage("list complete");
}
return employees;
}
};
progressBar.progressProperty().bind(task.progressProperty());
progressLabel.textProperty().bind(task.messageProperty());
listView.itemsProperty().bind(task.valueProperty());
}
@FXML
public void buttonPressed() {
new Thread(task).start();
}
}
- Progress label shows each name as it is added to the list then a 'list complete' message is displayed
- Status: Working
- To-Do: Nothing
- Udemy: Java Programming Masterclass for Software Developers
- SimpleTech TV: Fix JavaFX runtime components are missing and are required to run this application in IntelliJ IDEA
- N/A
- Repo created by ABateman, email: gomezbateman@yahoo.com