JavaFx Tableview checkbox requires focus












0















I implemented boolean representation in my tableView as checkbox. It works fine in general but very irritating fact is that it requires row to be focused (editing) to apply change of checkbox value. It means I first have to double click on the field and then click checkbox.



How to make checkbox change perform onEditCommit right away?



public class BooleanCell<T> extends TableCell<T, Boolean> {
private CheckBox checkBox;

public BooleanCell() {
checkBox = new CheckBox();
checkBox.selectedProperty().addListener(new ChangeListener<Boolean>() {
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
if (isEditing())
commitEdit(newValue == null ? false : newValue);
}
});
setAlignment(Pos.CENTER);
this.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
this.setEditable(true);
}

@Override
public void updateItem(Boolean item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
} else {
checkBox.setSelected(item);
setGraphic(checkBox);
}
}


}










share|improve this question























  • Have you set your TableView to be editable (not just the BooleanCell?

    – Zephyr
    Nov 18 '18 at 15:00






  • 2





    Why not use CheckBoxTableCell?

    – Slaw
    Nov 18 '18 at 15:02






  • 1





    On a side note, why not use a CheckBoxTableCell instead?

    – Zephyr
    Nov 18 '18 at 15:02
















0















I implemented boolean representation in my tableView as checkbox. It works fine in general but very irritating fact is that it requires row to be focused (editing) to apply change of checkbox value. It means I first have to double click on the field and then click checkbox.



How to make checkbox change perform onEditCommit right away?



public class BooleanCell<T> extends TableCell<T, Boolean> {
private CheckBox checkBox;

public BooleanCell() {
checkBox = new CheckBox();
checkBox.selectedProperty().addListener(new ChangeListener<Boolean>() {
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
if (isEditing())
commitEdit(newValue == null ? false : newValue);
}
});
setAlignment(Pos.CENTER);
this.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
this.setEditable(true);
}

@Override
public void updateItem(Boolean item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
} else {
checkBox.setSelected(item);
setGraphic(checkBox);
}
}


}










share|improve this question























  • Have you set your TableView to be editable (not just the BooleanCell?

    – Zephyr
    Nov 18 '18 at 15:00






  • 2





    Why not use CheckBoxTableCell?

    – Slaw
    Nov 18 '18 at 15:02






  • 1





    On a side note, why not use a CheckBoxTableCell instead?

    – Zephyr
    Nov 18 '18 at 15:02














0












0








0








I implemented boolean representation in my tableView as checkbox. It works fine in general but very irritating fact is that it requires row to be focused (editing) to apply change of checkbox value. It means I first have to double click on the field and then click checkbox.



How to make checkbox change perform onEditCommit right away?



public class BooleanCell<T> extends TableCell<T, Boolean> {
private CheckBox checkBox;

public BooleanCell() {
checkBox = new CheckBox();
checkBox.selectedProperty().addListener(new ChangeListener<Boolean>() {
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
if (isEditing())
commitEdit(newValue == null ? false : newValue);
}
});
setAlignment(Pos.CENTER);
this.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
this.setEditable(true);
}

@Override
public void updateItem(Boolean item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
} else {
checkBox.setSelected(item);
setGraphic(checkBox);
}
}


}










share|improve this question














I implemented boolean representation in my tableView as checkbox. It works fine in general but very irritating fact is that it requires row to be focused (editing) to apply change of checkbox value. It means I first have to double click on the field and then click checkbox.



How to make checkbox change perform onEditCommit right away?



public class BooleanCell<T> extends TableCell<T, Boolean> {
private CheckBox checkBox;

public BooleanCell() {
checkBox = new CheckBox();
checkBox.selectedProperty().addListener(new ChangeListener<Boolean>() {
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
if (isEditing())
commitEdit(newValue == null ? false : newValue);
}
});
setAlignment(Pos.CENTER);
this.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
this.setEditable(true);
}

@Override
public void updateItem(Boolean item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
} else {
checkBox.setSelected(item);
setGraphic(checkBox);
}
}


}







javafx checkbox tableview






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 18 '18 at 14:05









Mateusz GawełMateusz Gaweł

5231517




