【发布时间】:2019-01-24 14:53:02
【问题描述】:
我有一个ArrayList,其中的数据是从 Excel 文件中读取的。我现在想在我的 JavaFX 应用程序中创建一个 TableView,它从已创建的 ArrayList 中填充。我有一个类,其中包含所有读取的地址数据,例如建筑物编号、街道名称等,但我似乎无法获取这些数据并将其放入表视图中,它只是空白。
这是我到目前为止生成的代码:
List<AddressDetails> addressList = ReadExcel.readExcel();
TableView<AddressDetails> table = new TableView<>();
ObservableList<AddressDetails> addresses = FXCollections.observableArrayList(addressList);
TableColumn<AddressDetails, String> buildNameCol
= new TableColumn<AddressDetails, String>("Building Name");
buildNameCol.setCellValueFactory(new PropertyValueFactory<AddressDetails, String>("buildName"));
TableColumn<AddressDetails, Double> buildNumCol
= new TableColumn<AddressDetails, Double>("Building Number");
buildNumCol.setCellValueFactory(new PropertyValueFactory<AddressDetails, Double>("buildNum"));
TableColumn<AddressDetails, String> streetCol
= new TableColumn<AddressDetails, String>("Street Name");
streetCol.setCellValueFactory(new PropertyValueFactory<AddressDetails, String>("streetName"));
TableColumn<AddressDetails, String> cityCol
= new TableColumn<AddressDetails, String>("City");
cityCol.setCellValueFactory(new PropertyValueFactory<AddressDetails, String>("city"));
TableColumn<AddressDetails, String> postCol
= new TableColumn<AddressDetails, String>("Postcode");
postCol.setCellValueFactory(new PropertyValueFactory<AddressDetails, String>("postCode"));
TableColumn<AddressDetails, String> countryCol
= new TableColumn<AddressDetails, String>("Country");
countryCol.setCellValueFactory(new PropertyValueFactory<AddressDetails, String>("country"));
table.getColumns().addAll(buildNameCol, buildNumCol, streetCol, cityCol, postCol, countryCol);
table.setItems(addresses);
这是我的AddressDetails班级:
public class AddressDetails {
private String buildName;
private double buildNum;
private String streetName;
private String city;
private String postCode;
private String country;
public AddressDetails() {
}
public String getBuildName() {
return buildName;
}
public void setBuildName(String buildName) {
this.buildName = buildName;
}
public double getBuildNum() {
return buildNum;
}
public void setBuildNum(double buildNum) {
this.buildNum = buildNum;
}
public String getStreetName() {
return streetName;
}
public void setStreetName(String streetName) {
this.streetName = streetName;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getPostCode() {
return postCode;
}
public void setPostCode(String postCode) {
this.postCode = postCode;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
这是将excel文件中的地址添加到ArrayList中的excel阅读器:
public class ReadExcel {
public static List<AddressDetails> readExcel() {
List<AddressDetails> addressList = null;
try {
Workbook workbook = WorkbookFactory.create(new FileInputStream(FileSelector.getSelectedFile()));
// Get the first sheet from the excel file
Sheet sheet = workbook.getSheetAt(0);
if (sheet != null) {
addressList = readExcelSheet(sheet);
}
} catch (EncryptedDocumentException | IOException | ParseException e) {
e.printStackTrace();
}
if (addressList == null)
addressList = Collections.emptyList();
return addressList;
}
private static List<AddressDetails> readExcelSheet(Sheet sheet) throws ParseException {
Iterator<Row> rowItr = sheet.iterator();
List<AddressDetails> addressList = new ArrayList<>();
// Iterate each row in the sheet
while (rowItr.hasNext()) {
AddressDetails address = new AddressDetails();
Row row = rowItr.next();
// First row is header so skip it
if (row.getRowNum() <= 2) {
continue;
}
Iterator<Cell> cellItr = row.cellIterator();
// Iterate each cell in a row
while (cellItr.hasNext()) {
Cell cell = cellItr.next();
int index = cell.getColumnIndex();
switch (index) {
case 0:
address.setBuildName((String) getValueFromCell(cell));
break;
case 1:
address.setBuildNum((double) getValueFromCell(cell));
break;
case 2:
address.setStreetName((String) getValueFromCell(cell));
break;
case 3:
address.setCity((String) getValueFromCell(cell));
break;
case 4:
address.setPostCode((String) getValueFromCell(cell));
break;
case 5:
address.setCountry((String) getValueFromCell(cell));
break;
}
}
addressList.add(address);
}
return addressList;
}
// Method to get cell value based on cell type
private static Object getValueFromCell(Cell cell) {
switch (cell.getCellType()) {
case STRING:
return cell.getStringCellValue();
case NUMERIC:
return cell.getNumericCellValue();
case BLANK:
return "";
default:
return "";
}
}
}
【问题讨论】:
-
AddressDetails addressList = new AddressDetails();我没有看到你向这个对象添加值。
-
@StephanHogenboom 抱歉,这是说 List
addressList = ReadExcel.readExcel();因为这是我通过读取 excel 文件创建数组列表的地方