1

I want create dynamic table with TableView class.

I send to contractor the number of columns (int), columns name (String[]) and rows (Student), but I can't deal with that. How I need to define the TableColumn for each one of columns?

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;
}
}

I be glad for receiving your answer.

Edit: Student class:

 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()); 

    }

}   
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Ramz
  • 207
  • 1
  • 2
  • 15
  • Each row in the table represents a `Student` object. So each column has to define how to map a `Student` object into a value to be displayed in the cell. Right now all you know about the columns are a name and an index. What does your `Student` class look like and how is the column name and/or index going to be used to get a value from the `Student` to display in a cell? – James_D Mar 04 '15 at 12:59
  • @James_D I added the Student class. – Ramz Mar 04 '15 at 13:24
  • OK, fine, but you also need to answer my second question: what is going to be displayed in a given cell? Right now all you know about that cell is: A `Student` object representing the row the cell is in. A `String` representing the column header text. The index of the column. You need to get from that information to a value for the cell. – James_D Mar 04 '15 at 13:31
  • @James_D How do I define the mapping? by 'get' function on Student Class? – Ramz Mar 04 '15 at 16:18

1 Answers1

1

First, you need to add property accessor methods to your Student class:

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
}

Then your cell value factory will look something like this:

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

Where you replace xxxProperty() with the actual property whose value you want to display in that column.

James_D
  • 201,275
  • 16
  • 291
  • 322
  • Thank you. And what about "tableView.getColumns().addAll(col)"?. Because I want to be a dynamic table, 'cell value factory' is in for loop, and I created 'get' function that get index of column and return StringProperty of him. – Ramz Mar 04 '15 at 17:48
  • Sorry, I have no clue what you mean. – James_D Mar 04 '15 at 17:53
  • I need to use "addAll(col)", for adding the columns? – Ramz Mar 04 '15 at 18:06
  • Yes, you still need that line. Since you're only adding one at a time, you can just do `add(...)` instead of `addAll(...)`. – James_D Mar 04 '15 at 18:09
  • I got this exeption: "Exception in Application start method java.lang.NullPointerException at javaNetFinal.DaynamicTable.buildTable(DynamicTable.java:40)" :( – Ramz Mar 04 '15 at 19:12
  • That doesn't really have anything to do with your original question. In general you should post new questions as new questions, though in this case it would simply be marked as a duplicate of http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it – James_D Mar 04 '15 at 19:14