JavaFX with FXML change css file runtime, with menu-items - css

I'm learing JavaFx and I'm trying to change the css style of my window (Mastermind game) at runtime.
Currently it works with some objects like buttons, background, menu bar, labels and shapes.
But another object I'd like to change is the menu items (background and labels).
I've tried to use the next css code to acces it :
.context-menu {
-fx-background-color: linear-gradient(rgba(200,200,200,1),rgba(50,50,50,1) 80%);
}
It works on initalisation but not when changed at runtime.
For example, it works fine for the menu bar itself :
.menu-bar {
-fx-background-color: black;
-fx-selection-bar: #505050;
}
.menu-bar .label {
-fx-font-size: 12pt;
-fx-font-family: "Segoe UI Light";
-fx-text-fill: white;
-fx-opacity: 0.9;
}
In the FXMLcontroller I call the following code to change the skin :
#FXML private AnchorPane generalPane;
private final String themeNormal = getClass().getResource("/mastermindStyles/MasterMind_Normal.css").toExternalForm();
#FXML
void handleSkinNormal(ActionEvent event){
generalPane.getStylesheets().clear();
generalPane.getStylesheets().add(themeNormal);
}
How can I do it?

I've found it :
To use predefinite styles (like Modena and Caspian) :
Application.setUserAgentStylesheet(STYLESHEET_MODENA);
To use your own css stylesheet :
Application.setUserAgentStylesheet(null);
StyleManager.getInstance().addUserAgentStylesheet(
getClass().getResource("NewSkin.css").toExternalForm());

Related

Javafx style - inheritance not working intuitively, -fx-text-fill not working

I'm new to JavaFX, and am confused by styles. I'm using OpenJFX 13.0.1. I have the following simple scene:
val oscs = HBox()
val osc1 = VBox()
val title = Text("Osc 1")
title.styleClass.add("text")
osc1.children.add(title)
oscs.children.add(osc1)
val root = BorderPane()
root.children.add(oscs)
val scene = Scene(root, 500.0, 400.0)
I have two issues:
-fx-text-fill isn't working to color the text, whereas -fx-fill is. I.e.
.text {
-fx-fill: #44FFFF;
}
works, whereas:
.text {
-fx-text-fill: #44FFFF;
}
does not. According to the docs, it seems like this is wrong.
Inheritance isn't working as I'd expect it to. Shouldn't the child nodes inherit from root?
The following doesn't work to color the label, regardless of whether it's -fx-fill or -fx-text-fill.
.root {
-fx-font-size: 24px;
-fx-font-family: sans-serif;
-fx-fill: #44FFFF;
-fx-background-color: #000000;
-fx-border-color: #44FF00;
-fx-border-width: 2px;
}
What am I doing wrong?
You probably got Label and Text confused.
Text is a subclass of Shape and does not provide a -fx-text-fill property. Furthermore Shape's -fx-fill property is not inherited. See CSS Reference Guide.
Other than going with Label you could use a descendant selector to assign the fill:
.root .text {
-fx-fill: #44FFFF;
}
Edit:
Label's -fx-text-fill is not inherited either. You should be able to fix both cases by specifying inherit as value for the -fx-text-fill or -fx-fill properties. The following should work for Text:
.root {
-fx-fill: #44FFFF;
-fx-background-color: #000000;
}
.text {
-fx-fill: inherit;
}

Javafx TableView tabelmenubutton CSS Stylesheet

