简体   繁体   English

查看不刷新按钮-Java FX

[英]View don't refresh Button - Java FX

This is my first project in JAVAFX , it's a simple program for the moment. 这是我在JAVAFX中的第一个项目,目前是一个简单的程序。 My problem is that when the program starts the button "loadLogButton" of the FXMLLoadLogController charge the properly message. 我的问题是,当程序启动时,FXMLLoadLogController的按钮“ loadLogButton”会正确充电。 But when I choose another language in the comboBox , the setter of the button works fine but the view is not refreshed. 但是,当我在comboBox中选择另一种语言时,按钮的设置器可以正常工作,但视图不会刷新。 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. RPV:LoadLogController的chargeI18nValues函数可以正常工作,但是从SolverManager中调用按钮文本时,不会在程序中刷新按钮文本。

Codes: 代码:

SolverAssistant.java 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 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 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 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). 问题是存在两个子/子FXML实例(及其控制器)。 These are: 这些是:
1) Loaded from SolverManager.showMainView() using FXMLLoader 1 SolverManager.showMainView()使用FXMLLoaderSolverManager.showMainView() FXMLLoader
2) Loaded from FXMLMain.fxml via <fx:include> s 2)通过<fx:include>FXMLMain.fxml加载

The scene contains the ones loaded via includes, but text changes happen at the other ones. 场景包含通过include加载的场景,但是其他位置发生文本更改。 To fix the problem you need you choose one of these approaches, either remove includes or remove fxmlloadings. 要解决此问题,您需要选择以下方法之一,即删除包含或删除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 : FXMLMainController添加到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 ) (变量名称来自fx:id + Controller 。请参见未将Subcontroller注入到主控制器中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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