The best way is not to use an index, but to use a custom factory string and observe the corresponding properties of the element in the string.
This is a bit complicated with the current API, since you probably need to observe the item property of the table row property. You can use Bindings.select(...)
, but the current version displays a lot of unnecessary warnings when the element is null (which will be quite common). I prefer to use the EasyBind structure for this kind of function.
In this example, all rows of the table are disabled for which the property value of the displayed item is less than 5:
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Random; import java.util.function.Function; import javafx.application.Application; import javafx.beans.binding.Bindings; import javafx.beans.property.IntegerProperty; import javafx.beans.property.ReadOnlyObjectWrapper; import javafx.beans.property.SimpleIntegerProperty; import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.StringProperty; import javafx.beans.value.ObservableValue; import javafx.collections.ObservableList; import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.ContentDisplay; import javafx.scene.control.TableCell; import javafx.scene.control.TableColumn; import javafx.scene.control.TableRow; import javafx.scene.control.TableView; import javafx.scene.layout.BorderPane; import javafx.stage.Stage; import org.fxmisc.easybind.EasyBind; public class DisabledTableRowExample extends Application { @Override public void start(Stage primaryStage) { TableView<Item> table = new TableView<>(); table.getItems().addAll(createData()); TableColumn<Item, Item> deleteCol = createTableColumn("Delete", ReadOnlyObjectWrapper<Item>::new); deleteCol.setCellFactory(this::createDeleteCell); table.getColumns().addAll(Arrays.asList( createTableColumn("Name", Item::nameProperty), createTableColumn("Value", Item::valueProperty), deleteCol ));
If you really disconnect based on the index, you can use a very similar method:
IntegerProperty disabledRowIndex = new SimpleIntegerProperty(); // ... // in row factory do: row.disableProperty().bind(row.indexProperty.isEqualTo(disabledRowIndex));
Then calling disabledRowIndex.set(...)
will disconnect the row in the specified pointer.
Note that disable
semantics may not be exactly what you want. This disables all input in the table row (for example, the delete button will not be enabled); however, it does not prevent the selection of a row (keyboard navigation is controlled by the table itself, so you can select a row using the keyboard). Defining custom selection behavior is more complex.
source share