2014-09-22 58 views
0

這是我在JAVAFX中的第一個項目,這是一個簡單的程序。我的問題是,當程序啓動時,FXMLLoadLogController的按鈕「loadLogButton」將收取正確的消息。但是當我在組合框中選擇另一種語言時,按鈕的設置器工作正常,但視圖不刷新。有什麼問題?查看不刷新按鈕 - Java FX

RPV:LoadLogController的函數chargeI18nValues可以正常工作,但從SolverManager調用它時,程序中不會刷新按鈕文本。

代碼:

SolverAssistant.java

public class SolverAssistant extends Application { 

    public static ResourceBundle messages; 
    public static Utils utils = new Utils(); 
    public static SolverManager manager; 

    @Override 
    public void start(Stage stage) throws Exception { 
     // Load the resource bundle 
     this.chargeResourceBundleLanguage(); 

     // Load Controllers 
     Scene scene = new Scene(new StackPane()); 
     manager = new SolverManager(scene); 
     manager.showMainView(); 
     stage.setScene(scene); 
     stage.show(); 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     launch(args); 
    } 

    public void chargeResourceBundleLanguage() { 
     String defaultLanguage = utils.fileReader(new File("lang.txt")); 
     switch (defaultLanguage) { 
      case "en": 
       messages = ResourceBundle.getBundle("bundles.bundle", Locale.forLanguageTag("en")); 
       break; 
      case "es": 
       messages = ResourceBundle.getBundle("bundles.bundle", Locale.forLanguageTag("es")); 
       break; 
      case "cat": 
       messages = ResourceBundle.getBundle("bundles.bundle", Locale.forLanguageTag("cat")); 
       break; 
      default: 
       messages = ResourceBundle.getBundle("bundles.bundle", Locale.forLanguageTag("en")); 
       break; 
     } 
    } 
} 

SolverManager.java

public class SolverManager { 

    private final Scene scene; 
    private FXMLMainController mainController; 
    private FXMLLoadLogController logController; 

    public SolverManager(Scene scene) { 
     this.scene = scene; 
    } 

    public void showMainView() throws IOException { 
     FXMLLoader mainLoader = new FXMLLoader(getClass().getResource("FXMLMain.fxml")); 
     scene.setRoot((Parent) mainLoader.load()); 
     mainController = mainLoader.<FXMLMainController>getController(); 
     FXMLLoader loadLoader = new FXMLLoader(getClass().getResource("FXMLLoadLog.fxml")); 
     loadLoader.load(); 
     logController = loadLoader.<FXMLLoadLogController>getController(); 
    } 

    public void refreshI18nResources() { 
     mainController.chargeI18nValues(); 
     logController.chargeI18nValues(); 
    } 
} 

FXMLMainController.java

public class FXMLMainController implements Initializable { 

    @FXML 
    private ComboBox<String> comboLanguage; 

    @FXML 
    private Tab loadTab; 

    @FXML 
    private Tab editTab; 

    @Override 
    public void initialize(URL url, ResourceBundle rb) { 
     this.chargeI18nValues(); 
     this.chargeLanguageComboBox(SolverAssistant.messages.getLocale()); 
     comboLanguage.getSelectionModel().selectedItemProperty().addListener(this.languageComboBoxListener()); 
    } 

    public void chargeI18nValues() { 
     loadTab.setText(SolverAssistant.messages.getString("LoadLog")); 
     editTab.setText(SolverAssistant.messages.getString("EditLog")); 
    } 

    private void chargeLanguageComboBox(Locale language) { 
     comboLanguage.getItems().addAll(
       "Català", 
       "English", 
       "Español" 
     ); 
     switch (language.getLanguage()) { 
      case "cat": 
       comboLanguage.setValue("Català"); 
       break; 
      case "es": 
       comboLanguage.setValue("Español"); 
       break; 
      case "en": 
       comboLanguage.setValue("English"); 
       break; 
     } 
    } 

    // -------- Listeners 
    private ChangeListener languageComboBoxListener() { 
     return new ChangeListener() { 
      public void stateChanged(ChangeEvent changeEvent) { 
      } 

      @Override 
      public void changed(ObservableValue observable, Object oldValue, Object newValue) { 
       switch ((String) newValue) { 
        case "Català": 
         SolverAssistant.messages = ResourceBundle.getBundle("bundles.bundle", Locale.forLanguageTag("cat")); 
         SolverAssistant.utils.fileWriter("lang.txt", "cat"); 
         break; 
        case "Español": 
         SolverAssistant.messages = ResourceBundle.getBundle("bundles.bundle", Locale.forLanguageTag("es")); 
         SolverAssistant.utils.fileWriter("lang.txt", "es"); 
         break; 
        case "English": 
         SolverAssistant.messages = ResourceBundle.getBundle("bundles.bundle", Locale.forLanguageTag("en")); 
         SolverAssistant.utils.fileWriter("lang.txt", "en"); 
         break; 
       } 
       SolverAssistant.manager.refreshI18nResources(); 
      } 
     }; 
    } 

} 

