我有这个问题:在tableview中我有一个列数来计算文件数。在这一刻我有3个档案...... 但是当我删除一行(即2')时,结果是: tableview error count 然后,当我添加另一个文件时,您可以看到它继续正确计数: Tableview correctly count 我怎样才能更新2'文件名? 谢谢大家
这是两个函数addFile和removeFile的代码:
public void addFileAudio(ActionEvent event) {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Seleziona il file da caricare");
//imposta i filtri nel FileChooser
fileChooser.getExtensionFilters().addAll(new ExtensionFilter("Audio Files", "*.wav", "*.mp3"));
File selectedFile = fileChooser.showOpenDialog(new Stage());
if (selectedFile != null) {
String fileName = selectedFile.getName();
//aggiunge nome del file e il conteggio nella tabella FX
int count = Table.getItems().size()+1;
Table.getItems().add(new PopulateTable(count, fileName, "DateToFind"));
}
}
public void removeFileAudio(ActionEvent event) {
Table.setEditable(true);
int selectedIndex = Table.getSelectionModel().getSelectedIndex();
if (selectedIndex >= 0) {
Table.getItems().remove(selectedIndex);
} else {
// Nothing selected.
Alert alert = new Alert(AlertType.WARNING);
alert.setTitle("No Selection");
alert.setHeaderText("Nessun file audio selezionato!");
alert.setContentText("Selezionare file audio in tabella");
alert.showAndWait();
}
}
这是我的代码,现在在Controller类中:
public void addFileAudio(ActionEvent event) {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Seleziona il file da caricare");
//imposta i filtri nel FileChooser
fileChooser.getExtensionFilters().addAll(new ExtensionFilter("Audio Files", "*.wav", "*.mp3"));
File selectedFile = fileChooser.showOpenDialog(new Stage());
if (selectedFile != null) {
String fileName = selectedFile.getName();
//aggiunge nome del file e il conteggio nella tabella FX
Table.getItems().add(new PopulateTable( fileName, "DataDaTrovare"));
}
count.setCellFactory(col -> new TableCell<PopulateTable, Void>() {
@Override
public void updateIndex(int index) {
super.updateIndex(index);
if (isEmpty() || index < 0) {
setText(null);
} else {
setText(Integer.toString(index));
}
}
});
Table.getColumns().add(count);
}
private <S, T> TableColumn<S, T> createColumn(String title,
Function<S, ObservableValue<T>> property, double width) {
TableColumn<S, T> col = new TableColumn<>(title);
col.setCellValueFactory(cellData -> property.apply(cellData.getValue()));
col.setPrefWidth(width);
return col;
}
}
在控制台中:
线程中的异常&#34; JavaFX应用程序线程&#34; java.lang.IllegalStateException:在TableView列列表中检测到的带有标题&#39;索引的重复TableColumns