I would to display data from an arrayList into a TableView but I have some problem with the mechanism of ''setCellValueFactory''. The code is this:
public class Example implements Comparable<Example>{
private List<Object> example=new ArrayList<Object>();
public void add(Object o){
example.add(o);
}
public Object get(int i){
return example.get(i);
}
public int compareTo(Example ex) {
int i=0;
for(Object o:ex.example){
if(!o.equals(this.example.get(i)))
return ((Comparable)o).compareTo(example.get(i));
i++;
}
return 0;
}
public String toString(){
String str="";
for(Object o:example)
str+=o.toString()+ " ";
return str;
}
}
public void start(Stage stage) throws SQLException, DatabaseConnectionException {
....
....
....
....
....
....
tab.setMinWidth(700);
tables.getSelectionModel().selectedItemProperty().addListener(
(ov,old_val,new_val)->{
tab.getColumns().clear();
try {
List<Example> data = new ArrayList<Example>();
TableData tableData = new TableData(new DbAccess((String)dataBases.getValue()));
data = tableData.getDistinctTransazioni(new_val);
TableSchema tableSchema = new TableSchema(new DbAccess((String)dataBases.getValue()),new_val);
for (int i=0;i<tableSchema.getNumberOfAttributes();i++) {
TableColumn column = new TableColumn(tableSchema.getColumn(i).getColumnName());
tab.getColumns().add(column);
}
ObservableList<Example> values = FXCollections.
observableArrayList(data);
tab.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
tab.setItems(values);
} catch (SQLException e1) {
e1.printStackTrace();
} catch (DatabaseConnectionException e1) {
e1.printStackTrace();
} catch (EmptySetException e1) {
e1.printStackTrace();
}
}
);
In the method : column.setCellValueFactory(new PropertyValueFactory<>("Here what I put?")).- I don't have a property but I have an arrayList of Object in class Example.
Example.getyou cannot do this withPropertyValueFactory. Instead you need to write your owncellValueFactory:final int index = i; TableColumn<Example, Object> column = new TableColumn<>(tableSchema.getColumn(i).getColumnName()); column.setCellValueFactory(cd -> new SimpleObjectProperty<Object>(cd.getValue().get(index)));...