Abhängig von einem Zelleninhalt soll in der gleichen Row die Checkbox disabled werden.
Code:
package checkbox;
import javafx.application.Application;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
import javafx.util.Callback;
public class TableCellFactorySample extends Application {
public static final String DONT_MISS = "Love";
private void init(Stage primaryStage) {
Group root = new Group();
primaryStage.setScene(new Scene(root));
final ObservableList<Needs> data = FXCollections.observableArrayList(new Needs(true, "Gold"),
new Needs(false, "SUV"), new Needs(true, DONT_MISS), new Needs(true, "Diamonds"),
new Needs(false, "Boot"));
TableColumn<Needs, Boolean> neededColumn = new TableColumn<>("Need Things");
neededColumn.setMinWidth(50);
neededColumn.setCellValueFactory(
new Callback<TableColumn.CellDataFeatures<Needs, Boolean>, ObservableValue<Boolean>>() {
@Override
public ObservableValue<Boolean> call(CellDataFeatures<Needs, Boolean> param) {
Needs need = param.getValue();
SimpleBooleanProperty needBooleanProperty = new SimpleBooleanProperty(need.isNeeded());
needBooleanProperty.addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue,
Boolean newValue) {
if (need.getName().equals(DONT_MISS)) {
System.err.println("Avoid changing the Checkbox");
} else {
System.err.println("Changing");
}
System.err.println(need.getName() + " / " + needBooleanProperty + "\n");
}
});
return needBooleanProperty;
}
});
neededColumn.setCellFactory(//
new Callback<TableColumn<Needs, Boolean>, TableCell<Needs, Boolean>>() {
@Override
public TableCell<Needs, Boolean> call(TableColumn<Needs, Boolean> abbreviation) {
CheckBoxTableCell<Needs, Boolean> checkBoxTableCell = new CheckBoxTableCell<Needs, Boolean>();
checkBoxTableCell.setAlignment(Pos.CENTER);
return checkBoxTableCell;
}
});
TableColumn<Needs, String> nameColumn = new TableColumn<>("Name");
nameColumn.setCellValueFactory(new PropertyValueFactory<Needs, String>("name"));
TableView<Needs> tableView = new TableView<>();
tableView.setItems(data);
tableView.setEditable(true);
tableView.getColumns().add(neededColumn);
tableView.getColumns().add(nameColumn);
root.getChildren().add(tableView);
}
@Override
public void start(Stage primaryStage) throws Exception {
init(primaryStage);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}