2

Have data from db in ResultSet - working good - line System.out.println(alarm.toString()); prints data OK.

Trying to put the data into TableView - something wrong - line System.out.println(data.toString()); - prints last line X NUMBER OF RECORDS, the same in TableView - only last line duplicated a lot.

public void setAlarmsTableView(ResultSet rs) throws SQLException {
    private ObservableList<AlarmType> data = FXCollections.observableArrayList();
    columnDeviceIndex.setCellValueFactory(new PropertyValueFactory<>("deviceIndex"));
    /* ... */
    columnAlarmTime.setCellValueFactory(new PropertyValueFactory<>("alarmTime"));

    AlarmType alarm = new AlarmType();
    while (rs.next()) {
        alarm.setDeviceIndex(rs.getInt(1));
        /* ... */
        alarm.setAlarmTime(rs.getTimestamp(8));
        alarmsTableView.getItems().add(alarm);
        data.add(alarm);
        System.out.println(alarm.toString());
    }
    alarmsTableView.setItems(data);

    System.out.println(data.toString());
}
0

2 Answers 2

1

You only ever create & modify a single AlarmType instance. You need to create one instance per ResultSet row however. Also I'm not sure adding the instance to both the items list of the TableView and the data list is neccessary.

// AlarmType alarm = new AlarmType();
while (rs.next()) {
    AlarmType alarm = new AlarmType(); // create new instance here
    alarm.setDeviceIndex(rs.getInt(1));
    /* ... */
    alarm.setAlarmTime(rs.getTimestamp(8));
    // alarmsTableView.getItems().add(alarm); // this shouldn't be necessary
    data.add(alarm);
    System.out.println(alarm.toString());
}
alarmsTableView.setItems(data);
Sign up to request clarification or add additional context in comments.

1 Comment

That's absolutely right, all AlarmType(s) in the Table point to the same object created at the beginning. Nice observation.
0

Ok, I finally did something like that.

    AlarmType alarm = new AlarmType();
    while (rs.next()) {
        alarm.setDeviceIndex(rs.getInt(1));
        /* ... */
        alarm.setAlarmTime(rs.getTimestamp(8));
        data.add(new AlarmType(alarm));
    }
    alarmsTableView.setItems(data);

The table is full of data, and I'm satisfied learned my fault. @fabian @Yahya Thanks a lot for help!

2 Comments

Why create a additional AlarmType to hold the data when you're creating a new one from the data anyway? This way your code uses 4 assignments instead of 2: 2 in the setters and 2 in the copy constructor. If you simply create & fill a new instance inside the loop body you only need the 2 assignments for the setters.
U mean while loop included only data.add(new AlarmType(rs.getInt(1), /*...*/ ,rs.getTimestamp(8)));? Yup! U re deadly right! I should have been refacroted code before creating the question

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.