查看不刷新按钮 - Java FX

时间:2014-09-22 14:40:24

标签: java swing javafx javafx-8

这是我在JAVAFX的第一个项目,它目前是一个简单的程序。我的问题是当程序启动按钮" loadLogButton" FXMLLoadLogController收取正确的消息。但是当我在comboBox中选择另一种语言时,按钮的设置器工作正常但视图没有刷新。问题是什么?

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

1 个答案:

答案 0 :(得分:0)

问题在于有2个子/子FXML(及其控制器)实例。这些是:
1)使用SolverManager.showMainView()来自FXMLLoader加载 2)通过<fx:include> s FXMLMain.fxml 加载

场景包含通过包括加载的场景,但文本更改发生在其他场景中。要解决此问题,您需要选择其中一种方法,删除包含或删除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

@FXML
private FXMLLoadLogController barTabPageLoadController;

@FXML
private FXMLEditLogController barTabPageEditController;

(变量名称来自fx:id + 控制器 。请参阅Subcontroller not being injected into main controller

并在同一个文件中更改以下内容

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