【问题标题】:Javafx dynamic TableJavafx 动态表
【发布时间】:2015-05-05 11:22:49
【问题描述】:

我想用 TableView 类创建动态表。

我将列数 (int)、列名 (String[]) 和行数 (Student) 发送给承包商,但我无法处理。 我需要如何为每一列定义 TableColumn?

public class DynamicTable extends TableView<Student>{

private ObservableList<Student> data;
private int columnCount;
private String[] columnName;
private TableView<Student> tableView;

DynamicTable(){

}
DynamicTable(int columnCount, Student[] rows, String[] columnName){

    this.columnName=columnName;
    this.columnCount=columnCount;
    data = FXCollections.observableArrayList();
    setData(rows);
}

public void buildTable(){

    for(int i=0 ; i<columnCount; i++){
        final int j=i;
        TableColumn<Student,String> col = new TableColumn<Student,String>(columnName[i]); 

        //col.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Student,String>, ObservableValue<String>>() {

            //@Override
            //public ObservableValue<String> call(
                //  CellDataFeatures<Student, String> param) {

            //  return new SimpleStringProperty(param.getValue().getAddress());
        //  }
        //});
        tableView.getColumns().addAll(col);

        tableView.setItems(data);
    }
}

public void setData(Student[] rows){
    data.setAll(rows);

}
public String[] getColumnName(){
    return this.columnName;
}
}

很高兴收到您的答复。

编辑:学生班级:

 public class Student implements  Externalizable
    { 
    private final SimpleIntegerProperty ID;
    private final SimpleStringProperty firstName;
    private final SimpleStringProperty lastName;
    private final SimpleStringProperty address;
    private final SimpleObjectProperty<Date> birthDate;
    private final SimpleStringProperty department;
    private final SimpleIntegerProperty pointsAmount;
    private final SimpleObjectProperty<Date> startStudyingDate;
    private final SimpleIntegerProperty failedAmount;
    private final SimpleDoubleProperty average;
    private final SimpleIntegerProperty lavelByGrade;
    private final SimpleStringProperty pic;

    public Student(){

        this.ID = new SimpleIntegerProperty();
        this.firstName= new SimpleStringProperty();
        this.lastName= new SimpleStringProperty();
        this.address= new SimpleStringProperty();
        this.birthDate= new SimpleObjectProperty<Date>();
        this.department= new SimpleStringProperty();
        this.pointsAmount= new SimpleIntegerProperty();
        this.startStudyingDate= new  SimpleObjectProperty<Date>();
        this.failedAmount= new SimpleIntegerProperty();
        this.average= new SimpleDoubleProperty();
        this.lavelByGrade= new SimpleIntegerProperty();
        this.pic = new SimpleStringProperty();
    }

    public Student(int ID, String firstName, String lastName, String address,
            Date  birthDate, String department,
            int pointsAmount, Date startStudyingDate, int failedAmount, 
            double average, int  lavelByGrade, String pic){

        this.ID= new SimpleIntegerProperty(ID);
        this.firstName= new SimpleStringProperty(firstName);
        this.lastName= new SimpleStringProperty(lastName);
        this.address= new SimpleStringProperty(address);
        this.birthDate= new SimpleObjectProperty<Date>(birthDate);
        this.department= new SimpleStringProperty(department);
        this.pointsAmount= new SimpleIntegerProperty(pointsAmount);
        this.startStudyingDate= new  SimpleObjectProperty<Date>(startStudyingDate);
        this.failedAmount= new SimpleIntegerProperty(failedAmount);
        this.average= new SimpleDoubleProperty(average);
        this.lavelByGrade= new SimpleIntegerProperty(lavelByGrade);
        this.pic = new SimpleStringProperty(pic);
    }

    public int getID() {
        return ID.get();
    }
    public void setID(int ID) {
        this.ID.set(ID);
    }

    public String getFirstName() {
        return firstName.get();
    }

    public void setFirstName(String firstName) {
        this.firstName.set(firstName);
    }

    public String getLastName() {
        return lastName.get();
    }

    public void setLastName(String lastName) {
        this.lastName.set(lastName);
    }

    public String getAddress() {
        return address.get();
    }

    public void setAddress(String address) {
        this.address.set(address);
    }

    public Date getBirthDate() {
        return birthDate.get();
    }

    public void setBirthDate(Date birthDate) {
        this.birthDate.set(birthDate);
    }

    public String getDepartment() {
        return department.get();
    }

    public void setDepartment(String department) {
        this.department.set(department);
    }

    public int getPointsAmount() {
        return pointsAmount.get();
    }

    public void setPointsAmount(int pointsAmount) {
        this.pointsAmount.set(pointsAmount);
    }

    public Date getStartStudyingDate() {
        return startStudyingDate.get();
    }

    public void setStartStudyingDate(Date startStudyingDate) {
        this.startStudyingDate.set(startStudyingDate);
    }

    public int getFailedAmount() {
        return failedAmount.get();
    }

    public void setFailedAmount(int failedAmount) {
        this.failedAmount.set(failedAmount);
    }

    public double getAverage() {
        return average.get();
    }

    public void setAverage(Double average) {
        this.average.set(average);
    }

    public int getLavelByGrade() {
        return lavelByGrade.get();
    }

    public void setLavelByGrade(int lavelByGrade) {
        this.lavelByGrade.set(lavelByGrade);
    }

    public String getPic() {
        return pic.get();
    }

    public void setPic(String pic) {
        this.pic.set(pic);
    }

    @Override
    public void readExternal(ObjectInput in) throws IOException,
    ClassNotFoundException {

        setID(in.readInt());
        setFirstName((String)in.readObject());
        setLastName((String)in.readObject());
        setAddress((String)in.readObject());
        setBirthDate((Date)in.readObject());
        setDepartment((String)in.readObject());
        setPointsAmount(in.readInt());
        setStartStudyingDate((Date)in.readObject());
        setFailedAmount(in.readInt());
        setAverage(in.readDouble());
        setLavelByGrade(in.readInt());
        setPic((String)in.readObject());
    }

    @Override
    public void writeExternal(ObjectOutput out) throws IOException {

        out.writeInt(getID());
        out.writeObject(getFirstName()); 
        out.writeObject(getLastName()); 
        out.writeObject(getAddress()); 
        out.writeObject(getBirthDate());
        out.writeObject(getDepartment());
        out.writeInt(getPointsAmount()); 
        out.writeObject(getStartStudyingDate()); 
        out.writeInt(getFailedAmount()); 
        out.writeDouble(getAverage()); 
        out.writeInt(getLavelByGrade());
        out.writeObject(getPic()); 

    }

}   

