Rav*_*ndu 8 java javafx javafx-2
我有一个表,它有一个带复选框的列.

在按钮上单击,我想找出哪些复选框被选中,哪些不是.到目前为止,我设法在表格中创建复选框.代码如下.
public class TTEs implements Initializable {
@FXML
private TableView<TestObject> tableReport;
@FXML
private TableColumn<TestObject, String> name;
@FXML
private TableColumn<TestObject, Boolean> checkbox;
@FXML
public void getValues() {
//the method will get what check boxes are checked (this part is the problem)
}
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
ObservableList<TestObject> data = FXCollections.observableArrayList();
data.add(new TestObject("Test 1", true));
data.add(new TestObject("Test 2", false));
tableReport.setItems(data);
name.setCellValueFactory(new PropertyValueFactory<TestObject, String>("name"));
checkbox.setCellValueFactory(new PropertyValueFactory<TestObject, Boolean>("checked"));
checkbox.setCellFactory(new Callback<TableColumn<TestObject, Boolean>,
TableCell<TestObject, Boolean>>() {
public TableCell<TestObject, Boolean> call(TableColumn<TestObject, Boolean> p) {
return new CheckBoxTableCell<TestObject, Boolean>();
}
});
}
//CheckBoxTableCell for creating a CheckBox in a table cell
public static class CheckBoxTableCell<S, T> extends TableCell<S, T> {
private final CheckBox checkBox;
private ObservableValue<T> ov;
public CheckBoxTableCell() {
this.checkBox = new CheckBox();
this.checkBox.setAlignment(Pos.CENTER);
setAlignment(Pos.CENTER);
setGraphic(checkBox);
}
@Override public void updateItem(T item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
setGraphic(checkBox);
if (ov instanceof BooleanProperty) {
checkBox.selectedProperty().unbindBidirectional((BooleanProperty) ov);
}
ov = getTableColumn().getCellObservableValue(getIndex());
if (ov instanceof BooleanProperty) {
checkBox.selectedProperty().bindBidirectional((BooleanProperty) ov);
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我调试时,我发现:
ov = getTableColumn().getCellObservableValue(getIndex());
if (ov instanceof BooleanProperty) {
checkBox.selectedProperty().bindBidirectional((BooleanProperty) ov);
}
Run Code Online (Sandbox Code Playgroud)
在上面的条件中,它永远不会进入if语句,这意味着它ov不是一个实例BooleanProperty.但是当我打印出来的时候ov,
System.out.println(ov.getClass().getName());
Run Code Online (Sandbox Code Playgroud)
它打印为
javafx.beans.property.ReadOnlyObjectWrapper
Run Code Online (Sandbox Code Playgroud)
ReadOnlyObjectWrapper是一个子类BooleanProperty,为什么instanceof检查不起作用?
got*_*ero 13
在Java 8上测试过.
只有4个简单的东西.
1)制作CheckBoxCellFactory类.放在你的项目中的某个地方.
public class CheckBoxCellFactory implements Callback {
@Override
public TableCell call(Object param) {
CheckBoxTableCell<Person,Boolean> checkBoxCell = new CheckBoxTableCell();
return checkBoxCell;
}
}
Run Code Online (Sandbox Code Playgroud)
2)你的模特课.比如人.
public static class Person {
private SimpleBooleanProperty checked = new SimpleBooleanProperty(false);
// other columns here
public SimpleBooleanProperty checkedProperty() {
return this.checked;
}
public java.lang.Boolean getChecked() {
return this.checkedProperty().get();
}
public void setChecked(final java.lang.Boolean checked) {
this.checkedProperty().set(checked);
}
// getter/setter for other columns
}
Run Code Online (Sandbox Code Playgroud)
3)在fxml文件中进行了修改.TableView - > TableColumn的部分将如下所示:
<TableColumn fx:id="checkBoxTableColumn" maxWidth="34.0" minWidth="26.0" prefWidth="34.0" resizable="false" sortable="false">
<cellValueFactory><PropertyValueFactory property="checked" /></cellValueFactory>
<cellFactory><partarch.fx.CheckBoxCellFactory /></cellFactory>
</TableColumn>
Run Code Online (Sandbox Code Playgroud)
4)如果您想使您的复选框可编辑
循环TableView检查绑定到每个的布尔值的数据模型CheckBox.
@FXML
public void getValues(){
ObservableList<TestObject> data = tableReport.getItems();
for (TestObject item : data){
//check the boolean value of each item to determine checkbox state
}
}
Run Code Online (Sandbox Code Playgroud)
package checkboxtablecelltest;
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.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
/**
*
* @author reegan
*/
public class CheckBoxTableCellTest extends Application {
@Override
public void start(Stage primaryStage) {
final TableView<Person> tableView = new TableView<Person>();
tableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
tableView.setItems(FXCollections.observableArrayList(
new Person("Robert", "Plant"),
new Person("Neil", "Young"),
new Person("Willie", "Nelson"),
new Person("Natalie", "Merchant")));
tableView.getItems().get(3).setVegetarian(true);
final TableColumn<Person, String> firstNameCol = new TableColumn<Person, String>("First Name");
final TableColumn<Person, String> lastNameCol = new TableColumn<Person, String>("Last Name");
final TableColumn<Person, Boolean> vegetarianCol = new TableColumn<Person, Boolean>("Vegetarian");
tableView.getColumns().addAll(firstNameCol, lastNameCol, vegetarianCol);
firstNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName"));
lastNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("lastName"));
vegetarianCol.setCellValueFactory(new PropertyValueFactory<Person, Boolean>("vegetarian"));
vegetarianCol.setCellFactory(CheckBoxTableCell.forTableColumn(vegetarianCol));
vegetarianCol.setEditable(true);
tableView.setEditable(true);
final BorderPane root = new BorderPane();
root.setCenter(tableView);
final HBox controls = new HBox(5);
final Button infoButton = new Button("Show details");
infoButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
for (Person p : tableView.getItems()) {
System.out.printf("%s %s (%svegetarian)%n", p.getFirstName(),
p.getLastName(), p.isVegetarian() ? "" : "not ");
System.out.println(tableView.getSelectionModel().getSelectedItems());
}
System.out.println();
}
});
controls.getChildren().add(infoButton);
root.setBottom(controls);
Scene scene = new Scene(root, 300, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
public static class Person {
private StringProperty firstName;
private StringProperty lastName;
private BooleanProperty vegetarian;
public Person(String firstName, String lastName) {
this.firstName = new SimpleStringProperty(firstName);
this.lastName = new SimpleStringProperty(lastName);
this.vegetarian = new SimpleBooleanProperty(false);
}
public String getFirstName() {
return firstName.get();
}
public String getLastName() {
return lastName.get();
}
public boolean isVegetarian() {
return vegetarian.get();
}
public void setFirstName(String firstName) {
this.firstName.set(firstName);
}
public void setLastName(String lastName) {
this.lastName.set(lastName);
}
public void setVegetarian(boolean vegetarian) {
this.vegetarian.set(vegetarian);
}
public StringProperty firstNameProperty() {
return firstName;
}
public StringProperty lastNameProperty() {
return lastName;
}
public BooleanProperty vegetarianProperty() {
return vegetarian;
}
}
}
Run Code Online (Sandbox Code Playgroud)
`