AndrewJBateman / javafx-observables-tasks

:clipboard: JavaFX using observables and threads

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

⚑ JavaFX Observables Tasks

  • 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

GitHub repo size GitHub pull requests GitHub Repo stars GitHub last commit

πŸ“„ Table of contents

πŸ“š General info

  • 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

πŸ“· Screenshots

N/A

πŸ“Ά Technologies

πŸ’Ύ Setup

  • Open folder in an IDE such as IntelliJ. Run from Main.java

πŸ’» Code Examples

  • 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();
  }
}

πŸ†’ Features

  • Progress label shows each name as it is added to the list then a 'list complete' message is displayed

πŸ“‹ Status & To-Do List

  • Status: Working
  • To-Do: Nothing

πŸ‘ Inspiration

πŸ“ License

  • N/A

βœ‰οΈ Contact

About

:clipboard: JavaFX using observables and threads


Languages

Language:Java 95.4%Language:HTML 4.6%