【问题讨论】:

  • 表格中的每一行代表一个Student 对象。所以每一列都必须定义如何将Student 对象映射到要在单元格中显示的值。现在,您所知道的关于列的所有信息都是名称和索引。您的 Student 类是什么样的?列名和/或索引将如何用于从 Student 获取值以显示在单元格中?
  • @James_D 我添加了 Student 类。
  • 好的,很好,但是您还需要回答我的第二个问题:给定单元格中将显示什么?现在,您对该单元格的了解只有:Student 对象表示单元格所在的行。String 表示列标题文本。列的索引。您需要从该信息中获取单元格的值。
  • @James_D 如何定义映射?通过学生课堂上的“获取”功能?

标签: dynamic javafx tableview


【解决方案1】:

首先,您需要将属性访问器方法添加到您的 Student 类中:

public class Student {

    private final SimpleStringProperty firstName ;
    // etc ...

    public String getFirstName() {
        return firstName.get();
    }

    public void setFirstName(String firstName) {
        this.firstName.set(firstName);
    }

    // Add methods like this:

    public StringProperty firstNameProperty() {
        return firstName ;
    }

    // ... etc
}

那么您的单元格值工厂将如下所示:

TableColumn<Student,String> col = new TableColumn<Student,String>(columnName[i]);
col.setCellValueFactory(cellData -> {
    Student student = cellData.getValue();
    return student.xxxProperty();
});

xxxProperty() 替换为要在该列中显示其值的实际属性。

【讨论】:

  • 谢谢。那么“tableView.getColumns().addAll(col)”呢?因为我想成为一个动态表,'cell value factory'在for循环中,我创建了'get'函数来获取列的索引并返回他的StringProperty。
  • 对不起,我不明白你的意思。
  • 我需要使用“addAll(col)”来添加列吗?
  • 是的,你仍然需要那条线。由于您一次只添加一个,因此您可以使用 add(...) 而不是 addAll(...)
  • 我得到了这个异常:“javaNetFinal.DaynamicTable.buildTable(DynamicTable.java:40) 的应用程序启动方法 java.lang.NullPointerException 中的异常”:(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-23
  • 2013-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多