天天看点

JavaFX中TableView的使用(结合scene builder)

以下图运行效果为例:

JavaFX中TableView的使用(结合scene builder)

1.创建TableView和TableColumn

直接使用scene builder拖拽控件。

2.在控制器中编写数据填充类

class StudentInformation {
        private final StringProperty sno;
        private final StringProperty sname;
        private final StringProperty ssex;
        private final StringProperty sbirthday;
        private final StringProperty sscore;
        private final StringProperty classname;
        private final ObjectProperty edit;

        public StudentInformation() {
            this(null, null, null, null, null, null, null);
        }

        public StudentInformation(String sno, String sname, String ssex, String sbirthday, String sscore, String classname, Object edit) {
            this.sno = new SimpleStringProperty(sno);
            this.sname = new SimpleStringProperty(sname);
            this.ssex = new SimpleStringProperty(ssex);
            this.sbirthday = new SimpleStringProperty(sbirthday);
            this.sscore = new SimpleStringProperty(sscore);
            this.classname = new SimpleStringProperty(classname);
            this.edit = new SimpleObjectProperty(edit);
        }

        public String getSno() {
            return sno.get();
        }

        public void setSno(String sno) {
            this.sno.set(sno);
        }

        public StringProperty snoProperty() {
            return sno;
        }

        public String getSname() {
            return sname.get();
        }

        public void setSname(String sname) {
            this.sname.set(sname);
        }

        public StringProperty snameProperty() {
            return sname;
        }

        public String getSsex() {
            return ssex.get();
        }

        public void setSsex(String ssex) {
            this.ssex.set(ssex);
        }

        public StringProperty ssexProperty() {
            return ssex;
        }

        public String getSbirthday() {
            return sbirthday.get();
        }

        public void setSbirthday(String sbirthday) {
            this.sbirthday.set(sbirthday);
        }

        public StringProperty sbirthdayProperty() {
            return sbirthday;
        }

        public String getSscore() {
            return sscore.get();
        }

        public void setSscore(String sscore) {
            this.sscore.set(sscore);
        }

        public StringProperty sscoreProperty() {
            return sscore;
        }

        public String getClassname() {
            return classname.get();
        }

        public void setClassname(String classname) {
            this.classname.set(classname);
        }

        public StringProperty classnameProperty() {
            return classname;
        }

        public Object getEdit(){
            return edit.get();
        }

        public void setEdit(Object edit){
            this.edit.set(edit);
        }
        public ObjectProperty editProperty(){
            return edit;
        }
    }
           

3.在控制器中声明控件

@FXML
private TextField TF_name;
@FXML
private TextField TF_sex;
@FXML
private TextField TF_score;
@FXML
private TextField TF_sno;
@FXML
private TextField TF_birthday;
@FXML
private TextField TF_classno;
@FXML
private TableColumn<StudentInformation, String> tc_sno;
@FXML
private TableColumn<StudentInformation, String> tc_sname;
@FXML
private TableColumn<StudentInformation, String> tc_ssex;
@FXML
private TableColumn<StudentInformation, String> tc_sbirthday;
@FXML
private TableColumn<StudentInformation, String> tc_sscore;
@FXML
private TableColumn<StudentInformation, String> tc_classname;
@FXML
private TableColumn<StudentInformation,Object> edit;
@FXML
private TableColumn delete;
@FXML
private TableView<StudentInformation> tv_information;
private ObservableList<StudentInformation> data = FXCollections.observableArrayList();
           

4.在控制器中绑定控件

tc_sno.setCellValueFactory(cellData -> cellData.getValue().snoProperty());
tc_sname.setCellValueFactory(cellData -> cellData.getValue().snameProperty());
tc_ssex.setCellValueFactory(cellData -> cellData.getValue().ssexProperty());
tc_sbirthday.setCellValueFactory(cellData -> cellData.getValue().sbirthdayProperty());
tc_sscore.setCellValueFactory(cellData -> cellData.getValue().sscoreProperty());
tc_classname.setCellValueFactory(cellData -> cellData.getValue().classnameProperty());
edit.setCellValueFactory(cellData -> cellData.getValue().editProperty());
tv_information.setItems(data);
           

以下为完整代码的链接(截取自学生管理系统):

学生管理系统