5231517













  • Have you set your TableView to be editable (not just the BooleanCell?

    – Zephyr
    Nov 18 '18 at 15:00






  • 2





    Why not use CheckBoxTableCell?

    – Slaw
    Nov 18 '18 at 15:02






  • 1





    On a side note, why not use a CheckBoxTableCell instead?

    – Zephyr
    Nov 18 '18 at 15:02



















  • Have you set your TableView to be editable (not just the BooleanCell?

    – Zephyr
    Nov 18 '18 at 15:00






  • 2





    Why not use CheckBoxTableCell?

    – Slaw
    Nov 18 '18 at 15:02






  • 1





    On a side note, why not use a CheckBoxTableCell instead?

    – Zephyr
    Nov 18 '18 at 15:02

















Have you set your TableView to be editable (not just the BooleanCell?

– Zephyr
Nov 18 '18 at 15:00





Have you set your TableView to be editable (not just the BooleanCell?

– Zephyr
Nov 18 '18 at 15:00




2




2





Why not use CheckBoxTableCell?

– Slaw
Nov 18 '18 at 15:02





Why not use CheckBoxTableCell?

– Slaw
Nov 18 '18 at 15:02




1




1





On a side note, why not use a CheckBoxTableCell instead?

– Zephyr
Nov 18 '18 at 15:02





On a side note, why not use a CheckBoxTableCell instead?

– Zephyr
Nov 18 '18 at 15:02












1 Answer
1






active

oldest

votes


















1














I'm not sure about the rest of your implementation, but I assume you do not have your TableView set to editable:



tableView.setEditable(true);




On a side note, you could easily use a CheckBoxTableCell instead of implementing your own (BooleanCell).



Here is a very simple application you can run to see how this works. Each CheckBox may be clicked without first focusing the row and its value updates your underlying model as well (which you can see by clicking the "Print List" button).



import javafx.application.Application;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class CheckBoxTableViewSample extends Application {

public static void main(String args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {

// Simple interface
VBox root = new VBox(5);
root.setPadding(new Insets(10));
root.setAlignment(Pos.CENTER);

// List of sample items
ObservableList<MyItem> myItems = FXCollections.observableArrayList();
myItems.addAll(
new MyItem(false, "Item 1"),
new MyItem(false, "Item 2"),
new MyItem(true, "Item 3"),
new MyItem(false, "Item 4"),
new MyItem(false, "Item 5")
);

// Create TableView
TableView<MyItem> tableView = new TableView<MyItem>();

// We need the TableView to be editable in order to allow each CheckBox to be selectable
tableView.setEditable(true);

// Create our table Columns
TableColumn<MyItem, Boolean> colSelected = new TableColumn<>("Selected");
TableColumn<MyItem, String> colName = new TableColumn<>("Name");

// Bind the columns with our model's properties
colSelected.setCellValueFactory(f -> f.getValue().selectedProperty());
colName.setCellValueFactory(f -> f.getValue().nameProperty());

// Set the CellFactory to use a CheckBoxTableCell
colSelected.setCellFactory(param -> {
return new CheckBoxTableCell<MyItem, Boolean>();
});

// Add our columns to the TableView
tableView.getColumns().addAll(colSelected, colName);

// Set our items to the TableView
tableView.setItems(myItems);

// Create a button to print out our list of items
Button btnPrint = new Button("Print List");
btnPrint.setOnAction(event -> {
System.out.println("-------------");
for (MyItem item : myItems) {
System.out.println(item.getName() + " = " + item.isSelected());
}
});

root.getChildren().addAll(tableView, btnPrint);
// Show the Stage
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}

/**
* Just a simple sample class to display in our TableView
*/
final class MyItem {

// This property will be bound to the CheckBoxTableCell
private final BooleanProperty selected = new SimpleBooleanProperty();

// The name of our Item
private final StringProperty name = new SimpleStringProperty();

public MyItem(boolean selected, String name) {
this.selected.setValue(selected);
this.name.set(name);
}

public boolean isSelected() {
return selected.get();
}

public BooleanProperty selectedProperty() {
return selected;
}

public void setSelected(boolean selected) {
this.selected.set(selected);
}

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

public StringProperty nameProperty() {
return name;
}

public void setName(String name) {
this.name.set(name);
}
}





share|improve this answer


























  • That's what I was looking for! Can you also guide what would be the best way to get some callback? Something like onEditCommit. I did it by implementing new Callback() {...}.call(Integer), but I dont think it's the best way. Maybe adding onEditCommit in fxml somehow?

    – Mateusz Gaweł
    Nov 18 '18 at 15:48











  • That's a bit too broad, @MateuszGaweł. It depends on what your needs are for the commit. If you simply want to update the selected property of the MyItem object, that is already done with the CheckBoxTableCell. You might want to ask a separate question regarding that.

    – Zephyr
    Nov 18 '18 at 15:49






  • 1





    Ok, understand. That example helped me a lot. Thank you very much:)

    – Mateusz Gaweł
    Nov 18 '18 at 15:51











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53361763%2fjavafx-tableview-checkbox-requires-focus%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














I'm not sure about the rest of your implementation, but I assume you do not have your TableView set to editable:



tableView.setEditable(true);




On a side note, you could easily use a CheckBoxTableCell instead of implementing your own (BooleanCell).



Here is a very simple application you can run to see how this works. Each CheckBox may be clicked without first focusing the row and its value updates your underlying model as well (which you can see by clicking the "Print List" button).



import javafx.application.Application;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class CheckBoxTableViewSample extends Application {

public static void main(String args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {

// Simple interface
VBox root = new VBox(5);
root.setPadding(new Insets(10));
root.setAlignment(Pos.CENTER);

// List of sample items
ObservableList<MyItem> myItems = FXCollections.observableArrayList();
myItems.addAll(
new MyItem(false, "Item 1"),
new MyItem(false, "Item 2"),
new MyItem(true, "Item 3"),
new MyItem(false, "Item 4"),
new MyItem(false, "Item 5")
);

// Create TableView
TableView<MyItem> tableView = new TableView<MyItem>();

// We need the TableView to be editable in order to allow each CheckBox to be selectable
tableView.setEditable(true);

// Create our table Columns
TableColumn<MyItem, Boolean> colSelected = new TableColumn<>("Selected");
TableColumn<MyItem, String> colName = new TableColumn<>("Name");

// Bind the columns with our model's properties
colSelected.setCellValueFactory(f -> f.getValue().selectedProperty());
colName.setCellValueFactory(f -> f.getValue().nameProperty());

// Set the CellFactory to use a CheckBoxTableCell
colSelected.setCellFactory(param -> {
return new CheckBoxTableCell<MyItem, Boolean>();
});

// Add our columns to the TableView
tableView.getColumns().addAll(colSelected, colName);

// Set our items to the TableView
tableView.setItems(myItems);

// Create a button to print out our list of items
Button btnPrint = new Button("Print List");
btnPrint.setOnAction(event -> {
System.out.println("-------------");
for (MyItem item : myItems) {
System.out.println(item.getName() + " = " + item.isSelected());
}
});

root.getChildren().addAll(tableView, btnPrint);
// Show the Stage
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}

/**
* Just a simple sample class to display in our TableView
*/
final class MyItem {

// This property will be bound to the CheckBoxTableCell
private final BooleanProperty selected = new SimpleBooleanProperty();

// The name of our Item
private final StringProperty name = new SimpleStringProperty();

public MyItem(boolean selected, String name) {
this.selected.setValue(selected);
this.name.set(name);
}

public boolean isSelected() {
return selected.get();
}

public BooleanProperty selectedProperty() {
return selected;
}

public void setSelected(boolean selected) {
this.selected.set(selected);
}

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

public StringProperty nameProperty() {
return name;
}

public void setName(String name) {
this.name.set(name);
}
}





share|improve this answer


























  • That's what I was looking for! Can you also guide what would be the best way to get some callback? Something like onEditCommit. I did it by implementing new Callback() {...}.call(Integer), but I dont think it's the best way. Maybe adding onEditCommit in fxml somehow?

    – Mateusz Gaweł
    Nov 18 '18 at 15:48











  • That's a bit too broad, @MateuszGaweł. It depends on what your needs are for the commit. If you simply want to update the selected property of the MyItem object, that is already done with the CheckBoxTableCell. You might want to ask a separate question regarding that.

    – Zephyr
    Nov 18 '18 at 15:49






  • 1





    Ok, understand. That example helped me a lot. Thank you very much:)

    – Mateusz Gaweł
    Nov 18 '18 at 15:51
















1














I'm not sure about the rest of your implementation, but I assume you do not have your TableView set to editable:



tableView.setEditable(true);




On a side note, you could easily use a CheckBoxTableCell instead of implementing your own (BooleanCell).



Here is a very simple application you can run to see how this works. Each CheckBox may be clicked without first focusing the row and its value updates your underlying model as well (which you can see by clicking the "Print List" button).



import javafx.application.Application;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class CheckBoxTableViewSample extends Application {

public static void main(String args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {

// Simple interface
VBox root = new VBox(5);
root.setPadding(new Insets(10));
root.setAlignment(Pos.CENTER);

// List of sample items
ObservableList<MyItem> myItems = FXCollections.observableArrayList();
myItems.addAll(
new MyItem(false, "Item 1"),
new MyItem(false, "Item 2"),
new MyItem(true, "Item 3"),
new MyItem(false, "Item 4"),
new MyItem(false, "Item 5")
);

// Create TableView
TableView<MyItem> tableView = new TableView<MyItem>();

// We need the TableView to be editable in order to allow each CheckBox to be selectable
tableView.setEditable(true);

// Create our table Columns
TableColumn<MyItem, Boolean> colSelected = new TableColumn<>("Selected");
TableColumn<MyItem, String> colName = new TableColumn<>("Name");

// Bind the columns with our model's properties
colSelected.setCellValueFactory(f -> f.getValue().selectedProperty());
colName.setCellValueFactory(f -> f.getValue().nameProperty());

// Set the CellFactory to use a CheckBoxTableCell
colSelected.setCellFactory(param -> {
return new CheckBoxTableCell<MyItem, Boolean>();
});

// Add our columns to the TableView
tableView.getColumns().addAll(colSelected, colName);

// Set our items to the TableView
tableView.setItems(myItems);

// Create a button to print out our list of items
Button btnPrint = new Button("Print List");
btnPrint.setOnAction(event -> {
System.out.println("-------------");
for (MyItem item : myItems) {
System.out.println(item.getName() + " = " + item.isSelected());
}
});

root.getChildren().addAll(tableView, btnPrint);
// Show the Stage
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}

/**
* Just a simple sample class to display in our TableView
*/
final class MyItem {

// This property will be bound to the CheckBoxTableCell
private final BooleanProperty selected = new SimpleBooleanProperty();

// The name of our Item
private final StringProperty name = new SimpleStringProperty();

public MyItem(boolean selected, String name) {
this.selected.setValue(selected);
this.name.set(name);
}

public boolean isSelected() {
return selected.get();
}

public BooleanProperty selectedProperty() {
return selected;
}

public void setSelected(boolean selected) {
this.selected.set(selected);
}

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

public StringProperty nameProperty() {
return name;
}

public void setName(String name) {
this.name.set(name);
}
}





share|improve this answer


























  • That's what I was looking for! Can you also guide what would be the best way to get some callback? Something like onEditCommit. I did it by implementing new Callback() {...}.call(Integer), but I dont think it's the best way. Maybe adding onEditCommit in fxml somehow?

    – Mateusz Gaweł
    Nov 18 '18 at 15:48











  • That's a bit too broad, @MateuszGaweł. It depends on what your needs are for the commit. If you simply want to update the selected property of the MyItem object, that is already done with the CheckBoxTableCell. You might want to ask a separate question regarding that.

    – Zephyr
    Nov 18 '18 at 15:49






  • 1





    Ok, understand. That example helped me a lot. Thank you very much:)

    – Mateusz Gaweł
    Nov 18 '18 at 15:51














1












1








1







I'm not sure about the rest of your implementation, but I assume you do not have your TableView set to editable:



tableView.setEditable(true);




On a side note, you could easily use a CheckBoxTableCell instead of implementing your own (BooleanCell).



Here is a very simple application you can run to see how this works. Each CheckBox may be clicked without first focusing the row and its value updates your underlying model as well (which you can see by clicking the "Print List" button).



import javafx.application.Application;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class CheckBoxTableViewSample extends Application {

public static void main(String args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {

// Simple interface
VBox root = new VBox(5);
root.setPadding(new Insets(10));
root.setAlignment(Pos.CENTER);

// List of sample items
ObservableList<MyItem> myItems = FXCollections.observableArrayList();
myItems.addAll(
new MyItem(false, "Item 1"),
new MyItem(false, "Item 2"),
new MyItem(true, "Item 3"),
new MyItem(false, "Item 4"),
new MyItem(false, "Item 5")
);

// Create TableView
TableView<MyItem> tableView = new TableView<MyItem>();

// We need the TableView to be editable in order to allow each CheckBox to be selectable
tableView.setEditable(true);

// Create our table Columns
TableColumn<MyItem, Boolean> colSelected = new TableColumn<>("Selected");
TableColumn<MyItem, String> colName = new TableColumn<>("Name");

// Bind the columns with our model's properties
colSelected.setCellValueFactory(f -> f.getValue().selectedProperty());
colName.setCellValueFactory(f -> f.getValue().nameProperty());

// Set the CellFactory to use a CheckBoxTableCell
colSelected.setCellFactory(param -> {
return new CheckBoxTableCell<MyItem, Boolean>();
});

// Add our columns to the TableView
tableView.getColumns().addAll(colSelected, colName);

// Set our items to the TableView
tableView.setItems(myItems);

// Create a button to print out our list of items
Button btnPrint = new Button("Print List");
btnPrint.setOnAction(event -> {
System.out.println("-------------");
for (MyItem item : myItems) {
System.out.println(item.getName() + " = " + item.isSelected());
}
});

root.getChildren().addAll(tableView, btnPrint);
// Show the Stage
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}

/**
* Just a simple sample class to display in our TableView
*/
final class MyItem {

// This property will be bound to the CheckBoxTableCell
private final BooleanProperty selected = new SimpleBooleanProperty();

// The name of our Item
private final StringProperty name = new SimpleStringProperty();

public MyItem(boolean selected, String name) {
this.selected.setValue(selected);
this.name.set(name);
}

public boolean isSelected() {
return selected.get();
}

public BooleanProperty selectedProperty() {
return selected;
}

public void setSelected(boolean selected) {
this.selected.set(selected);
}

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

public StringProperty nameProperty() {
return name;
}

public void setName(String name) {
this.name.set(name);
}
}





share|improve this answer















I'm not sure about the rest of your implementation, but I assume you do not have your TableView set to editable:



tableView.setEditable(true);




On a side note, you could easily use a CheckBoxTableCell instead of implementing your own (BooleanCell).



Here is a very simple application you can run to see how this works. Each CheckBox may be clicked without first focusing the row and its value updates your underlying model as well (which you can see by clicking the "Print List" button).



import javafx.application.Application;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class CheckBoxTableViewSample extends Application {

public static void main(String args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {

// Simple interface
VBox root = new VBox(5);
root.setPadding(new Insets(10));
root.setAlignment(Pos.CENTER);

// List of sample items
ObservableList<MyItem> myItems = FXCollections.observableArrayList();
myItems.addAll(
new MyItem(false, "Item 1"),
new MyItem(false, "Item 2"),
new MyItem(true, "Item 3"),
new MyItem(false, "Item 4"),
new MyItem(false, "Item 5")
);

// Create TableView
TableView<MyItem> tableView = new TableView<MyItem>();

// We need the TableView to be editable in order to allow each CheckBox to be selectable
tableView.setEditable(true);

// Create our table Columns
TableColumn<MyItem, Boolean> colSelected = new TableColumn<>("Selected");
TableColumn<MyItem, String> colName = new TableColumn<>("Name");

// Bind the columns with our model's properties
colSelected.setCellValueFactory(f -> f.getValue().selectedProperty());
colName.setCellValueFactory(f -> f.getValue().nameProperty());

// Set the CellFactory to use a CheckBoxTableCell
colSelected.setCellFactory(param -> {
return new CheckBoxTableCell<MyItem, Boolean>();
});

// Add our columns to the TableView
tableView.getColumns().addAll(colSelected, colName);

// Set our items to the TableView
tableView.setItems(myItems);

// Create a button to print out our list of items
Button btnPrint = new Button("Print List");
btnPrint.setOnAction(event -> {
System.out.println("-------------");
for (MyItem item : myItems) {
System.out.println(item.getName() + " = " + item.isSelected());
}
});

root.getChildren().addAll(tableView, btnPrint);
// Show the Stage
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}

/**
* Just a simple sample class to display in our TableView
*/
final class MyItem {

// This property will be bound to the CheckBoxTableCell
private final BooleanProperty selected = new SimpleBooleanProperty();

// The name of our Item
private final StringProperty name = new SimpleStringProperty();

public MyItem(boolean selected, String name) {
this.selected.setValue(selected);
this.name.set(name);
}

public boolean isSelected() {
return selected.get();
}

public BooleanProperty selectedProperty() {
return selected;
}

public void setSelected(boolean selected) {
this.selected.set(selected);
}

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

public StringProperty nameProperty() {
return name;
}

public void setName(String name) {
this.name.set(name);
}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 18 '18 at 15:32

























answered Nov 18 '18 at 15:25









ZephyrZephyr

3,6632931




3,6632931













  • That's what I was looking for! Can you also guide what would be the best way to get some callback? Something like onEditCommit. I did it by implementing new Callback() {...}.call(Integer), but I dont think it's the best way. Maybe adding onEditCommit in fxml somehow?

    – Mateusz Gaweł
    Nov 18 '18 at 15:48











  • That's a bit too broad, @MateuszGaweł. It depends on what your needs are for the commit. If you simply want to update the selected property of the MyItem object, that is already done with the CheckBoxTableCell. You might want to ask a separate question regarding that.

    – Zephyr
    Nov 18 '18 at 15:49






  • 1





    Ok, understand. That example helped me a lot. Thank you very much:)

    – Mateusz Gaweł
    Nov 18 '18 at 15:51



















  • That's what I was looking for! Can you also guide what would be the best way to get some callback? Something like onEditCommit. I did it by implementing new Callback() {...}.call(Integer), but I dont think it's the best way. Maybe adding onEditCommit in fxml somehow?

    – Mateusz Gaweł
    Nov 18 '18 at 15:48











  • That's a bit too broad, @MateuszGaweł. It depends on what your needs are for the commit. If you simply want to update the selected property of the MyItem object, that is already done with the CheckBoxTableCell. You might want to ask a separate question regarding that.

    – Zephyr
    Nov 18 '18 at 15:49






  • 1





    Ok, understand. That example helped me a lot. Thank you very much:)

    – Mateusz Gaweł
    Nov 18 '18 at 15:51

















That's what I was looking for! Can you also guide what would be the best way to get some callback? Something like onEditCommit. I did it by implementing new Callback() {...}.call(Integer), but I dont think it's the best way. Maybe adding onEditCommit in fxml somehow?

– Mateusz Gaweł
Nov 18 '18 at 15:48





That's what I was looking for! Can you also guide what would be the best way to get some callback? Something like onEditCommit. I did it by implementing new Callback() {...}.call(Integer), but I dont think it's the best way. Maybe adding onEditCommit in fxml somehow?

– Mateusz Gaweł
Nov 18 '18 at 15:48













That's a bit too broad, @MateuszGaweł. It depends on what your needs are for the commit. If you simply want to update the selected property of the MyItem object, that is already done with the CheckBoxTableCell. You might want to ask a separate question regarding that.

– Zephyr
Nov 18 '18 at 15:49





That's a bit too broad, @MateuszGaweł. It depends on what your needs are for the commit. If you simply want to update the selected property of the MyItem object, that is already done with the CheckBoxTableCell. You might want to ask a separate question regarding that.

– Zephyr
Nov 18 '18 at 15:49




1




1





Ok, understand. That example helped me a lot. Thank you very much:)

– Mateusz Gaweł
Nov 18 '18 at 15:51





Ok, understand. That example helped me a lot. Thank you very much:)

– Mateusz Gaweł
Nov 18 '18 at 15:51


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53361763%2fjavafx-tableview-checkbox-requires-focus%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Hercules Kyvelos

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud