Lookup style of POJO object in CSS - css

A helper Label that doesn't really fit in fxml, so it is created (as a POJO) and attached at initialize() step.
This is straightforward in fxml:
stylesheet.css: .my-label { -fx-font-size: 12px; }
my_view.fxml: <Label fx:id="label" styleClass="my-label" />
Or in POJO locally: label.setStyle("-fx-font-size: 12px;");
What is the right way to define Label in POJO, but move style to css?

All you need to do is add the style class:
label.getStyleClass().add("my-label");
and then use the same CSS you have for your FXML example.

Related

Vaadin Flow/10/11 style component via css

My question is pretty basic.
How to add styling from a css-file to a basic vaadin component?
What I do NOT want to use:
PolymerTemplate
getStlye().set(...)
Do I have to #ImportHtml, which includes the css-code or do I have to #StyleSheet with the css-file? And afterwards, do I have to add the "css-style" via .getElement().getClassList().add(...)?
I really need help to have a working simple code example for a Label, Button or whatsever, please. I cannot find anything to satisfy my requirements.
In our documentation we guide to use #ImportHtml in MainView for global styles as a html style module.
In the global style module you can apply themable mixins to change stylable shadow parts, etc. of the components.
In case your target is not in shadow DOM, you can set the styles in custom styles block directly, e.g.
Say you have a Label and TextField in your application
// If styles.html is in src/main/java/webapp/frontend/ path is not needed
#HtmlImport("styles.html")
public class MainLayout extends VerticalLayout implements RouterLayout {
...
Label label = new Label("Title");
label.addClassName("title-label");
add(label);
...
TextField limit = new TextField("Limit");
limit.addClassName("limit-field");
add(limit);
...
}
And in src/main/java/webapp/frontend/styles.html
<custom-style>
<style>
.title-label {
color: brown;
font-weight: bold;
}
...
</style>
</custom-style>
<dom-module theme-for="vaadin-text-field" id="limit-field">
<template>
<style>
:host(.limit-field) [part="value"]{
color:red
}
</style>
</template>
</dom-module>
And your "Title" text will have brown bold font, and the value in text field will be red, but its title un-affected.
See also: Dynamically changing font, font-size, font-color, and so on in Vaadin Flow web apps

How to refer to an anchor pane in css?

is there a possibility to refer to an anchor pane in css?
If the anchor pane happens to be the root, than it's ok:
.root{
-fx-background-image: url("xxx.yy");
}
It works. But if not, then how to do this? I tried .anchor-pane{}, it didn't work. Then I read, that anchor pane has everything that Pane has. So I tried .pane{} too... It didn't work.
How can I set the background to a none root anchor pane?
Thank you!
You can always assign a css class and add it to the AnchorPane explicitly
anchorPane.getStyleClass().add("pane");
In your css :
.pane{
-fx-background-image: url("xxx.yy");
}
You can perform the same by adding a css id
anchorPane.setId("pane");
In you css :
#pane{
-fx-background-image: url("xxx.yy");
}
This answer is the same as Itachi's I just wrote it at the same time..
You use CSS selectors in a CSS stylesheet to select nodes.
A pane is a node. A node can have a css id set on it node.setId("xyzzy-id"), or it can have style classes set on it node.getStyleClass().add("xyzzy-class").
For the provided examples you could select the pane in either of these ways:
Select by ID:
#xyzzy-id {
-fx-background-color: palegreen;
}
Select by Class:
.xyzzy-class {
-fx-background-color: papayawhip;
}
You can also set FXML attributes on the node for the id and style class (rather than doing this in code as explained above). SceneBuilder has fields for this, so if you are writing FXML using SceneBuilder, just fill in the appropriate fields and it will add the required attributes to your FXML file.

How to set Default CSS for all Buttons in JAVAFx

