2015-07-21 321 views
1

我在該特定控制器中有一個tableView,我想用傳入參數的值進行初始化。 問題是,在控制器將參數設置爲變量之前,正在執行initialize方法。 任何建議都會很棒。使用初始化方法將參數傳遞給控制器​​

我指的是位置參數只是爲了清楚。

這裏是控制器調用方法:

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);} 

     } 

回答

4

實例化事先控制器類,然後將其設置爲FXMLLoader :

MyController mc = new MyController(); 
mc.setInitData(myData); 
FXMLLoader loader = new FXMLLoader(); 
loader.setController(mc); 
... 
loader.load(); 
相關問題