I am new to JavaFx. I have designed a table thorugh scenebuilder where in column 1 I have a button and in column 2 I have a checkbox. What I want to get the checkbox property when I click the button. But the problem is I am always getting false as output. here is my code
Model.java
private boolean Active;
public boolean isActive() {
return Active;
}
public void setActive(boolean active) {
Active = active;
}
In my controller class I have designed the initialize() method below way
Controller.java
@FXML
private TableView<Model> tableBuilding;
@FXML
private TableColumn<Model,Boolean> colActive;
@FXML
private TableColumn<Model,Model> colAction;
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
//Containing the button
colAction.setCellFactory(col -> {
Button ShowButton = new Button("Show");
TableCell<Model, Model> cell = new TableCell<Model, Model>() {
@Override
//Updating with the number of row
public void updateItem(Model building, boolean empty) {
super.updateItem(building, empty);
if (empty) {
setGraphic(null);
} else {
setGraphic(ShowButton);
}
}
};
ShowButton.setOnAction(event -> {
TableRow row = cell.getTableRow();
Model building=(Model) row.getItem();
ObservableList<Model> data = tableBuilding.getItems();
for (Model item : data){
//check the boolean value of each item to determine checkbox state
System.out.println(item.isActive());
}
});
return cell ;
});
//Containing the checkbox
colActive.setCellValueFactory(new PropertyValueFactory<Model,Boolean>("Active"));
colActive.setCellFactory(tc -> new CheckBoxTableCell<>());
}
here System.out.println(item.isActive());
is always retunrning false as output. How can I get the actual property of that checkbox? Can anyone help me please?
I believe you can simplify your controller a bit. Try changing your controller code to:
public class Controller {
@FXML
private TableView<Model> tableBuilding;
@FXML
private TableColumn<Model, Boolean> colActive;
@FXML
private TableColumn<Model, Model> colAction;
@FXML
public void initialize() {
// Sample List
ObservableList<Model> models = FXCollections.observableArrayList();
models.addAll(
new Model(),
new Model(),
new Model()
);
tableBuilding.setItems(models);
//Containing the button
colAction.setCellFactory(col -> {
Button ShowButton = new Button("Show");
TableCell<Model, Model> cell = new TableCell<>() {
@Override
//Updating with the number of row
public void updateItem(Model building, boolean empty) {
super.updateItem(building, empty);
if (empty) {
setGraphic(null);
} else {
setGraphic(ShowButton);
}
}
};
ShowButton.setOnAction(event -> {
TableRow row = cell.getTableRow();
Model building = (Model) row.getItem();
System.out.println(building.getActive());
});
return cell;
});
//Containing the checkbox
colActive.setCellValueFactory(new PropertyValueFactory<>("active"));
colActive.setCellFactory(col -> new CheckBoxTableCell<>());
}
}
Specifically, the last two lines of code; you shouldn't need to specify your own TableCell
as the CheckBoxTableCell
handles the updating of the Model
on its own.
However, the Model
class needs to be changed to update the Active
field to a true property; otherwise, the TableView
cannot update it properly. The line in your controller colActive.setCellValueFactory(new PropertyValueFactory<Model,Boolean>("Active"));
tells the cell's value to be bound to a JavaFX Property in your Model
class named Active
, but there is no such property.
Here's the proper way to define the property:
public class Model {
private SimpleBooleanProperty active = new SimpleBooleanProperty(true);
public boolean getActive() {
return active.get();
}
public SimpleBooleanProperty activeProperty() {
return active;
}
public void setActive(boolean active) {
this.active.set(active);
}
}
Interestingly, this seems to be one of those cases where it's important to follow proper Java coding practices. The Active
property should be lowercase here as well, not capitalized.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.