I have over 100 Buttons in my JAVAfx application and I want to give a DEFAULT styling[given below] to all buttons in the programme. Please help ! :)
-fx-background-color:#3c7fb1;
-fx-text-fill: black;
-fx-font-size: 14px;
-fx-padding: 3 30 3 30;
Create a new CSS file.
Attach the CSS file to your Scene.
Put your button styles to .button {}.
It's easy to set default Style for all JavaFX Button in an application.
Just give a id to the style sheet which you want to set as default for all button.And then set this id for all button of your application.
Button button =new Button("Button");
Button button1 =new Button("Button");
button.setId("allbtn");
button1.setId("allbtn");
String style= getClass().getResource("New.css").toExternalForm();
scene.getStylesheets().add(style);
Create buttons
apply id to them (for css as we apply in the html) using setId().
Define CSS for this ID
Finally add CSS file to the Scene Thats it.
And CSS file :
#allbtn{
-fx-color:black;
-fx-padding:4px;
-fx-background-color:#34c669;
-fx-background-radius: 10px;
}
Learn more about JavaFX Button CSS
To apply a default style in an fxml file add a '.button' class to your css file and include it in the Anchor pane
Add this to your css file e.g. app.css
.button {
-fx-color:black;
-fx-padding:4px;
-fx-background-color:blue;
-fx-background-radius: 10px;
}
Update your AnchorPane tag to include the stylesheet:
<AnchorPane prefHeight="700.0" prefWidth="500.0" stylesheets="#../app.css" >
This will change all buttons to use your style for within the fxml file

Why doesn't setting the styles of Labels work?

I want to change the font size and weight in a label. I did find this on Stack Overflow:
StackOverflow article
However it doesn't seem to fully answer the question OR there is something I am not getting.
Consider the following code:
package com.mycompany.project.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Label;
/**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class StyleTest implements EntryPoint
{
private final Label lblATestLabel = new Label( "A Test Label" );
public void onModuleLoad()
{
RootPanel rootPanel = RootPanel.get();
lblATestLabel.setStyleName( "gwt-Label.TestStyle" );
rootPanel.add( lblATestLabel, 204, 187 );
lblATestLabel.setSize( "73px", "32px" );
HTML hlabel = new HTML();
hlabel.setStyleName("gwt-Label.TestStyle");
hlabel.setHTML("Brown <span class=\"brown\">fox</span>");
rootPanel.add(hlabel, 42, 36);
hlabel.setSize("335px", "41px");
}
}
And this style at the bottom of StyleTest.css:
.gwt-Label.TestStyle {
color: Green;
vertical-align: middle;
}
My expectation is that my text will be centered vertically and be green. The result is no change at all. I can set any styles I want and there is no change to either the Label or HTML label. It all looks normal, what am I doing wrong?
setStyleName sets the class attribute (actually, the className property, but that's equivalent) of the widget's element; so lblATestLabel.setStyleName("gwt-Label.TestStyle") will be equivalent to having a <div class="gwtLabel.TestStyle"> in HTML, that is an element with a single class of gwt-Label.TestStyle (class="" is whitespace-separated).
On the other hand, .gwt-Label.TestStyle as a selector in your CSS stylesheet will match any element with both the gwt-label and TestStyle classes; that would be an element such as <div class="gwt-Label TestStyle"> (remember: class="" is whitespace-separated)
So, depending on what you need, either fix your selector (.gwt-Label\.TestStyle, might not work in all browsers), rename your class (e.g. gwt-Label-TestStyle) or add a class to the existing (for a Label widget) gwt-Label one (lblATestLabel.addStyleName("TestStyle"))

Add custom style property to MXML Custom Component

I have a Custom Component that has a couple of Canvas with some background colors assigned to them. Now i have hard coded the colors, i want to move them to an external css file.
So i would like to have the css declaration like this :
ControlBar
{
dividerRightColor: #ffffff;
dividerLeftColor: #f3f3f3;
}
My question is if i can define custom style names like dividerRightColor and if so, how can i use that value inside my MXML Component? I have seen examples of using them inside Pure AS components.
In CSS:
.dividerRightColor {
background-color: #ffffff;
}
.dividerLeftColor {
background-color: #f3f3f3;
}
In MXML:
<mx:ControlBar>
<mx:Canvas styleName="dividerLeftColor">
…
</mx:Canvas>
<mx:Canvas styleName="dividerRightColor">
…
</mx:Canvas>
</mx:ControlBar>
It sounds to me like you need to create the style in the component; not just send the style values into the component as the other answer.
Read this documentation.
Basically, styles don't get defined the same way that properties get defined. You can set any style name on the component you want. However, the component needs to know what to do w/ the style. To do that you need to override the styleChanged method:
override public function styleChanged(styleProp:String):void {
super.styleChanged(styleProp);
// Check to see if style changed.
if (styleProp=="dividerRightColor")
{
// do stuff to implement the style
dividerRight.setStyle('backgroundColor',getStyle('dividerRightColor'));
}
}
A common approach is to set "styleChanged" properties and invalidate the display list and then make the appropriate style changes in the updateDisplayList() method.
To make the style available in code hinting, you'll need to add metadata, like this:
[Style(name="dividerRightColor")]
This will only be required if you wish to set the style as a property in MXML.

Resources