我在javafx中有一个表视图,我动态添加列,因为我不知道列的数量。我想要在单击按钮时删除tableView的所有行和列。我有下面的代码。我想在表中添加新数据之前删除它中的所有列。我尝试了很多我在网上找到但没有用的东西。感谢。
public void handleEventsClick(){
String tableName = (String) events.getSelectionModel().getSelectedItem();
ObservableList<ObservableList> transitionEvents = FXCollections.observableArrayList();
tableColumns = new TableColumn[traEv.size()];
int trEvSize = 0;
for(int i = 0; i < traEv.size();i++){
ArrayList<TransitionSubsequencePair> tep = traEv.get(i);
TableColumn tc = new TableColumn("TrEv:" + i);
tableColumns[i] = tc;
final int j = i;
tc.setCellValueFactory(new Callback<CellDataFeatures<ObservableList,String>,ObservableValue<String>>(){
public ObservableValue<String> call(CellDataFeatures<ObservableList, String> param) {
return new SimpleStringProperty(param.getValue().get(j).toString());
}
});
eventsTableView.getColumns().addAll(tc);
}
ObservableList<String> row = FXCollections.observableArrayList();
row.clear();
for(int i = 0; i < traEv.size();i++){
ArrayList<TransitionSubsequencePair> tep = traEv.get(i);
ArrayList<String> seenTables = new ArrayList<String>();
for(TransitionSubsequencePair tre: tep){
System.out.println(tre.getTable());
String lala = "";
if(tre.getTable().equals(tableName) && (!seenTables.contains(tre.getTable()) || seenTables.isEmpty())){
System.out.println(i);
lala += tre.getTransition() + ":" + tre.getSubsequence() + ",";
row.add(lala);
seenTables.add(tre.getTable());
}
System.out.println("--"+lala);
}
}
transitionEvents.add(row);
eventsTableView.setItems(transitionEvents); // finally add data to tableview
}
答案 0 :(得分:15)
删除所有数据:
eventsTableView.getItems().clear();
删除所有列:
eventsTableView.getColumns().clear();