實現(xiàn)了TableView中使用CheckBox可點擊,并可以獲取值
直接來Demo 兩個類
application.model 包下 Person2
//========================
package application.model;
import javafx.scene.control.CheckBox;
//The Data Model
public class Person2 {
private CheckBox checkBox = new CheckBox();
/*
* Constructors
*/
public Person2(boolean checkbox) {
checkBox.setSelected(checkbox);
}
public Person2() {
this(false);
}
public CheckBox getCheckBox() {
return checkBox;
}
public void setCheckBox(CheckBox checkBox) {
this.checkBox = checkBox;
}
}
//===================
application 包下main
//=============================================
package application;
import java.util.ArrayList;
import java.util.List;
import application.model.Person2;
import javafx.application.Application;
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.CheckBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
VBox root;
List<Person2> personList;
@Override
public void start(Stage primaryStage) {
initView();
try {
Scene scene = new Scene(root,400,400);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
//這里面是源代碼
private void initView() {
personList = new ArrayList<Person2>();
personList.add( new Person2(true));
personList.add( new Person2(false));
TableView<Person2> tblView = new TableView<Person2>();
tblView.setItems(FXCollections.observableList(personList) );
TableColumn<Person2,CheckBox> registered_col = new TableColumn<Person2,CheckBox>("Registered");
registered_col.setCellValueFactory(new PropertyValueFactory<Person2,CheckBox>("checkBox"));
tblView.getColumns().addAll(registered_col);
tblView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
Button button = new Button(" 進(jìn)行數(shù)據(jù)獲取");
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
sysoData();
}
});
root = new VBox();
root.getChildren().addAll(tblView,button);
}
public void sysoData() {
for (Person2 person : personList) {
boolean registered = person.getCheckBox().isSelected();
System.out.print(registered);
}
System.out.println();
}
}
//============================================