How to use custom fonts with CSS in JavaFX? - java

I'm going to lose my mind now. I am unable to connect my font in css styles for a label in my javafx project. I just don't know what the matter is, the stylesheet is connected correctly, because I can change the color of the label. I put the font file in the same directory as the stylesheet, tried even specifying the absolute path. Nothing works. What am I doing wrong?
Java 11, JavaFX 16.
And yes, I checked all the titles and the name of the font. Everything matches
CSS
#font-face {
src: url("Lobster-Regular.ttf");
}
.label {
-fx-font-family: "Lobster";
}
FXML:
<Label id="main_label" layoutX="237.0" layoutY="47.0" stylesheets="#styles/lable.css" text="Test" textFill="WHITE">
</Label>
enter image description here

Related

Unicode emojis with colour

I'm trying to build a instant messaging application using JavaFX. I want to implement emoji support by using unicode however, when using a font such as Google's Noto Color Emoji the emoji's only appear as greyscale. like so:
I've applied the font like so and can confirm that it is loading correctly.
#font-face {
font-family: 'Noto Color Emoji';
src: url('/fonts/NotoColorEmoji.ttf');
}
.message-widget-font {
-fx-font-family: Noto Color Emoji;
}
Applying the CSS to the Text object:
messageText.getStyleClass().add("message-widget-font");
Is there a way to get the emoji's to have colour?

Hover style of a JavaFX MenuItem programatically

I want to know if there is a way to set the hover style of a JavaFX ManuItem without add a stylesheet.
for Node I normally use: this.setOnMouseEntered(e -> this.setStyle(initialStateCss + hoverStateCss));and this.setOnMouseExited(e -> this.setStyle(initialStateCss));
But for MenuItem this events are not available. Someone have an idea of how to do it?
Thanks in advance!
I suggest you to use CSS stylesheets then use the .menu-item:focused selector event. In your CSS file, the markup would look like this:
.menu .menu-item:focused {
-fx-background-color: #FFF6;
-fx-background-radius: 5.0;
-fx-font-weight: bold;
}
I would recommend you to use as little Java style as possible and use CSS stylesheets instead. It would enhance your code as it will be more maintainable.
For more details, this SO request gives useful information about MenuItem's CSS style.

How do i use custom fonts through JavaFx CSS?

I've attempted adding a custom font to my program using CSS and i can't get it to work, is anyone able to help?
This is what i've got at the moment:
#font-face
{
font-family: walterturncoat;
src: url("../resources/fonts/walterturncoat.ttf");
}
.label
{
-fx-font-family: "Walter Turncoat";
-fx-text-fill: #ffffff;
-fx-font-size: 20pt;
-fx-effect: dropshadow(gaussian, #000000, 10, 0, 2, 2);
}
The url works as i used a similar path for an image (just /images/ instead of /fonts/) and i was able to get the font to work outside of CSS in the bulk of the program but i intend to be using it a lot so having it in CSS would be ideal.

How to style JFXtras CalendarTimeTextField?

I want to style the font of the CalendarTimeTextField Popup control using a CSS File.
The following entry doesn't work for me:
.CalendarTimeTextFieldSkin_popup {
-fx-text-fill: white;
-fx-font-style: italic;
}
Setting the font-type works, changing text color doesn't.
Thanks for supporting me
GGK
The text is rendered with a Text node (https://docs.oracle.com/javafx/2/api/javafx/scene/doc-files/cssref.html#text) so use -fx-fill and/or -fx-stroke.

Accessing images with CSS url() in Primefaces

This is from the Primefaces docs:
Button Icons
An icon on a button is displayed using
CSS and image attribute.
<p:commandButton value=”With Icon” image=”disk”/>
<p:commandButton image=”disk”/>
.disk is a simple css class with a
background property:
.disk {
background-image: url(‘disk.png’) !important; }
My question is: where does this url() point to? In other words, where should I put my images so that the CSS could access it?
I've tried /resources, /resources/img, no luck. Yes, it was working with an absolute URL (one that includes the context path), but that makes the application not portable.
Simple solution:
IF you are using JavaServer Faces 2.0 you have chance to put all resources inside specially designed directory. So you need to have this folder structure in web app:
-rootdir
--resources
---images
----disk.png
And to receive image in css background you should type something like this:
.disk {
background: url(#{resource['images/disk.png']}) !important;
}
It looks like your question is more concerned with the client-side aspects of things, so even though I don't know Primefaces, here's a stab at answering it:
CSS paths are relative to the location of where the CSS rule is declared.
If this is in your HTML (in a <style> block), then disk.png has to be in the same directory as your file.
If this rule is in a separate CSS file, then disk.png should be in the directory where the CSS file is.
If you create a directory images, then the directory will be relative from the CSS too.
Hope this helps?
I faced the same problem for a moment and I reckon documentation about it is unclear!
It works this way for Primefaces commandButton and similar (menuItem, submenu, etc):
What I did is:
Download a theme from the theme library in PrimeFaces website (example: aristo or redmond).
Unzip the archive to your web application resources folder, (in my case: root/resources/css/aristo
Notice that in aristo folder you have : theme.css and /images folder where an indexed png image contains all the icons used by the theme.
If you open the theme.css you'll notice that to access an indexed image icon you should call two classes : .ui-icon and .ui-icon-theiconyouwant (the .ui-icon retrieves the index png using #{resource['primefaces.......png']} and .ui-icon-agivenicon delimit the icon by its X & Y position (index) in the png image).
Now output the theme.css into you application, by using **<h:outputStyleSheet />**. Best way to do it is between tags of your template.
So in your xhtml or jsp, do **<p:commandButton image="ui-icon ui-icon-someicon"} />**.
Now if you want to add your personal images to use with a <p:commandButton, create classes in theme.css :
.smiley {
background-image: url("#{resource['images/smiley.png']}") !important; /this will be ignored by Internet Explorer if not compatible/
width: 16px;
height: 16px;
}
Normally JSF accesses the first images folder available within your resources folder.
Now do: <p:commandButton image="smiley" value="Smile" />

Categories