TableColumn javafx

 
  

public class TableColumn

extends java.lang.Object 

implements EventTarget

A TableView is made up of a number of TableColumn instances. Each TableColumn in a table is responsible for displaying (and editing) the contents of that column. As well as being responsible for displaying and editing data for a single column, a TableColumn also contains the necessary properties to:

  • Be resized (using minWidth/ prefWidth/maxWidth and width properties)
  • Have its visibility toggled
  • Display header text
  • Display any nested columns it may contain
  • Have a context menu when the user right-clicks the column header area
Have the contents of the table be sorted (using comparator, sortable and sortType)When creating a TableColumn instance, perhaps the two most important properties to set are the column text (what to show in the column header area), and the column cell value factory (which is used to populate individual cells in the column). This can be achieved using some variation变异) on the following code:

 ObservableList data = ...
 TableView tableView = new TableView(data);
 
 TableColumn firstNameCol = new TableColumn("First Name");
 firstNameCol.setCellValueFactory(new Callback, ObservableValue>() {
     public ObservableValue call(CellDataFeatures p) {
         // p.getValue() returns the Person instance for a particular TableView row
         return p.getValue().firstNameProperty();
     }
  });
 
 tableView.getColumns().add(firstNameCol);}

This approach assumes that the object returned from p.getValue() has a JavaFX ObservableValue that can simply be returned. The benefit of this is that the TableView will internally create bindings to ensure that, should the returned ObservableValue change, the cell contents will be automatically refreshed.
In situations where a TableColumn must interact(相互作用) with classes created before JavaFX, or that generally do not wish to use JavaFX apis for properties, it is possible to wrap the returned value in a ReadOnlyObjectWrapper instance. For example:
 firstNameCol.setCellValueFactory(new Callback, ObservableValue>() {
     public ObservableValue call(CellDataFeatures p) {
         return new ReadOnlyObjectWrapper(p.getValue().getFirstName());
     }
  });
It is hoped that over time there will be convenience cell value factories developed and made available to developers. As of the JavaFX 2.0 release, there is one such convenience class: PropertyValueFactory. This class removes the need to write the code above, instead relying on reflection to look up a given property from a String. Refer to the PropertyValueFactory class documentation for more information on how to use this with a TableColumn. Finally, for more detail on how to use TableColumn, there is further documentation in the TableView class documentation.
See Also:
TableView, TableCell, TablePosition


public final ObjectProperty,ObservableValue>> cellValueFactoryProperty()

The cell value factory needs to be set to specify how to populate all cells within a single TableColumn. A cell value factory is a Callback that provides a TableColumn.CellDataFeatures instance, and expects an ObservableValue to be returned. The returned ObservableValue instance will be observed internally to allow for immediate updates to the value to be reflected on screen. An example of how to set a cell value factory is:
 lastNameCol.setCellValueFactory(new Callback, ObservableValue>() {
     public ObservableValue call(CellDataFeatures p) {
         // p.getValue() returns the Person instance for a particular TableView row
         return p.getValue().lastNameProperty();
     }
  });
 }
 TableColumn.CellDataFeatures提供了这个TableView的相关信息,包括这个表格本身,加上填充这个表格的数据列表,而ObservableValue是用来填充这一列的数据,具体是什么数据,需要你在实现中给出,如上是Person类中的lastName


A common approach is to want to populate cells in a TableColumn using a single value from a Java bean. To support this common scenario(脚本), there is the PropertyValueFactory class. Refer to this class for more information on how to use it, but briefly here is how the above use case could be simplified using the PropertyValueFactory class:



 lastNameCol.setCellValueFactory(new PropertyValueFactory("lastName"));
 
See Also:
getCellValueFactory(), setCellValueFactory(Callback)

你可能感兴趣的:(javafx)