我在该特定控制器中有一个tableView,我想用传递的参数值初始化。 问题是,在控制器将参数设置为变量之前,正在执行initialize方法。 任何建议都会很棒。
我正在引用Location参数只是为了清楚。
这是控制器的调用方法:
protected static void openReservationNewDialog(Location loc){
try{
FXMLLoader loader = new FXMLLoader();
loader.setLocation(viewLogic.class.getResource("openNewReservations.fxml"));
AnchorPane page = (AnchorPane) loader.load();
Stage dialogStage = new Stage();
dialogStage.setTitle("New Reservation");
dialogStage.setResizable(false);
Scene scene = new Scene(page);
dialogStage.setScene(scene);
openReservationNewDialogController controller=loader.getController();
controller.setDialogStage(dialogStage);
controller.setSelectedLocation(loc);
dialogStage.showAndWait();
} catch(Exception e) {
e.printStackTrace();
}
这是相关的控制器部分:
public class openReservationNewDialogController implements Initializable{
@FXML
private TextField reservationId;
/**Reservation's member*/
@FXML
private ComboBox<String> member=new ComboBox<String>();
@FXML
private ComboBox<String> sMinutes= new ComboBox<String>();
@FXML
private ComboBox<String> eMinutes= new ComboBox<String>();
@FXML
private ComboBox<String> sHours= new ComboBox<String>();
@FXML
private ComboBox<String> eHours= new ComboBox<String>();
private ArrayList<String> minutes=new ArrayList<>();
private ArrayList<String> hours= new ArrayList<String>();
@FXML
/**Reservation's pick'n'drop locations (the same)*/
private Location loc;
/**Reservation's start time*/
@FXML
private DatePicker startTimeAndDate;
/**Reservation's end time*/
@FXML
private DatePicker endTimeAndDate;
private Stage dialogStage;
@FXML
private ObservableList<Reservation> loca=FXCollections.observableArrayList();
@FXML
private TableView<Reservation> table;
@FXML
private TableColumn<Reservation, String> reservationID;
@FXML
private TableColumn<Reservation, String> startTimeAndDateRes;
@FXML
private TableColumn<Reservation, String> endTimeAndDateREs;
public void setSelectedLocation(Location selectedLocation){
this.loc=selectedLocation;
}
@FXML
private void handleCancel(){
dialogStage.close();
}
public void setDialogStage(Stage dialogStage) {
this.dialogStage=dialogStage;
}
@Override
public void initialize(URL location, ResourceBundle resources) {
reservationId.addEventFilter(KeyEvent.KEY_TYPED , TextFilters.numeric_Validation(4));
for(Member mem : viewLogic.sData.getMembers().values()){
member.getItems().add(mem.getMemIdp().get());
}
//set combo boxes for time
initializeMinutes();
sMinutes.getItems().addAll(minutes);
eMinutes.getItems().addAll(minutes);
initializeHours();
sHours.getItems().addAll(hours);
eHours.getItems().addAll(hours);
if(!loc.getReservations().isEmpty()){
loca.addAll((viewLogic.sData.getLocations().get(loc.getIdP().get()).getReservations().values()));
reservationID.setCellValueFactory(cellData -> cellData.getValue().getReservationIdp());
startTimeAndDateRes.setCellValueFactory(cellData -> cellData.getValue().getStartTimeAndDateP());
endTimeAndDateREs.setCellValueFactory(cellData -> cellData.getValue().getEndTimeAndDateP());
table.setItems(loca);}
}
答案 0 :(得分:5)
提前实例化控制器类,然后将其设置为FXMLLoader:
MyController mc = new MyController();
mc.setInitData(myData);
FXMLLoader loader = new FXMLLoader();
loader.setController(mc);
...
loader.load();