今天这是一个演示,用于显示来自 DAT 文件的 CSV 数据,而无需在 JavaFX 2.0 中的 tableView 上创建自定义类 。我称这个 TableView 为动态 TableView,因为 tableview 自动管理列和行。
在我对 tableView 上可编辑的研究中,我们必须有一个自定义类并将其实现到 tableView 以显示为这个演示 ==> http://docs.oracle.com/javafx/2/ui_controls/table-view.htm
但在这种情况下我不能这样做,因为我们不知道有多少列示例与 csv 文件或 .dat 文件......我想在这个 tableView 上做 可编辑在这种情况下 通过将 TextField 添加到 TableCell 中。如果没有自定义类怎么办(因为你没有多少列......),如果 它必须创建自定义类 那么 那么如何为这种情况设计自定义类 ?
请你帮助我好吗?
private void getDataDetailWithDynamic() {
tblView.getItems().clear();
tblView.getColumns().clear();
tblView.setPlaceholder(new Label("Loading..."));
// @Override
try {
File aFile = new File(txtFilePath.getText());
InputStream is = new BufferedInputStream(new FileInputStream(aFile));
Reader reader = new InputStreamReader(is, "UTF-8");
BufferedReader in = new BufferedReader(reader);
final String headerLine = in.readLine();
final String[] headerValues = headerLine.split("\t");
for (int column = 0; column < headerValues.length; column++) {
tblView.getColumns().add(
createColumn(column, headerValues[column]));
}
// Data:
String dataLine;
while ((dataLine = in.readLine()) != null) {
final String[] dataValues = dataLine.split("\t");
// Add additional columns if necessary:
for (int columnIndex = tblView.getColumns().size(); columnIndex < dataValues.length; columnIndex++) {
tblView.getColumns().add(createColumn(columnIndex, ""));
}
// Add data to table:
ObservableList<StringProperty> data = FXCollections
.observableArrayList();
for (String value : dataValues) {
data.add(new SimpleStringProperty(value));
}
tblView.getItems().add(data);
}
} catch (Exception ex) {
System.out.println("ex: " + ex.toString());
}
for(int i=0; i<tblView.getColumns().size(); i++) {
TableColumn col = (TableColumn)tblView.getColumns().get(i);
col.setPrefWidth(70);
}
}
private TableColumn createColumn(
final int columnIndex, String columnTitle) {
TableColumn column = new TableColumn(DefaultVars.BLANK_CHARACTER);
String title;
if (columnTitle == null || columnTitle.trim().length() == 0) {
title = "Column " + (columnIndex + 1);
} else {
title = columnTitle;
}
Callback<TableColumn, TableCell> cellFactory = new Callback<TableColumn, TableCell>() {
@Override
public TableCell call(TableColumn p) {
System.out.println("event cell");
EditingCellData cellExtend = new EditingCellData();
return cellExtend;
}
};
column.setText(title);
column.setCellValueFactory(cellFactory);
return column;
}
感谢您的阅读。
最佳答案
这是解决它的最佳方法 ==> https://forums.oracle.com/message/11216643#11216643
我真的很感谢你阅读这方面的内容。
谢谢
关于javafx-2 - 如何在 JAVAFX 中使用带有动态列的动态 TableView 编辑数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19142896/