Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the language in JavaFX 8 DatePicker

When adding a DatePicker to my app I get the following:

DatePicker

I assume this is because I use Hebrew on my computer. How can I change the language of the DatePicker to be English?

like image 210
BioRoy Avatar asked Dec 09 '14 15:12

BioRoy


1 Answers

You can define the default locale for your instance of the Java Virtual Machine calling:

Locale.setDefault(Locale.ENGLISH);

Or if you can't find the locale, you need, in the pre made constants, you can look up the country code in the list of officially supported locales and create your "custom" locale like this:

Locale.setDefault(Locale("cs")) //locale for Czech language

on the start method. If you also want to implement a custom formatter for the text editor, you should add locale to the formatter too.

This is just an example:

private final DateTimeFormatter formatter = 
        DateTimeFormatter.ofPattern("EEEE, d.MM.uuuu", Locale.ENGLISH);

@Override
public void start(Stage primaryStage) {
    Locale.setDefault(Locale.ENGLISH);

    DatePicker datePicker=new DatePicker();
    datePicker.setValue(LocalDate.now());
    datePicker.setConverter(new StringConverter<LocalDate>() {

        @Override
        public String toString(LocalDate object) {
            return object.format(formatter);
        }

        @Override
        public LocalDate fromString(String string) {
            return LocalDate.parse(string, formatter);
        }
    });
    StackPane root = new StackPane(datePicker);
    Scene scene = new Scene(root, 400, 400);

    primaryStage.setScene(scene);
    primaryStage.show();
}

EDIT

By design, DatePicker uses Locale.getDefault() in all the formats applied to the controls displayed on the popup. This can be checked in com.sun.javafx.scene.control.skin.DatePickerContent class.

Unless you provide a custom skin for the control changing these formatters, in order to change the DatePicker content to English, avoiding further changes in other localized controls, a workaround could be this:

private final Locale myLocale = Locale.getDefault(Locale.Category.FORMAT);

@Override
public void start(Stage primaryStage) {
    DatePicker datePicker=new DatePicker();
    datePicker.setValue(LocalDate.now());
    datePicker.setOnShowing(e-> Locale.setDefault(Locale.Category.FORMAT,Locale.ENGLISH));
    datePicker.setOnShown(e-> Locale.setDefault(Locale.Category.FORMAT,myLocale));
    ...
}

EDIT 2

Returning to the original locale on setOnShown is too soon, since if the user changes the month, the original locale is used and it will not be shown properly. To work it should be turned off both on setOnHiding and on setOnAction.

private final Locale myLocale = Locale.getDefault(Locale.Category.FORMAT);

@Override
public void start(Stage primaryStage) {
    DatePicker datePicker=new DatePicker();
    datePicker.setValue(LocalDate.now());
    datePicker.setOnShowing(e-> Locale.setDefault(Locale.Category.FORMAT,Locale.ENGLISH));
    datePicker.setOnHiding(e-> Locale.setDefault(Locale.Category.FORMAT,myLocale));
    datePicker.setOnAction(e-> Locale.setDefault(Locale.Category.FORMAT,myLocale));
    ...
}
like image 192
José Pereda Avatar answered Nov 12 '22 05:11

José Pereda