JavaFX 2: Obtenir TableCell Index De Ligne

J'ai un Tableau avec des cases à cocher. Je veux changer la sélection de la case à cocher dans la première colonne, lorsque je clique sur la case de la troisième ou de la quatrième colonne. Je veux être en mesure de changer les autres cellules de la même ligne. J'ai déjà les colonnes, donc je veux savoir à quel rang se trouve la cellule. Je suis également très incertain de savoir si j'ai droit ou pas.

Ce que j'ai fait jusqu'à présent, je pensais surtout à partir de

JavaFX 2: Obtenir TableCell Index De Ligne

Voici mon SSCCE (Court Autonome Compilable Exemple)

S'il vous plaît corrigez-moi si il ya quelque chose de mal avec le code ci-dessous.

package javafxapplication5;
import javafx.application.Application;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Callback;
public class JavaFXApplication extends Application {
private static final ObservableList<ContactOptions> addContactOption = FXCollections.observableArrayList(
new ContactOptions("Yes", "John Doe", "No", "Yes"),
new ContactOptions("Yes", "Jane Doe", "No", null),
new ContactOptions("Yes", "John Smith", "Yes", "Yes"),
new ContactOptions("Yes", "Patty Smith", "Yes", "No"),
new ContactOptions("Yes", "Jo Johnson", "Yes", "Yes"),
new ContactOptions("No", "Mary Johnson", "No", "No"),
new ContactOptions("Yes", "Clint Doe", "No", null),
new ContactOptions("Yes", "Sally Sue", "No", "Yes"),
new ContactOptions("Yes", "Bob Ryan", null, "Yes"),
new ContactOptions("No", "Mary Sue", "No", "No"),
new ContactOptions("Yes", "Bob Smith", "No", "Yes"));
private static TableView<ContactOptions> contactOptions = new TableView<ContactOptions>();
public static void main(String[] args) {
Application.launch(JavaFXApplication.class, args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World");
Group root = new Group();
Scene scene = new Scene(root, 400, 200, Color.LIGHTGREEN);
Callback<TableColumn, TableCell> cellFactory = new Callback<TableColumn, TableCell>() {
@Override
public TableCell call(final TableColumn param) {
final CheckBox checkBox = new CheckBox();
final TableCell cell = new TableCell() {
@Override
public void updateItem(Object item, boolean empty) {
super.updateItem(item, empty);
if (item == null) {
checkBox.setDisable(true);
checkBox.setSelected(false);
} else {
checkBox.setDisable(false);
checkBox.setSelected(item.toString().equals("Yes") ? true : false);
commitEdit(checkBox.isSelected() ? "Yes" : "No");
}
}
};
cell.setNode(checkBox);
return cell;
}
};
TableColumn firstCol = new TableColumn("Contact?");
firstCol.setPrefWidth(60);
firstCol.setProperty("one");
firstCol.setCellFactory(cellFactory);
TableColumn secondCol = new TableColumn("Name");
secondCol.setPrefWidth(200);
secondCol.setSortAscending(true);
secondCol.setProperty("two");
TableColumn thirdCol = new TableColumn("Call");
thirdCol.setPrefWidth(60);
thirdCol.setProperty("three");
thirdCol.setCellFactory(cellFactory);
TableColumn fourthCol = new TableColumn("Email");
fourthCol.setPrefWidth(60);
fourthCol.setProperty("four");
fourthCol.setCellFactory(cellFactory);
contactOptions.setItems(addContactOption);
contactOptions.getColumns().addAll(firstCol, secondCol, thirdCol, fourthCol);
contactOptions.setPrefSize(400, 200);
root.getChildren().add(contactOptions);
primaryStage.setScene(scene);
primaryStage.setVisible(true);
}
public static class ContactOptions {
private final StringProperty one;
private final StringProperty two;
private final StringProperty three;
private final StringProperty four;
ContactOptions(String col1, String col2, String col3, String col4) {
this.one = new StringProperty(col1);
this.two = new StringProperty(col2);
this.three = new StringProperty(col3);
this.four = new StringProperty(col4);
}
public String getOne() {
return one.get();
}
public String getTwo() {
return two.get();
}
public String getThree() {
return three.get();
}
public String getFour() {
return four.get();
}
}
}

OriginalL'auteur Dorothy | 2011-08-29