JavaFX 2.1 TableView refresh items

2019-01-03 05:28发布

I have this common issue, as it appears to be. My table view wont refresh my items after I reset them. I have checked the data and it's the new one.

I tried multiple solution from internet but no success.

Can't reset all the columns because it adds one empty one extra (dont know why) and the resize just breakes.

My table is not editable. The new data is changed.

The data is refreshed if I change the ordering of the items and the rows change (:|).

I'm just left without ideas.

At the moment the refresh code is pretty simple.

ObservableList<User> data = FXCollections.observableArrayList(User.getResellers());
reseller_table.setItems(data);

Again the new data is correct. When I make an selection to the tableView it returns the new correct Item.

24条回答
smile是对你的礼貌
2楼-- · 2019-01-03 06:00

Since JavaFX 8u60 you can use(assuming tableView is an instance of TableView class):

tableView.refresh();

From the documentation:

Calling refresh() forces the TableView control to recreate and repopulate the cells necessary to populate the visual bounds of the control. In other words, this forces the TableView to update what it is showing to the user. This is useful in cases where the underlying data source has changed in a way that is not observed by the TableView itself.

查看更多
Fickle 薄情
3楼-- · 2019-01-03 06:02

I have been trying to find a way to refresh the tableView(ScalaFx) for 3-4 hours. Finally I got a answer. I just want to publish my solution because of i wasted already hours.

-To retrieve the rows from database, i used to declare a method which returns ObservableBuffer.

My JDBC CLASS

    //To get all customer details
def getCustomerDetails : ObservableBuffer[Customer] = {

val customerDetails = new ObservableBuffer[Customer]()
  try {

    val resultSet = statement.executeQuery("SELECT * FROM MusteriBilgileri")

    while (resultSet.next()) {

      val musteriId = resultSet.getString("MusteriId")
      val musteriIsmi = resultSet.getString("MusteriIsmi")
      val urununTakildigiTarih = resultSet.getDate("UrununTakildigiTarih").toString
      val bakimTarihi = resultSet.getDate("BakimTarihi").toString
      val urununIsmi = resultSet.getString("UrununIsmi")
      val telNo = resultSet.getString("TelNo")
      val aciklama = resultSet.getString("Aciklama")

      customerDetails += new Customer(musteriId,musteriIsmi,urununTakildigiTarih,bakimTarihi,urununIsmi,telNo,aciklama)

    }
  } catch {
    case e => e.printStackTrace
  }

  customerDetails
}

-And I have created a TableView object.

var table = new TableView[Customer](model.getCustomerDetails)
table.columns += (customerIdColumn,customerNameColumn,productInstallColumn,serviceDateColumn,
        productNameColumn,phoneNoColumn,detailColumn)

-And Finally i got solution. In the refresh button, i have inserted this code;

table.setItems(FXCollections.observableArrayList(model.getCustomerDetails.delegate))

model is the reference of my jdbc connection class

val model = new ScalaJdbcConnectSelect

This is the scalafx codes but it gives some idea to javafx

查看更多
啃猪蹄的小仙女
4楼-- · 2019-01-03 06:03

I suppose this thread has a very good description of the problem with table refresh.

查看更多
ら.Afraid
5楼-- · 2019-01-03 06:04

Based on Daniel De León's answer

public static void refresh_table(TableView table)
{
        for (int i = 0; i < table.getColumns().size(); i++) {
    ((TableColumn)(table.getColumns().get(i))).setVisible(false);
    ((TableColumn)(table.getColumns().get(i))).setVisible(true);
    }
}
查看更多
相关推荐>>
6楼-- · 2019-01-03 06:05

I had a similar problem with refreshing. My solution was to restrict the operations on the ObservableList to those which work correctly with bind().

Assume ObservableList obsList is the underlying list for the TableView.

Then
obsList.clear() (inherited from java.util.List<>) will not update the TableView correctly but.

Also calling setItem(obsList) did not work to trigger a refresh...but...

obsList.removeAll(obsList) (overwritten by ObservableList) works fine because it fires the changeEvent correctly.

Refilling a list with completely new content then works as follows:

  • obsList.removeAll(obsList);
  • obsList.add(...); //e.g. in a loop...

or

  • obsList.removeAll(obsList);
  • FXCollections.copy(obsList, someSourceList)

Best regards Ingo

查看更多
虎瘦雄心在
7楼-- · 2019-01-03 06:06

I have an use case where nothing else helped as the solution from Aubin. I adapted the method and changed it by removing and adding an item to the tables' item list as it works in the end only reliable with this hack, the column visible toggle did the job only the first time.

I reported it also in the Jira task: https://javafx-jira.kenai.com/browse/RT-22463

 public <T> void tableItemsRefresh(final ObservableList<T> items) {

      if (items == null || items.size() == 0)
         return;

      int idx = items.size() -1;
      final T item = items.get(idx);
      items.remove(idx);

      new Timer().schedule(new TimerTask() {
         @Override
         public void run() {
            Platform.runLater(new Runnable() {
               @Override
               public void run() {
                  items.add(item);
               }
            });
         }
      }, 100);
   } 
查看更多
登录 后发表回答