Hemnath T
Hemnath T

Reputation: 21

HOw to refresh a particular row or cell in Tableview

How to refresh a particular row or cell in Tableview.

Below is code line to refresh the complete table.

tableView.refresh()

Below is code line to refresh a particular column in the table.

tableView.getColumns().get(0).setVisible(false);
tableView.getColumns().get(0).setVisible(true);

How to refresh a particular row or particular cell?

Upvotes: 2

Views: 2923

Answers (1)

Vinagy
Vinagy

Reputation: 159

You shouldn't need to refresh your rows or your TableView. It is best practice to bind your cells to a property of the class you put in them.

From the javadocs for TableView:

You can achieve this by defining your class as such:

 public class Person {
 private StringProperty firstName;
 public void setFirstName(String value) { firstNameProperty().set(value); }
 public String getFirstName() { return firstNameProperty().get(); }
 public StringProperty firstNameProperty() { 
     if (firstName == null) firstName = new SimpleStringProperty(this, "firstName");
     return firstName; 
 }

 private StringProperty lastName;
 public void setLastName(String value) { lastNameProperty().set(value); }
 public String getLastName() { return lastNameProperty().get(); }
 public StringProperty lastNameProperty() { 
     if (lastName == null) lastName = new SimpleStringProperty(this, "lastName");
     return lastName; 
 } 
 }

Firstly, a TableView instance needs to be defined, as such:

 TableView<Person> table = new TableView<Person>();

With the basic table defined, we next focus on the data model. As mentioned, for this example, we'll be using a ObservableList. We can immediately set such a list directly in to the TableView, as such:

 ObservableList<Person> teamMembers = getTeamMembers();
 table.setItems(teamMembers);

With the items set as such, TableView will automatically update whenever the teamMembers list changes. If the items list is available before the TableView is instantiated, it is possible to pass it directly into the constructor.

At this point we now have a TableView hooked up to observe the teamMembers observableList. The missing ingredient now is the means of splitting out the data contained within the model and representing it in one or more TableColumn instances. To create a two-column TableView to show the firstName and lastName properties, we extend the last code sample as follows:

 ObservableList<Person> teamMembers = ...;
 table.setItems(teamMembers);

 TableColumn<Person,String> firstNameCol = new TableColumn<Person,String>("First Name");
 firstNameCol.setCellValueFactory(new PropertyValueFactory("firstName"));
 TableColumn<Person,String> lastNameCol = new TableColumn<Person,String>("Last Name");
 lastNameCol.setCellValueFactory(new PropertyValueFactory("lastName"));

 table.getColumns().setAll(firstNameCol, lastNameCol);

With the code shown above we have fully defined the minimum properties required to create a TableView instance. Running this code (assuming the people ObservableList is appropriately created) will result in a TableView being shown with two columns for firstName and lastName. Any other properties of the Person class will not be shown, as no TableColumns are defined.

If your class doesn't have properties and you don't want to implement them, you can create your own custom CellValueFactory like this:

 firstNameCol.setCellValueFactory(new Callback<CellDataFeatures<Person, String>, ObservableValue<String>>() {
     public ObservableValue<String> call(CellDataFeatures<Person, String> p) {
         // p.getValue() returns the Person instance for a particular TableView row
         return p.getValue().firstNameProperty();
     }
  });
 }

Note that this is straight from the javadocs.

Upvotes: 5

Related Questions