Reputation: 517
I'm making app using JavaFX and Scene Builder
I have label:
@FXML
private Label category1;
When I click on toggle button it calls the method:
@FXML
void changeFont(ActionEvent event) {
try {
FontSelectorDialog fs = new FontSelectorDialog(null);
fs.setTitle("Select Font");
fs.setHeaderText("");
fs.setContentText("asdasdada");
fs.show();
} catch (Exception e) {
System.out.println("can't load new window");
}
}
It shows ControlFX FontSelector Dialog where I can pick font family, font style and font size, There are also two buttons "OK" and "CANCEL"
How to apply changes in this dialog to my label when I click on "OK" button?
EDIT:
I've tried that line in code with "en" and "US", but nothing happens.
FontSelectorDialog fs = new FontSelectorDialog(null);
fs.setTitle("Select Font");
fs.setHeaderText("");
Localization.setLocale(new Locale("en", "US"));
fs.show();
fs.setOnCloseRequest(e -> {
if (fs.getResult() != null) {
category1.setFont(fs.getResult());
}
});
} catch (Exception e) {
System.out.println("can't load new window");
}
But language doesn't change. I also found files with languages and I wanted to edit file with russian laguage there
Other languages have strings like that:
But russian language have strange symbols:
I don't know how to change those symbols to make sample text become in english. Any ideas?
EDIT 2: I' tried your version and nothing happens. I even tried to use Localization in Main class, no results
Locale locale = Localization.getLocale();
Localization.setLocale(new Locale("en", "US")); // en-US
FontSelectorDialog fs = new FontSelectorDialog(null);
fs.show();
fs.setOnCloseRequest(e->{
if (fs.getResult() != null) {
category1.setStyle(null);
category1.setFont(fs.getResult());
}
Localization.setLocale(locale); // Reset locale
});
I copied file controlsfx.properties, changed the name to controlsfx_en_US.properties
but when I pasted this file to directory with other files alert dialog appears, even there is no such file with that name:
I' also tried to change the name to controlsfx_eng_USA.properties, but Alert dialog appears : "File couldn't be created"
Upvotes: 1
Views: 1062
Reputation: 49201
show
doesn't wait for a user response and you have to poll the result
property to get the result, e.g. on closing the font-dialog:
fs.setOnCloseRequest(e -> {
if (fs.getResult() != null) {
category1.setFont(fs.getResult());
}
});
If you want to wait for the user response (and I assume that's what you want) you can rather use showAndWait
. For this, just replace
fs.show();
with
Optional<Font> optional = fs.showAndWait();
if (optional.isPresent()) {
category1.setFont(optional.get());
}
See showAndWait and show for further details.
EDIT 1: Concerning the answer "EDIT: @Topaco I've tried your code"
There seems to be a problem due to the css
, more precisely with the line -fx-font-size: 2em;
. The problem is not directly related to the font-dialog (or the show
- / showAndWait
-method) itself, also a simple
category1.setFont(...)
doesn't work in combination with the css
for some reason.
A solution depends on your code. If you have set the styleClass
-property of the label in your fxml
, a possible solution is to define the font-size outside of the css
using the style
-property:
<Label fx:id="category1" ... styleClass="category-label" style="-fx-font-size: 2em;"/>
with
.category-label {
-fx-background-color: #8CA5FF;
-fx-text-fill: #fff;
-fx-padding: 0 0 0 20px;
}
Then, before you set the font remove the style with category1.setStyle(null)
:
fs.setOnCloseRequest(e->{
if (fs.getResult() != null) {
category1.setStyle(null);
category1.setFont(fs.getResult());
}
});
This should work.
EDIT 2: Concerning the answer "EDIT: Thanx @Topaco for the Answer, it works! But I would like also to modify another thing"
The language displayed in the ControlsFX-elements is controlled by the locale returned by Localization.getLocale()
. If no locale has been explicitly set with Localization.setLocale(...)
the default-locale returned by Locale.getDefault()
is used (e.g. see ControlsFX Dialogs and Localization. Generally, the latter is determined by the JVM from the host-system (e.g. see how do I set the default locale for my JVM?).
Thus, I assume that your default-locale is the russian-locale which is automatically taken because you haven't probably set a locale with Localization.setLocale(...)
.
A possible solution is to set the locale at the very beginning of the start
-method with
Localization.setLocale(new Locale("en","US"));
affecting the language of all ControlsFX-elements to be US-english.
If only the language of the font-dialog should be changed set the locale at the very beginning of the changeFont
-method and reset it to the old value at the end:
@FXML
void changeFont(ActionEvent event) {
try {
Locale locale = Localization.getLocale();
Localization.setLocale(new Locale("en", "US"));
FontSelectorDialog fs = new FontSelectorDialog(null);
fs.show();
fs.setOnCloseRequest(e->{
if (fs.getResult() != null) {
category1.setStyle(null);
category1.setFont(fs.getResult());
}
Localization.setLocale(locale); // Reset locale
});
} catch (Exception e) {
System.out.println("can't load new window");
}
}
Of course, there are further possibilities, e.g. you can change the settings in your host-system (which of course has a massive impact also outside of your application and most likely you don't really want that) or an application-wide change of the default-locale at the beginning of the start
-method with Locale.setDefault(...)
(which affects all elements and not only the ControlsFX-elements).
EDIT 3: Concerning the EDIT-section of the question
1) There are two minor problems. First, contrary to the statement in the previous EDIT-section the en-US-locale has to be set with
Localization.setLocale(new Locale("", ""));
as explained in more detail below. Second, you have to call that method before you create the FontSelectorDialog
-instance i.e. at the very beginning of the changeFont
-method, e.g.:
@FXML
void changeFont(ActionEvent event) {
try {
Locale locale = Localization.getLocale();
Localization.setLocale(new Locale("", "")); // en-US
FontSelectorDialog fs = new FontSelectorDialog(null);
fs.show();
fs.setOnCloseRequest(e->{
if (fs.getResult() != null) {
category1.setStyle(null);
category1.setFont(fs.getResult());
}
Localization.setLocale(locale); // Reset locale
});
} catch (Exception e) {
System.out.println("can't load new window");
}
}
That code only changes the language of the font-dialog. As an alternative there is nothing wrong with calling the Localization.setLocale(...)
at the very beginning of the start
-method which changes the language of the ControlsFX-elements in the entire application.
The Localization.setLocale(...)
-method is also described in ControlsFX Dialogs, section "Localizing the Dialogs", point 5.
There is nothing else to do here to make it work, in particular it isn't necessary to replace the russian text in the controlsfx_ru_RU.properties
-file with an english text. On the contrary, that's the completely wrong way, see below.
2) Concerning your properties-files: This is exactly the source from which the language information is read, i.e. if you e.g. set the locale to the russian locale with Localization.setLocale(...)
the information is read from controlsfx_ru_RU.properties
.
In this context, please note, that the both suffixes in the name of the properties-file, e.g. controlsfx_ru_RU.properties
, "ru" and "RU", repectively, are used to instantiate the Locale
which is set with the Localization.setLocale(...)
-method. Thus, the russian locale is set with
Localization.setLocale(new Locale("ru", "RU"));
en-US is the default language which is read from controlsfx.properties
. Thus, the en-US-locale is set with
Localization.setLocale(new Locale("", ""));
and not with Localization.setLocale(new Locale("en", "US"));
as one might expect at first glance.
In general, you do not have to change the properties-files - with the only exception that you want to replace the original text with a custom text. To achieve this you have to proceed as follows: Copy the properties-file, then, make your changes in the copy and finally, place the copy in the project's src-folder. Now, the copy (with the custom text) is used instead of the original properties-file. This is also described in ControlsFX Dialogs, section "Localizing the Dialogs", points 1 - 4.
Note: As you see each properties-file corresponds to a specific language defined by the suffixes in the name of the properties-file. The usage of a language other than specified in the name isn't intended. I mention that explicitly because you indicate in the edited question to substitute the russian text in the russian properties-file with an english text. Don't do that (neither in the original properties-file nor in a copy)! That's just confusing and it isn't necessary at all (as described above).
3) Concerning the "strange symbols": Those symbols are Unicode-codepoints defining cyrillic letters, e.g. \u041F
is the codepoint defining the cyrillic capital letter PE
, see Cyrillic script in Unicode. So if you e.g. want to replace the current russian text in the controlsfx_ru_RU.properties
-file with a new russian text you have to use the unicode-codepoints which correponds to the letters of your new russian text.
Upvotes: 1