My first problem: [...]
It is simply the defined hierachy that e. g. a background color styling command for one control made in the FXML file will always overwrite the styling command for the same control made in the CSS file. It is the same behavior as if you have pure CSS with a set #id and a set .class for a control. The e. g. background color defined in the id statement will overwrite the background color defined for the class. So it is the standard behavior and you can not change it.
Second problem: [...]
There isn‘ t such a CSS command like „-fx-background-transparency: 0.7;“. You can do it like this with CSS (and without overwriting in FXML):
CSS File:
.my-btn-class {
-fx-background-color: rgb(176, 30, 0);
}
.my-btn-class:hover {
-fx-background-color: rgba(176, 30, 0, 0.7);
}
FXML File:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<Button styleClass="my-btn-class" stylesheets="@styling.css" text="Button" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller" />
Or you could do it like this:
Controller Class:
package sample;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Controller implements Initializable {
@FXML
private Button button;
@Override
public void initialize(URL location, ResourceBundle resources) {
button.hoverProperty().addListener(((observable, oldValue, newValue) -> makeButtonTransparent(button, newValue)));
}
private void makeButtonTransparent(Button button, boolean transparent) {
// Get the current style statements:
String currentStyle = button.getStyle();
// Check if there is a styling statement for background color with rgb or rgba:
Pattern pattern = Pattern.compile("-fx-background-color: rgb(a?)\\(([\\d,\\s.])*\\);");
Matcher matcher = pattern.matcher(currentStyle);
String currentBackgroundColorStyle;
if (matcher.find()) {
// Extract the existing background color statement:
currentBackgroundColorStyle = currentStyle.substring(matcher.start(), matcher.end());
} else
// No statement for background color in rgb(a) found:
return;
// Get the rgb values from the string:
int[] rgb = new int[3];
matcher = Pattern.compile("\\d{1,3}").matcher(currentBackgroundColorStyle);
for (int i = 0; i < 3; i++) {
if (matcher.find())
rgb[i] = Integer.parseInt(currentBackgroundColorStyle.substring(matcher.start(), matcher.end()));
}
if (transparent)
// Replace the background color statement with transparency value:
button.setStyle(currentStyle.replace(currentBackgroundColorStyle, String.format("-fx-background-color: rgba(%d, %d, %d, 0.7);", rgb[0], rgb[1], rgb[2])));
else
// Replace the background color statement without transparency value:
button.setStyle(currentStyle.replace(currentBackgroundColorStyle, String.format("-fx-background-color: rgb(%d, %d, %d);", rgb[0], rgb[1], rgb[2])));
}
}
FXML File:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<Button fx:id="button" style="-fx-background-color: rgb(176, 30, 0); -fx-border-color: blue;" text="Button" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller" />