field as a parameter and uses "getFieldName()" and "setFieldName()" methods in the rows to get/set
the value of the cell.
For example:
taskIdColumn.setCellValueFactory(new PropertyValueFactory<>("taskId"));
assuming that the row has a getTaskId() and a setTaskId() pair of methods.
We can create a context menu on a table by creating an instance of a ContextMenu and setting
the contextMenu property of the table to this newly created ContextMenu object. For
example:
ContextMenu menu = new ContextMenu();
MenuItem item = new MenuItem("test");
menu.getItems().add(item);
tableView.setContextMenu(menu);
The selection model of a table is obtained through the "selectionModel" property. This
returns an instance of a TableViewSelectionModel.
The TableColumn has a set of interesting properties including:
• visible – Should the column be displayed?
• columns – ability to nest columns
• sortable – Is this column sortable?
• text – The header of the column
• prefWidth – The preferred width of the column (in pixels)
The TableView has some interesting properties too:
• placeholder – A node to be shown when there is no content in the table.
CellFactory
So far we have assumed that the content of a cell is a text string however what if we want
something different? What if we want to change the text color or font or something bigger, for
example an image or a button? How can we go about that?
Previously we looked at the method called setCellValueFactory(). This returns the value
to be passed to the cell. The cell then renders this value at the appropriate location. There is
another method of interest to us called setCellFactory(). What this method does is register
how the cell itself is to be rendered (not just the value for the cell).
Again this function takes a Callback instance as a parameter which means that we will have to
implement the call() method. The input to the call method is:
TableColumn<RowClass,ColumnClass>
Remember, "RowClass" is the class type of the rows in the TableView and "ColumnClass"
is the class type for the content for cells in this column.
and the return from call() is
TableCell<RowClass,ColumnClass>
The TableCell has an important property called graphic which is a JavaFX Node. This can
Page 273
Kommentare zu diesen Handbüchern