1

I am developing a program for accounting attendance of students. I need a table with the first column - a list of students (TableColumn) and the next columns I generate dynamically (list of lectures). At the intersection of a row and column - ComboBox. I found how to make dynamic generation of of columns for one type (TableView>>), but not found a solution for my case. I had an idea to create an additional class

class Row{
    StringProperty audience;
    ObservableList<Attendence> lectures;
}

but do not understand how to implement it. How to solve this problem??

1 Answer 1

2

Your idea is basically the correct one:

public class Row{
    private final StringProperty audience = new SimpleStringProperty();
    private final List<ObjectProperty<Attendance>> lectures = new ArrayList<>();

    public Row(String audience, int numAttendances) {
        setAudience(audience);
        for (int i = 0 ; i < numAttendances ; i++) {
            lectures.add(new SimpleObjectProperty<>());
        }
    }

    public List<ObjectProperty<Attendance>> getLectures() {
        return lectures ;
    }

    public StringProperty audienceProperty() { 
        return audience ;
    }

    public final String getAudience() {
        return audienceProperty().get();
    }

    public final void setAudience(String audience) {
        audienceProperty().set(audience);
    }
}

Now you can set your table up as follows:

int numLectures = ... ;
TableView<Row> table = new TableView<>();
TableColumn<Row, String> audienceCol = new TableColumn<>("Audience");
audienceCol.setCellValueFactory(cellData -> cellData.getValue().audienceProperty());
table.getColumns().add(audienceCol);
for (int i = 0 ; i < numLectures ; i++) {
    TableColumn<Row, Attendance> col = new TableColumn<>("Attendance "+ (i+1));
    final int colIndex = i ;
    col.setCellValueFactory(cellData -> cellData.getValue().getLectures().get(colIndex));
    table.getColumns().add(col);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.