This is my first project in JAVAFX , it's a simple program for the moment. My problem is that when the program starts the button "loadLogButton" of the FXMLLoadLogController charge the properly message. But when I choose another language in the comboBox , the setter of the button works fine but the view is not refreshed. What's the problem?
RPV: The function chargeI18nValues of the LoadLogController works fine but the button text is not refreshed in the program when its called from the SolverManager.
Codes:
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);
}
}
}