samedi 2 décembre 2017

Javafx tableview add boolean value in loop

I'm trying to populate a tableview with a boolean value as false in a for loop for checkbox in the table. My goal is to load a csv file in to tableview with first column as "checkbox", so user can select rows for further task.

When i define the same data inside observableArrayList, it works.

public void addPredfined_Data_to_Tableview() {

    studentData_Predefined = FXCollections.observableArrayList(
            new StudentTableModel(false, "James", "Atlanta"),
            new StudentTableModel(false, "Karen", "New York"),
            new StudentTableModel(false, "Robert", "Texas")
            );
    tableView.setItems(studentData_Predefined);
}

The problem is when i try to update the tableview inside a for loop, it doesn't work. The error i get is IndexOutOfBoundsException: Index: 0, Size: 0

public void load_CSV_file_to_Tableview() {

    for (int i = 0; i < 3; i++) {
        studentData_fromCSV = FXCollections.observableArrayList(
                new StudentTableModel(Boolean.FALSE, "James", "Atlanta")
                );
    }
    tableView.setItems(studentData_fromCSV);
}

Here is my table model class.

package application;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;

public class StudentTableModel {

    private final BooleanProperty chekboxStudentRow;
    private final SimpleStringProperty studentName;
    private final SimpleStringProperty studentLocation;
    public StudentTableModel(Boolean chekboxStudentRow, String studentName, String studentLocation) {
        super();
        this.chekboxStudentRow = new SimpleBooleanProperty(chekboxStudentRow);
        this.studentName = new SimpleStringProperty(studentName);
        this.studentLocation = new SimpleStringProperty(studentLocation);
    }

    public String getStudentName() {
        return studentName.get();
    }

    public String getStudentLocation() {
        return studentLocation.get();
    }

    public final BooleanProperty chekboxStudentRowProperty() {
        return this.chekboxStudentRow;
    }

    public final boolean isChekboxStudentRow() {
        return this.chekboxStudentRowProperty().get();
    }

    public final void setChekboxStudentRow(final boolean chekboxStudentRow) {
        this.chekboxStudentRowProperty().set(chekboxStudentRow);
    }

}

And he is my controller class.

package application;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.util.Callback;

public class MainWindowController implements Initializable{

    @FXML public Button btn_addPredefined_data;
    @FXML public Button btn_load_csv_file;

    @FXML public TableView<StudentTableModel> tableView;
    @FXML public TableColumn<StudentTableModel, Boolean> col_CheckBox;
    @FXML public TableColumn<StudentTableModel, String> col_StudentName;
    @FXML public TableColumn<StudentTableModel, String> col_StudentLocation;

    public ObservableList<StudentTableModel> studentData_Predefined = FXCollections.observableArrayList();
    public ObservableList<StudentTableModel> studentData_fromCSV = FXCollections.observableArrayList();

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

        col_CheckBox.setCellFactory(CheckBoxTableCell.forTableColumn(new Callback<Integer, ObservableValue<Boolean>>() {
            @Override
            public ObservableValue<Boolean> call(Integer param) {
                // TODO Auto-generated method stub
                return studentData_Predefined.get(param).chekboxStudentRowProperty();
            }
        }));

        col_StudentName.setCellValueFactory(new PropertyValueFactory<StudentTableModel, String>("studentName"));
        col_StudentLocation.setCellValueFactory(new PropertyValueFactory<StudentTableModel, String>("studentLocation"));

        tableView.setEditable(true);

        btn_addPredefined_data.setOnAction(e -> addPredfined_Data_to_Tableview());
        btn_load_csv_file.setOnAction(e -> load_CSV_file_to_Tableview());

    }

    public void addPredfined_Data_to_Tableview() {

        studentData_Predefined = FXCollections.observableArrayList(
                new StudentTableModel(false, "James", "Atlanta"),
                new StudentTableModel(false, "Karen", "New York"),
                new StudentTableModel(false, "Robert", "Texas")
                );
        tableView.setItems(studentData_Predefined);
    }

    public void load_CSV_file_to_Tableview() {

        for (int i = 0; i < 3; i++) {
            studentData_fromCSV = FXCollections.observableArrayList(
                    new StudentTableModel(Boolean.FALSE, "James", "Atlanta")
                    );
        }
        tableView.setItems(studentData_fromCSV);
    }


}




Aucun commentaire:

Enregistrer un commentaire