In Java FX 2.x TableView is one of the controls you can't set a tooltip directly. You must set tooltips for TableCell or TableRow instead. The technique used in this turorial is very similar for both controls. Let's take a look!
Tooltips on TableCell
myTableColumn.setCellFactory(new Callback<TableColumn<DataModel, Object>, TableCell<DataModel, Object>>() {
@Override
public TableCell<DataModel, Object> call(TableColumn<DataModel, Object> p) {
return new TableCell<DataModel, Object>() {
@Override
public void updateItem(Object t, boolean empty) {
super.updateItem(t, empty);
if (t == null) {
setTooltip(null);
setText(null);
} else {
Tooltip tooltip = new Tooltip();
DataModel myModel = getTableView().getItems().get(getTableRow().getIndex());
tooltip.setText(myModel.getTip());
setTooltip(tooltip);
setText(t.toString());
}
}
};
}
});
To add tooltips to column cells we have to set a custom cell factory for the target column. In our example DataModel
is just the TableView's data model, change that to your model's name. Also this example's model returns Object
so that's one more thing you will probably need to change over the casts. The getTip()
method is there to also show you how to get the TableRow Data Item.
Tooltips on TableRow
myTable.setRowFactory(new Callback<TableView, TableRow>() {
@Override
public TableRow call(final TableView p) {
return new TableRow() {
@Override
public void updateItem(DataModel item, boolean empty) {
super.updateItem(item, empty);
if (item == null) {
setTooltip(null);
} else {
Tooltip tooltip = new Tooltip();
tooltip.setText(getItem().getTip());
setTooltip(tooltip);
}
}
};
}
});
Notice that for rows we need to set our custom row factory to the target table not column! Also it's much simpler to get the DataModel when working on rows, you only need to call getItem()
method.