JavaFX 2: Get TableCell Row Pointer

I have a table with checkboxes. I want to change the checkbox selection in the first column when I click on the checkbox in the third or fourth column. I want to be able to change other cells on the same line. I already have columns, so I want to know which row the cell is in. I also very much doubt whether I still have the right or not.

What I have done so far, I basically considered

enter image description here

Here is my SSCCE (Short Self Contained Compilable Example)

Please correct me if something is wrong with the code below.

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(); } } } 
+7
source share
2 answers

Almost there

Before calling commitEdit you must call getTableView().edit(getTableRow().getIndex(), param) . This puts the cell in edit mode. Since there is no startEdit method, it is very little involved in entering edit mode, but it is still required.

After that, as described here: http://download.oracle.com/javafx/2.0/ui_controls/table-view.htm

Must be called

 firstCol.setOnEditCommit(new EventHandler<EditEvent<String>>() { @Override public void handle(EditEvent<String> event) { String newValue = event.getNewValue(); ContactOptions data = (ContactOptions) event.getTableView().getItems().get(event.getTablePosition().getRow()); data.one.set(newValue) if(newValue.equals("No")) { data.three.set("No"); data.four.set("No"); } } } 

Now I need to know how to update the table display after updating the data .

+6
source

The advantage of using Observables is that JavaFX interface elements can do the bindings for you behind the scenes. In other words, if you implement your data model class as a JavaFX Bean, your user interface will automatically update with every change. This is because the bindings for the observed data in your model are automatically assigned and notification events are automatically changed.

But you must define your data model according to the JavaFX bean paradigm for this to happen, otherwise your user interface will not be updated as changes occur.

Your data model is defined as follows:

 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(); } } 

For this answer, I will focus only on your first instance field. To convert this so that it matches the JavaFX bean paradigm for the JavaFX property, write your code this way, for example:

 public static class ContactOptions { private final StringProperty one = new SimpleStringProperty(); public final String getOne() { return this.one.get(); } public final void setOne(String v) { this.one.set(v); } public final StringProperty oneProperty() { return this.one; } 

You can write property definitions for the JavaFX bean that provide lazier initialization, but this will work. The difference between a Java bean and a JavaFX bean is that you must also provide an accessory for the property (last line above).

If you do all of your fields in properties similar to the ones above, you will find that your user interface is updated to reflect the changes.

+1
source

All Articles