FXMLLoadLogController.java

public class FXMLLoadLogController implements Initializable { 

    @FXML 
    private Label logNameLabel; 

    @FXML 
    private TextArea logTextArea; 

    @FXML 
    private Button loadLogButton; 

    //private String logName; 
    private final String logName = "C:\\Users\\Daniel\\Desktop\\ahmaxsat-ls-ms_crafted-COMPLETE-1800-3500-2.log"; 
    private String log; 

    @Override 
    public void initialize(URL url, ResourceBundle rb) { 
     this.chargeI18nValues(); 
    } 

    public void chargeI18nValues() { 
     loadLogButton.setText(SolverAssistant.messages.getString("OpenNewLog")); 
    } 

    // -------- Actions 
    @FXML 
    private void openLog(ActionEvent event) { 
     JFileChooser fileChooser = new JFileChooser(); 
     fileChooser.setDialogTitle(SolverAssistant.messages.getString("Open")); 
     fileChooser.setCurrentDirectory(new File(".")); 
     // Selecting File 
     //if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { 
     if (true) { 
      //File file = fileChooser.getSelectedFile(); 
      log = SolverAssistant.utils.fileReader(new File(logName)); 
      logNameLabel.setText(logName); 
      logTextArea.setText(log); 
     } 
    } 
} 
+0

檢查'chargeI18nValues()'方法中的'SolverAssistant.messages.getString(...)'是否返回帶有正確語言的正確字符串,用於更改組合框項目。 – 2014-09-23 05:02:26

+0

是的它是正確的,如果我把一個System.out.println(loadLogButton.getText());在設置後的LoadLogController的chargeI18nValues函數中,向我顯示該按鈕可以更改文本,但它不會顯示在視圖上。這就像視圖不刷新... – 2014-09-23 08:44:51

+0

哪個版本的API和操作系統?嘗試創建新的演示項目,您可以在單擊按鈕時將標籤的文本設置爲任意值。通過這樣做,將確定特定的prbolem環境或特定的代碼。 – 2014-09-23 09:15:23

回答

0

問題是,有2個子/子FXML(及其控制器)的實例。它們是:使用來自SolverManager.showMainView()加載
1)經由<fx:include>小號

場景從FXMLMain.fxml加載FXMLLoader
2)含有裝載的那些經由包括但文本的變化在其他的情況發生。要解決此問題,您需要選擇其中一種方法,或者刪除包含或刪除fxmlloadings。下面是第二種方法:

public class SolverManager { 

    private final Scene scene; 
    private FXMLMainController mainController; 
// private FXMLLoadLogController loadLogController; 
// private FXMLEditLogController editLogController; 

    public SolverManager(Scene scene) { 
     this.scene = scene; 
    } 

    public void showMainView() throws IOException { 
     FXMLLoader mainLoader = new FXMLLoader(getClass().getResource("FXMLMain.fxml")); 
     scene.setRoot((Parent) mainLoader.load()); 
     mainController = mainLoader.<FXMLMainController>getController(); 

//  FXMLLoader loadLoader = new FXMLLoader(getClass().getResource("FXMLLoadLog.fxml")); 
//  loadLoader.load(); 
//  loadLogController = loadLoader.<FXMLLoadLogController>getController(); 
//  FXMLLoader editLoader = new FXMLLoader(getClass().getResource("FXMLEditLog.fxml")); 
//  editLoader.load(); 
//  editLogController = editLoader.<FXMLEditLogController>getController(); 
    } 

    public void refreshI18nResources() { 
     mainController.chargeI18nValues(); 
//  loadLogController.chargeI18nValues(); 
//  editLogController.chargeI18nValues(); 
    } 
} 

添加下列行來FXMLMainController:(變量名來自fx:id + 控制器參見Subcontroller not being injected into main controller

@FXML 
private FXMLLoadLogController barTabPageLoadController; 

@FXML 
private FXMLEditLogController barTabPageEditController; 

並更改以下在同一個文件中

public void chargeI18nValues() { 
    loadTab.setText(SolverAssistant.messages.getString("LoadLog")); 
    editTab.setText(SolverAssistant.messages.getString("EditLog")); 
    barTabPageLoadController.chargeI18nValues(); 
    barTabPageEditController.chargeI18nValues(); 
} 
+0

這很有道理,這是我的第一個項目,我沒有明確的概念,今天下午我會試一試,但似乎真的很好的解決方案,非常感謝你的幫助! – 2014-09-23 11:28:40

+0

非常完美,謝謝! – 2014-09-23 13:02:59