P
P
postya2019-04-03 05:25:56
Java
postya, 2019-04-03 05:25:56

How to apply background color to label from properties file in Java program?

There is one method that saves the label's background color to the properties file. The color is selected from the JFoenix Color Picker and immediately saved to the properties file.
The label color is stored like this:
5ca41724936eb096449814.jpegHow can I read this color from the properties file and apply it to the same label?
This is necessary in order to save all label settings (color, font, name) and automatically apply all settings from the properties file the next time the program is loaded.
This is how I saved the color to the .properties file:
@FXML private JFXColorPicker btnPalette;

@FXML void changeCategoryColor(ActionEvent event) throws IOException{
    Color selectedColor = btnPalette.getValue();

 if (btnPref1.isSelected()) {
      category1.setBackground(new Background(new BackgroundFill(Paint.valueOf(selectedColor.toString()), CornerRadii.EMPTY, Insets.EMPTY)));
    }

FileInputStream in = new FileInputStream("config.properties");
    Properties properties = new Properties();
    properties.load(in);
    in.close();

    FileOutputStream fos = new FileOutputStream("config.properties");
    properties.setProperty("categoryColor1", btnPalette.getValue().toString());
    properties.store(fos, null);
    fos.close();
}

This is how I tried to take the background color of the label from the .properties file and apply it to the label:
private void readPropertiesFile() {
    try {
      File file = new File("config.properties");
      FileInputStream fileInput = new FileInputStream(file);
      Properties properties = new Properties();
      properties.load(fileInput);
      fileInput.close();

String color = properties.getProperty("categoryColor1");
      Color c = Color.web(color);

      Background bg = new Background(new BackgroundFill(c, null, null));
category1.setBackground(color);

    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

This is where the error comes in, since the color I'm getting is a String:
5ca4197c69a8f377959908.jpeg

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sergey, 2019-04-03
kuzmin @sergueik

you wanted
https://www.programcreek.com/java-api-examples/?cl...

P
postya, 2019-04-03
@postya

Solution found!

private void readPropertiesFile() {

File file = new File("config.properties");
      FileInputStream fileInput = new FileInputStream(file);
      Properties properties = new Properties();
      properties.load(fileInput);
      fileInput.close();

      String colorCategory1 = properties.getProperty("categoryColor1");
      Color c1 = Color.web(colorCategory1);

      Background bg1 = new Background(new BackgroundFill(c1, null, null));

      category1.setBackground(bg1);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question