JavaFX CSS change property of custom style class at runtime - java

Im very new to CSS since I was never working with weblanguages but for a JavaFX application I've got a css style sheet with a windows 10 UWP theme. The default style classes use the default windows grey button theme, but i also created a custom style class for colored components.
The colors are as variables in the the .root style class for the default style and are overwritten in the .colored style class for the colored style.
.root
{
-fill-color: #CCCCCC;
...
}
.colored
{
-fill-color: #DD2867;
...
}
I now want to change the colored style colors at runtime.
I know about Node#setStyle(String) in which i can modify the fill color with something like this:
root.setStyle("-fill-color: #FF00FF;");
but this has only an effect on the color in the .root style class and not the .colored style class.
Can you tell me a way to direcly modify a property of a style class at runtime or maybe an even better approach to use a default and a colored style?
Thanks in advance,
Eleom.

Define another looked-up color on the root node, and use it in your .colored class:
{
-fill-color: #CCCCCC;
-colored-fill: #DD2867 ;
...
}
.colored
{
-fill-color: -colored-fill;
...
}
Then you can change that color programmatically the same way:
root.setStyle("-colored-fill: ... ;");

Related

CSS styling of ControlsFX segmented bar text

How can I change the size and color of the text for the ControlsFX segmented bar? The usual way:
-fx-font-size: 15px
Doesn’t seem to work.
It doesn’t matter to me whether I change this using a CSS stylesheet or through the code. I have tried both. -fx-background-color: does work, so my styling is having an effect.
I just can’t seem to find anywhere, or intuitively figure out the correct CSS for how to style the text of the segmented bar control.
Thanks!
ControlsFX uses a default SegmentViewFactory that uses a class called SegmentView with a style class segment-view, so this is the way to style a text in a SegmentedBar:
.segment-view {
-fx-font-size: 15px;
}
Another way to style the text in a SegmentedBar is to use a custom SegmentViewFactory. Here is an example:
SegmentedBar<Segment> bar = new SegmentedBar<>();
bar.getSegments().addAll(
new Segment(2.46, "Apple"),
new Segment(2.43, "Microsoft"),
new Segment(1.94, "Alphabet"),
new Segment(1.72, "Amazon"));
bar.setOrientation(Orientation.HORIZONTAL);
bar.setSegmentViewFactory(segment -> {
Label viewNode = new Label(String.format("%s (%.2fT)", segment.getText(), segment.getValue()));
viewNode.setStyle("-fx-font-size: 15px; -fx-font-style: italic;");
return viewNode;
});

Style the background of a Vaadin 14 Dialog

I want to change the background-color of a Vaadin 14 Dialog. I did not get it how to do it. There is no method for it. The id is "overlay" and the color is #3b85e6.
How to do that? I only managed to add styles to the content, but i want to change the background.
In your Java code, your view class, add import for styles.
#CssImport(value = "./styles/my-dialog.css", themeFor = "vaadin-dialog-overlay")
And create file "frontend/styles/my-dialog.css", whos content is
:host {
background: #3b85e6;
}

Vaadin change Textfield Label color

I am looking for a easy way to change the Label color of the Vaadin TextField Component.
It changes automatically to blue when the Textfield is on focus, but I need to change it to another color.
First I tried it to change it in the css file like this:
color: <anycolor>;
But this has only changed the input text color. Is there a way to change only the color of the label? I am using Vaadin 14.
If you check the CSS applied to the <label> tag in the browser DevTools (F12 or Ctrl+Shift+C), you'll see that it's
:host([focused]:not([readonly])) [part="label"] {
color: var(--lumo-primary-text-color);
}
There's 2 options how to customize that:
Change the variable.
You can write --lumo-primary-text-color: green; to set the value of that variable in your global CSS. Multiple options:
on that one specific textField textField.getStyle().set("--lumo-primary-text-color", "green"), or
Apply it to fields with a class, textField.addClassName("green-text")
green-text {
--lumo-primary-text-color: green;
}
, or
Apply it to ALL text fields:
vaadin-text-field {
--lumo-primary-text-color: green;
}
Overload the CSS with a more specific rule. You'll need to add this to vaadin-text-field's shadow DOM with
#CssImport(value = "./styles/path/yout-vaadin-text-field.css",
themeFor = "vaadin-text-field")
:host(.green-text[focused]:not([readonly])) [part="label"] {
color: green;
}

Vaadin 14 grid multi-line cells

I have quite long Strings in one column and I want to display them as multi-line cells in my grid.
I'm using Vaadin 14 + Java and I tried to set CSS-Style Class for the specific column:
Java-Code:
#CssImport("./styles/shared-styles.css")
public class RisikoGrid extends Grid<RisikoEntity> {
public RisikoGrid() {
setSizeFull();
// add the column to the grid
addColumn(Entity::getAttribute).setHeader("MyCol")
.setFlexGrow(10).setSortable(true).setKey("mycolumn");
// set CSS style class for the specific column
this.getColumnByKey("mycolumn").setClassNameGenerator(item -> {return "grid-mycol";});
}
}
CSS (shared-styles.css)
.grid-mycol{
background: red;
white-space: normal;
word-wrap: break-word;
}
While I do see the class-name when I use the inspector in my webbrowser (chrome), the css is not applied.
What do I need to change to make it work?
Edit: this is how my styles look like - and I can't even see the background:red for instance:
You can use the built-in wrap-cell-content theme variant to allow text to wrap inside the cells.
grid.addThemeVariants(GridVariant.LUMO_WRAP_CELL_CONTENT);
See the last example on https://vaadin.com/components/vaadin-grid/html-examples/grid-theme-demos – the texts there are not that long, but if you make your browsers window narrow enough you’ll see some of the addresses wrap on two lines.
I looked into why my css settings didn't show up in the inspector and the solution was a minimal change from:
#CssImport("./styles/shared-styles.css")
to
#CssImport(value = "./styles/shared-styles.css", themeFor = "vaadin-grid")
I'm guessing the component scope of the grid differs from the global CSS.
It works now.

Vaadin: Dynamically change a textfield's background color

How does one dynamically change a textfield's visual properties, say background color?
An example might be during a validation to modify the background color dependent on the value entered.
In your war (this will be inside WebContent if you are using ecilpse) have a file in this path VAADIN/themes/customrunno/styles.css (asuming you are using runno as your theme)
With this content:
#import url(../runo/styles.css);
.my_bg_style {
background: #99EE6B;
}
In your init method:
setTheme("customruno"); // same as the folder folder under VAADIN/themes
change your textfield background:
textField.addStyleName("my_bg_style"); // same as in styles.css
to remove the style just do:
textField.removeStyleName("my_bg_style");
Here is the sample
.v-textfield-dashing
{
border: thin dashed;
background: white; /* Has shading image by default */
}
Detailed information you will get on this link.
https://vaadin.com/book/-/page/components.textfield.html
And if you want to change textfield color dynamically then you will get content on below link
https://vaadin.com/wiki/-/wiki/Main/Dynamically%20injecting%20CSS

Categories