By setting setTableMenuButtonVisible(true) on my tableview, I am able to see a button with a "+" sign on the top right corner, where u can select to show/hide columns. I wish to use a stylesheet to change the background color as well as the label colours etc but nothing works. I've tried the following:
.button
.toggle-button,
.menu-button {
-fx-background-color: black;
}
As always when it is about JavaFX styling, I recommand to use the Oracle documentation in order to see what compose the control (in your case the TableView and to search the default styling for each element in Modena.css (the default styling sheet since JavaFx 8).
Knowing that it is easy to style the table menu button :
/**
* For styling only the "+" button on the right top corner
*/
.table-view > .column-header-background > .show-hide-columns-button {
-fx-background-color: black;
}
/**
* In order to style any other column header's background
*/
.table-view .column-header {
-fx-background-color : yellow;
}
/**
* For styling column header's labels
*/
.table-view .column-header .label {
-fx-text-fill : green;
}

ButtonType ButtonData.OK CSS JavaFX Alert

I'm trying to style ma Alert in JavaFX. My buttons in general are alright. This is what I have in my Java Class:
alert.getDialogPane().getStylesheets().add("/gui/style.css");
ButtonType importButtonType = new ButtonType("Importeer", ButtonData.OK_DONE);
ButtonType cancelButtonType = new ButtonType("Annuleer", ButtonData.CANCEL_CLOSE);
alert.getDialogPane().getButtonTypes().setAll(importButtonType, cancelButtonType);
And this is what I have in my CSS :
.dialog-pane .button {
-fx-font-family: 'Open Sans', sans-serif;
-fx-font-size: 14;
-fx-text-fill: #55595c;
-fx-background-color: #d0d0d0;
}
But I want to style my ButtonType with the ButtonData.OK_DONE in a different style. Any Ideas how?
I had the same problem and I found the solution by myself.
If someone is still interested :
Button okBtn = (Button) alert.getDialogPane().lookupButton(alert.getButtonTypes().get(index_of_the_OK_button));
okBtn.getStyleClass().add(your_custom_class);
with your custom class in your css file, it should work :)
To change the default button used in dialog/alerts, you can add the following to your css file.
.root { -fx-default-button: red; }
Another option would be to add the following:
.button:default { -fx-base: red; }

-fx-background-color for confirmation dialog box's OK button doesn't work if it's a variable

a.css:
.dialog-pane .button {
-fx-background-color: -fx-base; -fx-text-fill: white;
}
Code:
Alert confirmationDialog = new Alert(AlertType.CONFIRMATION);
confirmationDialog.getDialogPane().getStylesheets().add("a.css");
When I open the confirmation dialog, the Cancel button is styled correctly, but the OK button is not. However, if I change -fx-background-color to an actual color, like:
.dialog-pane .button {
-fx-background-color: red; -fx-text-fill: white;
}
Both buttons will be styled correctly. I've been googling for a while and I'm stumped. Any ideas why this is happening?
The default stylesheet handles the default button by changing the definition of -fx-base:
.button:default {
-fx-base: -fx-default-button;
}
So setting the background to -fx-base won't remove the default blue color.
It's not clear quite what you want to achieve, but you are probably needing something like
.dialog-pane .button {
-fx-background-color: -fx-base;
-fx-text-fill: white;
}
.dialog-pane .button:default {
-fx-background-color: -fx-default-button;
}
and then wherever you are changing -fx-base you should also change -fx-default-button.
I see my confusion. I was working on an existing stylesheet that was trying to use -fx-base like a variable even though it's the name of an existing attribute.

Change Button textFill property in javafx

Using javafx, I have a button which in the css properties has the textFill set to white. Then during runtime it needs to be changed, which happens with this code:
import javafx.scene.control.Button;
import javafx.scene.paint.Color;
public class QAButton extends Button {
public QAButton(String text) {
this.textProperty().set(text);
}
public void setAnswerVisible(Boolean vis) {
if (vis) this.setTextFill(Color.GREEN);
else this.setTextFill(Color.BLUE);
}
}
However it doesn't do anything, the textFill will stay white. What can I do to change it during runtime?
Thank you.
Edit: I should add the css code:
.button {
-fx-text-fill: white;
}
And the button's id is set to
#question {
-fx-background-color: darkblue;
-fx-background-radius: 0;
-fx-max-width: 2000px;
-fx-max-height: 200px;
-fx-min-height: 80px;
}
Extraction from JavaFX CSS Reference (second section):
"The implementation allows designers to style an application by using style sheets to override property values set from code."
The setStyle API can fix your problem.
Example:
this.setStyle("-fx-text-fill: green");
Now all attributes in the style class will be overwritten.
If this is a problem, look here.

Resources