What's the difference between fx:id and id: in JavaFX? - java

Maybe a really newbie's question....
I'm starting learning JavaFX in a FMXL Application using the Scene Builder, by reading this tutorials:
http://docs.oracle.com/javase/8/javafx/get-started-tutorial/fxml_tutorial.htm
So once i applied some changes, an issue with this 2 IDs came up... I might have missed or confused something about them...
Can anyone tell me in which cases they are used one or another?

id you use to set a CSS ID to your Component, for example <Text id="welcome-text" .../> and in your stylesheet you have something like #welcome-text { font-size: 16pt; } so this will be applied to your Text.
fx:id you use if you want to work with your Components in your Controller class, where you annotate them with #FXML Text myWelcomeText.

The fx:id is the identity associated to component in fxml to build a controller, and the id is used for css.

I took a look at an FXML document generated using the JavaFX Scene Builder. You access controls from Java Controller with the fx:id. (edit) I stand corrected, the id does matter.
You can apply css from the FXML document like this:
<Slider id="css_id" fx:id="myslider" styleClass="style_name" .../>
(Replace slider with any control)
And Java controller interaction:
#FXML
Slider myslider;

In JavaFX id is used to set a CSS ID to a component. And fx:id is used for accessing that component in code (i.e. in a controller class). fx:id works like a components name.

Related

VaadinGrid with default component variant

I want to define all VaadinGrids in my Application with the component variant GridVariant.LUMO_ROW_STRIPES. I don't want repeat the definition on all grid instances as shown below.
grid.addThemeVariants(GridVariant.LUMO_ROW_STRIPES);
Is there any way to do this with a global configuration or something else?
I have tried so far to use the #Theme Annoation to define a theme variant. But this doesn't work.
As already mentioned in the comment, one approach is to create a subclass of Grid, apply the variant to it, and use it instead of the Grid class in your app.
Another option is to apply the CSS with which the variant is implemented to the Grid in your own theme. It's only 4 lines of CSS: https://github.com/vaadin/web-components/blob/master/packages/grid/theme/lumo/vaadin-grid-styles.js#L312-L316
Just remove the [theme~='row-stripes'] parts from the selector, and load that css into the Grid's shadow DOM e.g. by placing it in themes/your-app-theme/components/vaadin-grid.css
One option is to subclass Grid and add the variant in the constructor:
public class MyGrid extends Grid {
public MyGrid() {
addThemeVariant(GridVariant.LUMO_ROW_STRIPES);
}
}
Another option is to copy-paste the variant CSS to your own custom theme, and remove the host selector, so that that styles are not scoped to any variant:
frontend/themes/myapp/components/vaadin-grid.css:
[part~='row']:not([odd]) [part~='body-cell'],
[part~='row']:not([odd]) [part~='details-cell'] {
background-image: linear-gradient(var(--lumo-contrast-5pct), var(--lumo-contrast-5pct));
background-repeat: repeat-x;
}
If you're using Vaadin 24 (prerelease at the time of writing), you can use the new recommended way of styling (avoid injecting styles into the shadow root of the component):
frontend/themes/myapp/styles.css:
vaadin-grid::part(even-row-cell),
vaadin-grid::part(even-row-cell details-cell) {
background-image: linear-gradient(var(--lumo-contrast-5pct), var(--lumo-contrast-5pct));
background-repeat: repeat-x;
}

Vaadin Label CSS

I want to apply CSS for Vaadin 23 label but its not working properly. Please suggest me appropriate process.
Label label1 = new Label("Temp");
label1.addClassName("bold-label");
vaadin-label.css:
:host(.bold-label) [part~="label"] {
font-weight: bold;
color:red;
}
Label is standard HTML element. See Mozilla Developer documentation. Not a specific Vaadin crafted component with shadow DOM (where you would need the vaadin-*.css in the components folder).
Instead you can put your css inside styles.css and use a simple label.bold-label selector.

