JavaFX刷新TableView线程 [英] JavaFX refresh TableView thread

查看:111
本文介绍了JavaFX刷新TableView线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望每隔几秒钟在 QueueTabPageController 中实现一个运行 displayQueue 方法的线程,以便表格视图在JavaFX GUI上自动更新。目前,通过按GUI中的刷新按钮手动更新,但这并不理想。我在这里尝试了一些使用一些例子的方法,但似乎无法正常运行。顺便说一句,我是新手。任何帮助将不胜感激。

I wish to implement a thread that runs the displayQueue method in QueueTabPageControllerevery couple of seconds so that a table view is updated automatically on a JavaFX GUI. Currently it is updated manually by pressing the refresh button in the GUI but this is not ideal. I have tried various methods using some examples on here but cant seem to get it running correctly. I am new to threads by the way. 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);
  }

}

谢谢,
K

thanks, K

推荐答案

这样的事情可能是:

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

这将启动一个每5秒调用displayQueue(Queue.queue)的线程。由于displayQueue(...)调用tableView.setItems(...),因此必须在JavaFX Application Thread上调用它,因此需要使用Platform.runLater()。

This will start a thread that calls displayQueue(Queue.queue) every 5 seconds. Since displayQueue(...) calls tableView.setItems(...), it must be called on the JavaFX Application Thread, thus the requirement to use Platform.runLater().

t.setDaemon(真);所有其他线程完成后,将确保线程不会阻止JVM终止。可能有更好的方法来处理线程关闭......

t.setDaemon(true); will make sure the thread does not prevent the JVM from terminating when all other threads have finished. There are probably better ways to handle thread shutdown...

这篇关于JavaFX刷新TableView线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