我的代码有问题。我想从Excel文件中读取。我有两个组合框。在第一个组合框中,选择一个国家。在第二个ComboBox中,仅应显示该国家/地区中可用的市场。我用If来做到这一点。但是,如果我更改国家/地区,则将再次读取Excel。我希望在程序启动时可以一次读取Excel。
我该怎么做? 我的想法是将代码放在Main中,但我不知道那是否可能。
控制器:
public class Controller implements Initializable {
@FXML
public ComboBox<Country> cbl;
@FXML
public ComboBox<Markt> cbm;
public static final String SAMPLE_XLSX_FILE_PATH = "C:\\Users\\msch05051\\Desktop\\Sonstiges\\VitracomShopFinder\\2018-07-19_Installationsplan_Kaufland.xlsx";
@Override
public void initialize(URL location, ResourceBundle resources) {
}
public void setData() throws IOException {
cbl.setItems(FXCollections.observableArrayList(
new Country("Rumänien", "RO"),
new Country("Bulgarien", "BG"),
new Country("Tschechien", "CZ"),
new Country("Slowakei", "SK"),
new Country("Polen", "PL"),
new Country("Kroatien", "HR")));
}
public void read() throws IOException {
// Creating a Workbook from an Excel file (.xls or .xlsx)
Workbook workbook = WorkbookFactory.create(new File(SAMPLE_XLSX_FILE_PATH));
// Retrieving the number of sheets in the Workbook
System.out.println("Workbook has " + workbook.getNumberOfSheets() + " Sheets : ");
// Getting the Sheet at index zero
Sheet sheet = workbook.getSheetAt(0);
// Create a DataFormatter to format and get each cell's value as String
DataFormatter dataFormatter = new DataFormatter();
// 3. Or you can use Java 8 forEach loop with lambda
ArrayList<Markt> markt = new ArrayList<>();
String a = cbl.getSelectionModel().getSelectedItem().getKürzel();
for (Row row : sheet) {
if ((dataFormatter.formatCellValue(row.getCell(49)).equals("produktiv") || dataFormatter.formatCellValue(row.getCell(49)).equals("Produktiv")) & a.equals(dataFormatter.formatCellValue(row.getCell(8)))) {
//markt.add(dataFormatter.formatCellValue(row.getCell(49)));
markt.add(new Markt(dataFormatter.formatCellValue(row.getCell(9)), dataFormatter.formatCellValue(row.getCell(49)), dataFormatter.formatCellValue(row.getCell(8)), dataFormatter.formatCellValue(row.getCell(0))));
}
}
cbm.setItems(FXCollections.observableArrayList(
markt
));
//read array
for (int i = 0;i<markt.size();i++){
System.out.println(markt.get(i));
}
}
主要:
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));
Parent root = loader.load();
Controller controller = loader.getController();
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
//Set Data to FXML through controller
controller.setData();
controller.cbl.setConverter(new StringConverter<Country>() {
@Override
public String toString(Country object) {
return object.getLand();
}
@Override
public Country fromString(String string) {
return null;
}
});
controller.cbm.setConverter(new StringConverter<Markt>() {
@Override
public String toString(Markt object) {
return object.getMarkt();
}
@Override
public Markt fromString(String string) {
return null;
}
});
}
public static void main(String[] args) {
launch(args);
}
}