0

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.

3
  • I am guessing it's similar to stackoverflow.com/questions/47885549/… Commented Aug 17, 2018 at 17:19
  • See my answer here: stackoverflow.com/questions/50670743/… Commented Aug 17, 2018 at 17:23
  • Assuming you want to display the result of Example.get you cannot do this with PropertyValueFactory. Instead you need to write your own cellValueFactory: final int index = i; TableColumn<Example, Object> column = new TableColumn<>(tableSchema.getColumn(i).getColumnName()); column.setCellValueFactory(cd -> new SimpleObjectProperty<Object>(cd.getValue().get(index)));... Commented Aug 17, 2018 at 18:51

0

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.