Javafx NullPointerException on Scenswitch [duplicate]

Maybe a really newbie's question....
I'm starting learning JavaFX in a FMXL Application using the Scene Builder, by reading this tutorials:
http://docs.oracle.com/javase/8/javafx/get-started-tutorial/fxml_tutorial.htm
So once i applied some changes, an issue with this 2 IDs came up... I might have missed or confused something about them...
Can anyone tell me in which cases they are used one or another?
id you use to set a CSS ID to your Component, for example <Text id="welcome-text" .../> and in your stylesheet you have something like #welcome-text { font-size: 16pt; } so this will be applied to your Text.
fx:id you use if you want to work with your Components in your Controller class, where you annotate them with #FXML Text myWelcomeText.
The fx:id is the identity associated to component in fxml to build a controller, and the id is used for css.
I took a look at an FXML document generated using the JavaFX Scene Builder. You access controls from Java Controller with the fx:id. (edit) I stand corrected, the id does matter.
You can apply css from the FXML document like this:
<Slider id="css_id" fx:id="myslider" styleClass="style_name" .../>
(Replace slider with any control)
And Java controller interaction:
#FXML
Slider myslider;
In JavaFX id is used to set a CSS ID to a component. And fx:id is used for accessing that component in code (i.e. in a controller class). fx:id works like a components name.

Does JavaFX support regular expressions (or wild cards) in CSS?

I've tried using wild cards in CSS selectors for JavaFX UI (TableView), but that doesn't seem to work, although JavaFX CSS reference notes that it's based on CSS version 2.1:
JavaFX Cascading Style Sheets (CSS) is based on the W3C CSS version
2.1 with some additions from current work on version 3.
For example:
TableColumnHeader[id|="column"] > .label
{
-fx-graphic: url("ico.png");
}
The above CSS is an attempt to show an icon "ico.png" on all column headers of a TableView
TableColumnHeader is the type selector for the table's column header Node
.label is the style class for the Label node rendered within the column header
[id|="column"] is similar to the example mentioned here: https://www.w3.org/TR/CSS21/selector.html#matching-attrs
The id of column header is inherited from its TableColumn. The id is set on the TableColumn object as follows: tableColumn.setId("column-"+ columnName) where columnName is a String variable
The above CSS doesn't work. Any variation that includes [id=...], or any other attribute other than id doesn't work.
Is this a limitation in JavaFX? or is there a way to make it work?

how to specify default font in javafx WebView to be the same as default UI font

Some coworkers and I are writing help documentation in HTML to be used with a JavaFX WebView panel. We want it to look consistent with the rest of the UI. Normally I would specify the default font in a CSS file, but the rest of the JavaFX application appears to use the default system UI font.
Is there any way to set this programmatically? I can pick Segoe UI by name in .css, but we want it to look correct regardless of platform.
Use
Font.getDefault().getName()
to get the defaults font name and just add it to the HTML file / String which is about to be added into the WebView.
example:
cssString = cssString + "html { font-family: '" + Font.getDefault().getName() + "';}";
Ofcause you should do it cleaner than this.
If I understood you completely you want a default font for the whole javafx part of your application. I suggest you do this:
Download a font of your own as .ttf and add it to the resources in your application.
Load the font with the static function Font.loadFont:
Font.loadFont(this.class.getClassLoader().getResource("fonts/YOURFONT.ttf").toExternalForm(), 10);
Now set the font to the root of the application. I suggest doing that with css as it is suggested here -> another stackoverflow answer: In your application.css add:
.root{ -fx-font-family: "YOURFONT"; }
After that you need to load the application.css file and set it on your scene:
scene.getStylesheets().add(this.class.getClassLoader().getResource("css/application.css").toExternalForm());
I believe that it is better if you are not dependent of the system on which your app is running, so pack the font in your application.
For the html you can do completely the same thing with normal css by providing a #font-face.

Categories

Resources