Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to reload the screen when changing languages in JavaFX 2?

773846Mar 19 2015 — edited Mar 20 2015

I'm beginner with javaFX and i need to change language of the screen. but i have no idea how to reload the screen when the language was changed. The application have a button where have the language available. I want just refresh screen when the user change language.

Here is the start method to show the stage.

    @Override

    public void start(Stage stage) throws Exception

    {

        this.stage = stage;

        Locale locale = Locale.getDefault();

        ResourceBundle rb = ResourceBundle.getBundle("resources/Label",locale);

        loader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"),rb);

        root = (Parent)loader.load();

        FXMLDocumentController controller = (FXMLDocumentController) loader.getController();

        controller.setMain(this);

        scene = new Scene(root);

        stage.setScene(scene);

        stage.show();

    }

here is the function to change the language(in the same class with start function),the rb is the new ResourceBundle:

    public void refresh(ResourceBundle rb)

    {

          //change the language here

     }

1. I don't want to use the resourceBundle to get value in resource file and set label in scene one by one.like following:

this.btnLabel.setText(rb.getString(key.test));

...

2. I don't want to reload the scene,like following:

    public void refresh(ResourceBundle rb)

    {

         try

        {

            loader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"),rb);

            root = (Parent)loader.load();

            scene.setRoot(root);

            FXMLDocumentController controller = (FXMLDocumentController) loader.getController();

            controller.setMain(this);

           

        }

        catch(Exception err)

        {

            err.printStackTrace();

        }

    }

So do we have a solution to just set the resourceBundle and reload the scene easier?

Thanks and best regards!

This post has been answered by bouye-JavaNet on Mar 20 2015
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 17 2015
Added on Mar 19 2015
5 comments
4,781 views