未在tableview中显示数据Javafx

时间:2017-07-12 12:19:52

标签: javafx tableview fxml

这是我的Controller类

public class IWS5Controller  extends BaseScreenController implements Initializable{

    private Stage dialogStage;

    @FXML
    private TableView<Table1Model> table1;
    @FXML
    private TableView<Table2Model> table2;
    @FXML
    private TableView<Table3Model> table3;

    @FXML
    private TableColumn<Table1Model,String> col1;
    @FXML
    private TableColumn<Table1Model,String> col2;
    @FXML
    private TableColumn<Table2Model,Integer> col3;
    @FXML
    private TableColumn<Table2Model,Integer> col4;
    @FXML
    private TableColumn<Table3Model,Integer> col5;
    @FXML
    private TableColumn<Table3Model,Integer> col6;
    @FXML
    private TableColumn<Table3Model,Integer> col7;
    @FXML
    private TableColumn<Table3Model,String> col8;

    @FXML
    private Button closeBtn;
    @Autowired
    private ScreensContoller screenController;
    /*@Autowired
    private CheckBoxController checkboxController;*/

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        // TODO Auto-generated method stub
    }

    @FXML
    public void initialize(){
          PropertyValueFactory<Table1Model, String> first = 
                  new PropertyValueFactory<Table1Model, String>("synid");      

          PropertyValueFactory<Table1Model, String> second = 
                  new PropertyValueFactory<Table1Model, String>("help");       

          PropertyValueFactory<Table2Model, Integer> third = 
                  new PropertyValueFactory<Table2Model, Integer>("page");      

          PropertyValueFactory<Table2Model, Integer> fourth = 
                  new PropertyValueFactory<Table2Model, Integer>("line");      

          PropertyValueFactory<Table3Model, Integer> fifth = 
                  new PropertyValueFactory<Table3Model, Integer>("recordDate");    

          PropertyValueFactory<Table3Model, Integer> sixth = 
                  new PropertyValueFactory<Table3Model, Integer>("diagnosisDate");      

          PropertyValueFactory<Table3Model, Integer> seventh = 
                  new PropertyValueFactory<Table3Model, Integer>("age");       

          PropertyValueFactory<Table3Model, String> eigth = 
                  new PropertyValueFactory<Table3Model, String>("note");

          col1.setCellValueFactory(first);
          col2.setCellValueFactory(second);
          col3.setCellValueFactory(third);
          col4.setCellValueFactory(fourth);
          col5.setCellValueFactory(fifth);
          col6.setCellValueFactory(sixth);  
          col7.setCellValueFactory(seventh);
          col8.setCellValueFactory(eigth);

              //setting up the table data source
              Iws5Data data = new Iws5Data();
              ObservableList<Table1Model> table1Items = data.getData();
              ObservableList<Table2Model> table2Items = data.getData2();
              ObservableList<Table3Model> table3Items = data.getData3();
              table1.setItems( table1Items );
              table2.setItems( table2Items );
              table3.setItems( table3Items );

            closeBtn.setOnAction((ActionEvent event)->{
            dialogStage.close();
        });
    }
}

表1模型:

package com.iws.model;

import javafx.beans.property.FloatProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class Table1Model {

    private final SimpleStringProperty synid;
    private final SimpleStringProperty help;

    public Table1Model(String synid,String help){
        this.synid = new SimpleStringProperty(synid);
        this.help = new SimpleStringProperty(help);
    }

    public String getSynid() {
        return synid.get();
    }
    public void setSynid(String synid) {
        this.synid.set( synid );
    }

    public String getHelp() {
        return help.get();
    }
    public void setHelp(String help) {
        this.help.set( help );
    }


}

表2模型:

package com.iws.model;

import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;


public class Table2Model {

    private final SimpleIntegerProperty page;
    private final SimpleIntegerProperty line;

    public Table2Model(Integer page, Integer line) {
        this.page = new SimpleIntegerProperty(page);
        this.line = new SimpleIntegerProperty(line);
    }

    public Integer getPage() {
        return page.get();
    }

    public void setPage(Integer page) {
        this.page.set( page );
    }

    public Integer getLine() {
        return line.get();
    }
    public void setLine(Integer line) {
        this.line.set( line );
    }
}

表3模型:

package com.iws.model;

import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;

public class Table3Model {

    private final SimpleIntegerProperty recordDate;
    private final SimpleIntegerProperty diagnosisDate;
    private final SimpleIntegerProperty age;
    private final SimpleStringProperty note;

    public Table3Model(Integer recordDate, Integer diagnosisDate, Integer age, String note) {
        this.recordDate = new SimpleIntegerProperty(recordDate);
        this.diagnosisDate = new SimpleIntegerProperty(diagnosisDate);
        this.age = new SimpleIntegerProperty(age);
        this.note = new SimpleStringProperty(note);
    }

    public Integer getRecordDate() {
        return recordDate.get();
    }
    public void setRecordDate(Integer recordDate) {
        this.recordDate.set( recordDate );
    }

    public Integer getDiagnosisDate() {
        return diagnosisDate.get();
    }
    public void setDiagnosisDate(Integer diagnosisDate) {
        this.diagnosisDate.set( diagnosisDate );
    }

    public Integer getAge() {
        return age.get();
    }
    public void setAge(Integer age) {
        this.age.set( age );
    }
    public String getNote() {
        return note.get();
    }
    public void setNote(String note) {
        this.note.set( note );
    }
}

数据类(硬编码数据)

package com.iws.database;

import com.iws.model.Table1Model;
import com.iws.model.Table2Model;
import com.iws.model.Table3Model;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

public class Iws5Data {

    private final ObservableList<Table1Model> data1 = 
            FXCollections.observableArrayList();

    public ObservableList<Table1Model> getData(){
        return data1;
    }
    private final ObservableList<Table2Model> data2 = 
            FXCollections.observableArrayList();

    public ObservableList<Table2Model> getData2(){
        return data2;
    }

    private final ObservableList<Table3Model> data3 = 
            FXCollections.observableArrayList();

    public ObservableList<Table3Model> getData3(){
        return data3;
    }

    public Iws5Data(){
        data1.add(new Table1Model("Synid", "help"));
        data2.add(new Table2Model(3434, 2343));
        data3.add(new Table3Model(22, 543, 5, "note"));
    }

}

正确显示所有3个表,没有任何错误。但是没有显示数据。 如果还有其他方法可以将硬编码数据添加到tableview,那么这也是有帮助的。

0 个答案:

没有答案