How do i use custom fonts through JavaFx CSS? - java

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.

Related

How do I make a media-query for a canvas made in Java?

Media queries in CSS file has no effect on the GUI made in javaFX.
I have made a canvas using javaFX. I have used FXML to setup the different GUI elements. Then I have used a CSS file to style everything. The problem now is, that I want to make it responsive. When I try to use media queries in the CSS file, nothing happens.
.userMenu {
-fx-max-height: 30;
-fx-max-width: 130;
-fx-background-color: rgba(255, 255, 255, 0.5);
-fx-padding: 10px;
-fx-background-radius: 12px;
}
#media only screen and (min-width: 300px) {
.userMenu {
-fx-max-height: 150;
-fx-max-width: 300;
-fx-background-color: rgba(255, 255, 255, 0.5);
-fx-padding: 10px;
-fx-background-radius: 12px;
}
}
I expect the userMenu box to rescale when crossing 300px width.
Just found this when searching myself and the simple answer is: You don't.
From the official docs:
While the JavaFX CSS parser will parse valid CSS syntax, it is not a fully compliant CSS parser. One should not expect the parser to handle syntax not specified in this document.
#-keyword statements are ignored.
I guess one needs to write Java code for that ¯\_(ツ)_/¯
Edit:
After looking at the JavaFX 8 docs there are two supported #-rules: #import and #font-face. But that's all.

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.

How to add css to a JavaFx element

I'm trying to add a an external .css file to a Java FX scene graph as follows:
File f = new File("../theming/css/test.css");
scene.getStylesheets().clear();
scene.getStylesheets().add("file:///" + f.getAbsolutePath().replace("\\", "/"));
test.css
.custom-background {
-fx-background-color: #1d1d1d;
-fx-background-color: red;
-fx-padding: 15;
-fx-spacing: 10;
}
.label {
-fx-font-size: 11pt;
-fx-font-family: "Segoe UI Semibold";
-fx-text-fill: white;
-fx-opacity: 0.6;
}
The style classes get added well, except where I try to add a custom class to an element:
Hbox hbox = new HBox();
hbox.setSpacing(10);
hbox.setMinSize(400, 300);
hbox.getStyleClass().add("custom-background");
That doesn't get picked up.
What could I be doing wrong?
Thank you in advance.
Don't try to convert the file name to a URL yourself. Instead use the build in methods of the File class:
scene.getStylesheets().setAll(f.toURI().toURL().toExternalForm());
This assumes the file is located at the specified path relative to the current working directory when the application is run. In most cases using a relative file path is a bad idea, since running from a different directory will break the program. It would be preferable to include the css file as resource.

How to change font on css?

I'm trying to manipulate the font in Agenda from jfxtras (javafx).
I have a css file linked to it.
I figured out how to make it black and bold as well.
.AgendaText {
-fx-text-fill: BLACK;
-fx-font-weight: bold;
}
But I don't know how to change the font to say, Arial.
Thanks,
Browser will try to take first font, second font and if it wont be able to find it it will serve a sans serif system font
.AgendaText {
font-family: arial, helvetica, sans-serif
}

JavaFX doesn't change the font of its components

I specified a font in a style .css file:
-fx-background-color: #1e1c1e;
-fx-font-family: "Segoe UI", Helvetica, Arial, sans-serif;
but IDEA underlines the second line:
"mismatched property value null".
Then I tried to set a font in the following way:
inputField.setFont(Font.font("Segoe UI"));
All the same. Font won't change. I'd like to ask your help because I'm stucked with this issue.

Categories