我想将arrayList中的数据显示到TableView中,但是“ setCellValueFactory”机制存在一些问题。 代码是这样的:
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();
}
}
);
在方法中:column.setCellValueFactory(new PropertyValueFactory <>(“”我在这里放什么?“))。-我没有属性,但是在类Example中有Object的arrayList。