How to apply a font from a file .properties to the label in a Java program?

There is a label from which I get the font(font name, family, style and size of font) and save in the properties file.

Do it as follows:

@FXML
 void applyFont(ActionEvent event) throws FileNotFoundException, IOException {
 FileInputStream in = new FileInputStream("config.properties");
 Properties properties = new Properties();
properties.load(in);
in.close();

 FileOutputStream fos = new FileOutputStream("config.properties");

 properties.setProperty("categoryFont1", fontLabel.getFont().toString());

 properties.store(fos, null);

fos.close();
}


The font stored in the file .properties like this:

5ca583a74fe98944498420.jpegHow to apply this font from the properties file to the label?

I tried to do the following:

private void readPropertiesFile() {
 try {
 File File = new File("config.properties");
 FileInputStream fileInput = new FileInputStream(file);
 Properties properties = new Properties();
properties.load(fileInput);
fileInput.close();

 CategoryFont1 String = properties.getProperty("categoryFont1");

category1.setStyle(categoryFont1);

} catch (FileNotFoundException e) {
 System.out.println("File not found");
 } catch (IOException e) {
 System.out.println("No such file");
}
 }


But I get an error:

5ca5847c12c00283943073.jpeg
March 19th 20 at 09:59
1 answer
March 19th 20 at 10:01
Solution
it also says that she wants the colon where you have the equal sign..
Yes, but how to do it? I don't know how to do it. Is there any way to do custom formatting of the font? that is, to make as on the screen according to the type of CSS? - Muhammad_Larson84 commented on March 19th 20 at 10:04
5ca75be8af6fd158767834.png
style.properties
font=-fx-font-size:36px;-fx-font-smoothing-type: lcd;-fx-text-fill: blue;-fx-font-family: "Comic Sans MS";-fx-stroke: black;

label.setFont(Font.font("System", FontWeight.NORMAL (double) 30.0));

 String style = properties.getProperty("font");
 if (style != null) {
 System.err.println("Applying style :" + style);
label.setStyle(style);
 }


screen a little no matter what css is shown , but

System.err.println(
 "Reading the font information:" + label.getFont().toString()); - will not change

 // Reading the font information: Font[name=System Regular, family=System,
 // style=Regular, size=30.0]
 // TODO: convert
System.err.println(
 "Reading the style information:" + label.getStyle().toString());

// will output the same style as was set:
Reading the style information: -fx-font-size:36px;-fx-font-smoothing-type: lcd;-
fx-text-fill: blue;-fx-font-family: "Comic Sans MS";-fx-stroke: black;
- danial_Daniel48 commented on March 19th 20 at 10:07
@danial_Daniel48, I don't need to specify the exact parameters of the font of the label, as the font of the label will be constantly changing, it is necessary to obtain the existing font of the label, convert it to a format understood by CSS, save to file .properties, then read the font from the properties file and again convert back to font, clear label - Muhammad_Larson84 commented on March 19th 20 at 10:10
@danial_Daniel48, I tried as you have in the latest comments from getStyle.toString();

But why is the label blank value, even though the text is there, and in the file properties also remains empty meaning font:

5ca78d83c374d260419670.jpeg5ca78d89f072b015832947.jpeg - Muhammad_Larson84 commented on March 19th 20 at 10:13
you all example ? sorry at work - a bit busy now

https://github.com/danial_Daniel48/selenium_java/blob/mas...
^^
Like it? Star it!

https://www.youtube.com/watch?v=RN0-QHZnvKk

in the latest commit saves properties - button actionEvent amp-TA sama will be examined

5ca79a9ea42ca848379958.png - danial_Daniel48 commented on March 19th 20 at 10:16
@danial_Daniel48, thank you for your answers! sorry that I am arrogant, but that's because I'm new in programming and many things do not understand. Of the entire foot bindings on github for your reference, I found what I need:
LabelFontStyle String = String.format("%s -fx-text-fill: purple;",
 label.getStyle().toString());


It works for me, but how can you not specify a specific value for each parameter of the CSS, like here purple, and to obtain a value from the label? - Muhammad_Larson84 commented on March 19th 20 at 10:19
you will see that it writes in fontstyle.properties:
#Fri Apr 05 15:18:56 EDT 2019
font=-fx-font-size\:36px;-fx-font-family\: "Comic Sans MS";-fx-stroke\: black; -
fx-text-fill\: purple;

all hands except the added fx-text-fill\: purple; picked up from the label.getStyle().toString() - danial_Daniel48 commented on March 19th 20 at 10:22
@danial_Daniel48,
Thank you very much for the tips!

We had a right to format the String :

LabelFontStyle String = String.format("%2s %2s %2s",
 "-fx-font-size: "+ fontLabel.getFont().getSize() + ";",
 "-fx-font-family: "+ fontLabel.getFont().getName() + ";",
 "-fx-text-fill: "+ "#" + fontLabel.getTextFill().toString().substring(2) + ";");

CategoryFont8 String = properties.getProperty("categoryFont8");
category8.setStyle(categoryFont8);


In the file .properties now have to look like this:

5ca8347eec59e006530266.jpeg

It works!) - Muhammad_Larson84 commented on March 19th 20 at 10:25

Find more questions by tags Java