Reputation: 201
Hello i trying create table view, with dynamically columns I want to add in first column all headers except first one and another cells can be a 0 value
It's give me a error " java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
I dont really understand how to definie setCellValueFactory
Ok
User adding Object in list, This list is set in columns header .
for(Object c : objectList){
TableColumn<List<String>, String> table1 = new TableColumn<>();
table1.setText(c.getName());
table1.setCellValueFactory(data -> {
List<String> rowValues = data.getValue();
String cellValue= rowValues.get(objectList.indexOf(c));
return new ReadOnlyStringWrapper(cellValue);
});
Now i trying add rows to table for example
I have table headers
|Row|Object1|Object2|
so i want to my table look like
|Row|Object1|Object2|
|Object1|0 |0 |
|Object2|0 |0 |
ObservableList<String> datal = FXCollections.observableArrayList();
for(Object a: objectList){
datal.clear();
int index = objectList.indexOf(a);
if(index > 0 ){
datal.add(a.getName());
}else{
datal.add(objectList.get(index+ 1).getName());
}
for(int i = index+ 1; i <objectList.size() ;i++){
datal.add("0");
}
tableview.getItems().add(datal);
But when i datal.clear() i get error java.lang.IndexOutOfBoundsException: Index: 1, Size: 1, when i dont use this function all rows look this same
Edited
It's look ok but i need another set of zeroes in table
|Objects|Object1|Object2|Object3|
|Object1|null|0|0|
|Object2|null|null|0|
|Object3|null|null|null|
Upvotes: 0
Views: 1680
Reputation: 82491
You're adding the same list over and over again as item. Probably you intend to create new lists inside the loop:
for(Object a: objectList){
ObservableList<String> datal = FXCollections.observableArrayList();
// datal.clear();
...
}
furthermore you assume there are at least objectList.size()
items in every list, which won't be the case, unless all elements in objectList
items are equal to the first one.
Therefore you need to therefore you need to check the size of the item
list in the cellValueFactory
:
table1.setCellValueFactory(data -> {
List<String> rowValues = data.getValue();
int index = objectList.indexOf(c);
return index >= 0 && index < rowValues.size()
? new SimpleStringProperty(rowValues.get(index)) // does just the same as ReadOnlyStringWrapper in this case
: null; // no value, if outside of valid index range
});
Otherwise you'll get those IndexOutOfBoundsException
s for some of the rows...
Upvotes: 1