简体   繁体   中英

View don't refresh Button - Java FX

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

The problem is that there are 2 instances of sub/child FXMLs (and their controllers). These are:
1) Loaded from SolverManager.showMainView() using FXMLLoader
2) Loaded from FXMLMain.fxml via <fx:include> s

The scene contains the ones loaded via includes, but text changes happen at the other ones. To fix the problem you need you choose one of these approaches, either remove includes or remove fxmlloadings. Here is the second approach:

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

Add following lines to FXMLMainController :

@FXML
private FXMLLoadLogController barTabPageLoadController;

@FXML
private FXMLEditLogController barTabPageEditController;

(The variable name come from fx:id + Controller . See Subcontroller not being injected into main controller )

and change the following in the same file

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM