package usersapp.controller;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TableColumn;
import javafx.util.Callback;
import usersapp.model.Person;
public class PersonUnpersonValueFactory implements Callback<TableColumn.CellDataFeatures<Person, CheckBox>, ObservableValue<CheckBox>> {
@Override
public ObservableValue<CheckBox> call(TableColumn.CellDataFeatures<Person, CheckBox> param) {
Person person = param.getValue();
CheckBox checkBox = new CheckBox();
checkBox.selectedProperty().setValue(person.isUnperson());
checkBox.selectedProperty().addListener((ov, old_val, new_val) -> {
person.setUnperson(new_val);
System.out.println(new_val);
});
return new SimpleObjectProperty<>(checkBox);
}
}
public class Person {
private Boolean unperson;
...
//unperson
public Boolean isUnperson() {
return this.unperson;
}
public void setUnperson(Boolean unperson){
this.unperson = unperson;
}
...
}
<?import usersapp.controller.PersonUnpersonValueFactory?>
...
<TableView fx:id="personTable" editable="true" layoutX="7.0" layoutY="53.0" prefHeight="285.0" prefWidth="378.0" tableMenuButtonVisible="true" AnchorPane.bottomAnchor="4.0" AnchorPane.leftAnchor="7.0" AnchorPane.rightAnchor="7.0" AnchorPane.topAnchor="53.0">
<columns>
<TableColumn prefWidth="50.0" style="-fx-alignment: CENTER;">
<cellValueFactory>
<PersonUnpersonValueFactory />
</cellValueFactory>
<graphic>
<CheckBox mnemonicParsing="false" />
</graphic>
</TableColumn>
...
</columns>
</TableView>
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class Main extends Application{
@Override
public void start(Stage primaryStage) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Tabel Test");
primaryStage.setScene(new Scene(root, 600, 400));
primaryStage.show();
}
public static void main (String[] args) {
launch();
}
}
package sample;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
public class Controller {
@FXML
private ResourceBundle resources;
@FXML
private URL location;
@FXML
private TableView<Names> tabel;
@FXML
private TableColumn<Names, Boolean> col1;
@FXML
private CheckBox checkBox;
@FXML
private TableColumn<Names, String> col2;
private static boolean chck;
@FXML
void checkBoxInitialize(ActionEvent event) {
chck = checkBox.isSelected() ? true : false;
for (Names n : data) {
n.setCheck(chck);
}
}
ObservableList<Names> data = FXCollections.observableArrayList();
@FXML
void initialize() {
col1.setCellValueFactory(new PropertyValueFactory<Names, Boolean>("check"));
col2.setCellValueFactory(new PropertyValueFactory<Names, String>("name"));
for (int i = 1; i < 10; i++) {
data.add(new Names("Name " + i));
}
tabel.setItems(data);
}
}
package sample;
import javafx.scene.control.CheckBox;
public class Names {
private CheckBox check;
private String name;
public CheckBox getCheck() {
return check;
}
public void setCheck(Boolean x) {
if(x) {
check.setSelected(true);
} else {
check.setSelected(false);
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Names(String name) {
check = new CheckBox();
this.name = name;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<TableView fx:id="tabel" editable="true" layoutX="32.0" layoutY="47.0" prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columns>
<TableColumn fx:id="col1" prefWidth="26.0" resizable="false">
<graphic>
<CheckBox fx:id="checkBox" mnemonicParsing="false" onAction="#checkBoxInitialize" prefHeight="17.0" prefWidth="23.0" />
</graphic>
</TableColumn>
<TableColumn fx:id="col2" minWidth="0.0" prefWidth="572.0" text="Имя" />
</columns>
</TableView>
</children>
</AnchorPane>