JavaFX TableView Update

I want to implement a thread that runs the method displayQueuein QueueTabPageControllerevery couple of seconds to view tables are automatically updated in JavaFX GUI. It is currently being updated manually by clicking the refresh button in the GUI, but this is not ideal. I tried various methods using some examples here, but it doesn't seem to work correctly. By the way, I'm new to streams. Any help would be greatly appreciated.

public class QueueTabPageController implements Initializable {

  @FXML
  private TableView<Patient> tableView;

  @FXML
  private TableColumn<Patient, String> firstNameColumn;

  @FXML
  private TableColumn<Patient, String> lastNameColumn;

  @FXML
  private TableColumn<Patient, String> timeEnteredColumn;

  @FXML
  private TableColumn<Patient, String> triageAssessmentColumn;

  @FXML
  private QueueTabPageController queueTabPageController;

  private ObservableList<Patient> tableData;

  @Override
  public void initialize(URL arg0, ResourceBundle arg1) {

    assert tableView != null : "fx:id=\"tableView\" was not injected: check your FXML file 'FXMLQueueTabPage.fxml'";

    firstNameColumn.setCellValueFactory(new PropertyValueFactory<Patient, String>("firstName"));
    lastNameColumn.setCellValueFactory(new PropertyValueFactory<Patient, String>("lastName"));
    timeEnteredColumn.setCellValueFactory(new PropertyValueFactory<Patient, String>("time"));
    triageAssessmentColumn.setCellValueFactory(new PropertyValueFactory<Patient, String>("triage"));

    // display the current queue to screen when opening page each time
    displayQueue(Queue.queue);

  }

  /**
   * @param event
   * @throws IOException
   */
  @FXML
  private void btnRefreshQueueClick(ActionEvent event) throws IOException {
    displayQueue(Queue.queue);
  }

  /**
   * @param queue
   */
  public void displayQueue(LinkedList<Patient> queue) {
    tableData = FXCollections.observableArrayList(queue);
    tableView.setItems(tableData);
    tableView.getColumns().get(0).setVisible(false);
    tableView.getColumns().get(0).setVisible(true);
  }

}

thanks K

+4
source share
1 answer

Something like this could be:

    Thread t = new Thread(() -> {
        while (true) {
            Thread.sleep(5000); // sleep 5 secs
            Platform.runLater(() -> {   // Ensure data is updated on JavaFX thread 
                displayQueue(Queue.queue);
            });
        }
    }); 
    t.setDaemon(true);
    t.start();

, displayQueue (Queue.queue) 5 . displayQueue (...) tableView.setItems(...), Thread Application JavaFX, Platform.runLater().

t.setDaemon(); , JVM, . , , ...

0

Source: https://habr.com/ru/post/1584794/


All Articles