1

I followed many links and found a solution to display check box in table view. But I am unable to change the value of checkbox in table view.

Link I followed: How to add CheckBox's to a TableView in JavaFX

Model Class:

public class TUser {
    private SimpleStringProperty name;
    private SimpleStringProperty address;
    private SimpleBooleanProperty active;

public TUser(String name, String address, boolean active) {
    this.name = new SimpleStringProperty(name);
    this.address = new SimpleStringProperty(address);
    this.active = new SimpleBooleanProperty(active);
}

public String getName() {
    return name.get();
}

public void setName(String name) {
    this.name = new SimpleStringProperty(name);
}

public String getAddress() {
    return address.get();
}

public void setAddress(String address) {
    this.address = new SimpleStringProperty(address);
}

public boolean getActive() {
    return active.get();
}

public void setActive(boolean active) {
    this.active = new SimpleBooleanProperty(active);
}

}

FXML File:

<TableView fx:id="usertable" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="500.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
  <columns>
      <TableColumn fx:id="name" prefWidth="142.0" text="Name" />
    <TableColumn fx:id="address" prefWidth="147.0" text="Address" />
      <TableColumn fx:id="active" prefWidth="153.0" text="Active" />
  </columns>
   <columnResizePolicy>
      <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
   </columnResizePolicy>
</TableView>

Controller Class:

public class UserManagement extends AnchorPane {

    @FXML
    private TableView<TUser> usertable;
    @FXML
    private TableColumn<TUser, String> address;
    @FXML
    private TableColumn<TUser, String> name;
    @FXML
    private TableColumn<TUser, Boolean> active;

    public UserManagement() throws IOException {
        initGraphics();
        initTable();
    }

    private class CheckBoxCellFactory<S, T> implements Callback<TableColumn<S, T>, TableCell<S, T>> {
    @Override public TableCell<S, T> call(TableColumn<S, T> p) {
        return new CheckBoxTableCell<>();
    }
}

    private void initGraphics() throws IOException {
        FXMLLoader content = new FXMLLoader(getClass().getResource("/mypack/fxmls/UserManagement.fxml"));
        content.setController(this);
        Node contentnode = (Node) content.load();
        AnchorPane.setBottomAnchor(contentnode, 0.0);
        AnchorPane.setLeftAnchor(contentnode, 0.0);
        AnchorPane.setRightAnchor(contentnode, 0.0);
        AnchorPane.setTopAnchor(contentnode, 0.0);
        getChildren().add(contentnode);

        active.setCellFactory(new CheckBoxCellFactory<TUser, Boolean>());

        address.setCellValueFactory(new PropertyValueFactory<TUser, String>("address"));
        name.setCellValueFactory(new PropertyValueFactory<TUser, String>("name"));
        active.setCellValueFactory(new PropertyValueFactory<TUser, Boolean>("active"));

    }

    private void initTable(){
        ObservableList<TUser> data = FXCollections.observableArrayList(new TUser("ABC", "ABC Road", true));
        usertable.setItems(data);
    }

}

Output:

TableView

Community
  • 1
  • 1
Biswadip Dey
  • 509
  • 2
  • 7
  • 20

1 Answers1

1

To make the check box properly bind to the property in the model class, you need to define a property accessor method:

public class TUser {

    // other methods and fields as before....

    public BooleanProperty activeProperty() {
        return active ;
    }

}

It's probably a good idea to define similar methods for your other properties too.

Note that your table is not editable, so the checkboxes cannot be manipulated by the user. If you want them to be editable, then make the TableView editable:

<TableView fx:id="usertable" editable="true" ...>

The TableColumns are editable by default.

James_D
  • 201,275
  • 16
  • 291
  • 322
  • But I don't want to make it editable. I only want show the data so that user cann't edit it. – Biswadip Dey Jan 08 '15 at 17:29
  • Ah, ok. (By "I am unable to change the value of the checkbox" I understood that you wanted the user to be able to change them. My mistake.) Just define the property accessor method, and it will properly bind to the property. – James_D Jan 08 '15 at 17:40
  • Updated answer accordingly. – James_D Jan 08 '15 at 18:02
  • Yes it is working but the checkbox is showing little blurred, How to make its opacity to 1. I have tried like this, private class CheckBoxCellFactory implements Callback, TableCell> { @Override public TableCell call(TableColumn p) { CheckBoxTableCell cell = new CheckBoxTableCell<>(); cell.setStyle("-fx-opacity: 1"); return cell; } but it is not working. – Biswadip Dey Jan 09 '15 at 17:23