I'm using JavaFX version 8.0.40-b27 and trying to embed a custom/external font via CSS. I've also tried programmatic approaches, all of which have failed. A System.out.print of "font" returns null, which I suspect to be the cause.
Java:
Font font = Font.loadFont( Main.class.getClassLoader().getResourceAsStream( "application/stratum.ttf"), 10);
System.out.println(font); // Prints "null"
nowPlayingTitle.setFont(font);
CSS:
#font-face {
font-family: stratum;
src: url('stratum.ttf');
}
.text{
-fx-font-family: "stratum", "Segoe UI Light";
-fx-font-weight: 100;
-fx-text-fill: white;
}
Directory:
http://i.stack.imgur.com/c92ii.png
EDIT:
System.out.println(font); now prints Font[name=StratumNo1-Thin, family=StratumNo1, style=Thin, size=10.0], so the file is being accessed correctly. However the font is still not being rendered on screen: http://i.stack.imgur.com/bueUk.png
For the URL in Java code, try either
// relative to classpath, with leading /
Font font = Font.loadFont( Main.class.getClassLoader().getResourceAsStream( "/application/stratum.ttf"), 10);
or
// relative to class:
Font font = Font.loadFont( Main.class.getClassLoader().getResourceAsStream( "stratum.ttf"), 10);
The CSS looks right to me... are you sure your ttf file is being deployed along with the compiled code as css, etc?
Related
https://i.stack.imgur.com/Y76Fl.png
On this picture on left side - running java program, and right side - Scene builder layout.
This is my github code: https://github.com/captsmile/calc
You can do the following code in .css file
.button
{
-fx-background-color: transparent;
}
This will make the color of button as the color of your application's background color(due to transparency).
Furthermore you may also apply some effects to make your button looks more cool. To add effects
.button:hover
{
-fx-background-color: yellow;
}
.button:pressed
{
-fx-background-color: brown;
}
At the end ,you may attach the case file with your file(suppose index.java file in which you want to apply css) by writing following code in your index.java
scene.getStylesheets().add(getClass().getClassLoader().getResource("application.css").toExternalForm());
Where application.css is the css file with whole css code given above . This application file must be present in your src folder.
I fixed this removing border at GridPane
.root{
-fx-padding: 5;
-fx-border-style: none;
-fx-border-width: 0;
-fx-border-insets: 0;
}
This is how you can do it in scenebuilder
Choose the button by clicking on it.
Then in properties->Style
Choose "-fx-background-color"
and put value as " transparent"
Like this
You can do it by JavaFX CSS selectors on the button.
The answer in this link check it out, and there are also many JavaFX related examples you may interested in:
http://tutorials.jenkov.com/javafx/button.html
In my Enunciate API documentation report, I need to change the font of some comment text to "Courier New" font type.
Is is possible?
Found a way for this feature -
Use Font html tag in the comment, Java doc code reads it as a html tag and displays in courier font.
/* Sample URL - <font face="courier">https://{Hostname}/programs/FireIn</font>, Replace the host name with the respective environment name</br>
*/
Starting with 1.2, Javadoc supports HTML style sheets. You can make these changes to stylesheet.css (located at the root directory of the Javadoc HTML files).
To choose a smaller, sans-serif fonts for the left-hand frames, change this:
#FrameItemFont { font-size: normal; font-family: normal }
To:
#FrameItemFont { font-size: 10pt; font-family: helvetica arial sans-serif }
Customize this yourself for whatever font you want.
I created a JavaFX Maven application with some resources files (in project folder "src/main/resources"). The application works well in NetBeans environment. When the self-contained JAR file is built and I copy it to another folder, however, the application's visual items (ComboBoxes, TextFields, VBoxes, ...) are not displaying correctly (different colors, wrong padding, ...).
For example, this screenshot shows a correct visualization (obtained in Netbeans environment) and this screenshot not. Here, the difference is the color of the ComboBox hovered items (which are white in the wrong version) and the size of the ComboBox bar. These styles are present in a CSS file (in "src/main/resources/styles").
I don't understand why the CSS is not being applied correctly in the deployed JavaFX Maven application. Some styles are still working (such as Buttons styles) but others not (such as ComboBoxes texts).
What should I do to prevent this happening?
UPDATE:
The CSS file is present in the JAR file because some styles (such as Buttons styles) are correctly loaded in the application (outside NetBeans). Here is the code I use to load the Stylesheet into the JavaFX Scene:
scene.getStylesheets().add(this.getClass().getResource("/styles/styles.css").toExternalForm());
The "styles.css" file is saved in "src/main/resources/styles/styles.css".
As an example, the following style is applied in Java code:
Button save = new Button("Save");
save.getStyleClass().add("primary-button"); // -> It works
Styles that do not work include those that do not have a direct styleclass. They affect all ComboBoxes, CheckBoxes, etc.. For instance:
.combo-box-popup {
-fx-min-width: 555px;
}
.combo-box-popup .list-view {
-fx-min-width: 555px;
}
.combo-box-base {
-fx-background-color: transparent, transparent, transparent, white;
-fx-border-width: 1;
-fx-border-color: #666666;
-fx-font-family: "Roboto Light";
-fx-font-size: 14px;
-fx-fill: #666666;
}
.combo-box .arrow {
-fx-background-color: #979797;
-fx-background-radius: 0;
}
.combo-box .arrow-button {
-fx-background-color: #d8d8d8;
-fx-background-radius: 0;
}
.combo-box .arrow-button:hover {
-fx-background-color: #e1e1e1;
-fx-background-radius: 0;
}
.combo-box-popup .list-view .list-cell:filled:selected, .combo-box-popup .list-view .list-cell:filled:selected:hover {
-fx-background-color: #6995df;
}
.combo-box-popup .list-view .list-cell:filled:hover {
-fx-background-color: #f3f3f3;
}
Netbeans IDE version: 8.0.1.
JavaFX version: 8 (from JDK 1.8.0_25).
Computer JRE version: 1.8.0_25.
I hope this helps to understand the problem.
After making some changes in CSS file, I realized that JavaFX 8 has changed some features in CSS (such as backgrounds) and added some new default styles (such as white text in ComboBoxes).
Finally, I changed and added some styles to my CSS file. Now, CSS works well. Thanks!
I m having trouble using JEditorPane.
I want to do left align and right align text on the same line.
Here is my code:
INFO_AREA = new JEditorPane();
INFO_AREA.setBorder(BorderFactory.createCompoundBorder(BORDER,
BorderFactory.createEmptyBorder(10, 10, 10, 10)));
HTMLEditorKit kit = new HTMLEditorKit();
INFO_AREA.setEditorKit(kit);
StyleSheet styleSheet = kit.getStyleSheet();
styleSheet.addRule(".alignleft{color : rgb(0,128,25); font-weight: bold; float: left;}");
styleSheet.addRule(".alignright{color : rgb(0,128,25); font-weight: bold; float: right;}");
INFO_AREA.setText("<html>" +
"<center><b><font size=6>Important Information</font></b></center>"
"<div id=textbox><p class='alignleft'>left</p><p class='alignright'>right</p></div>" +
"</html>");
INFO_AREA.setLocation(305, 10);
INFO_AREA.setSize(275, 200);
INFO_AREA.setEditable(false);
PANE.add(INFO_AREA);
It doesn't work it seems like JEditorPane does not support float.
So maybe anyone has any ideas how I can achieve left align and right align text on the same line?
Swing's HTML renderer is old. It supports HTML 3.2 and CSS 1.0, and it does not even support all capabilities of those specifications.
Looking at the documentation of the CSS class, we see that it lists all of the CSS properties it supports. There is a list of what is supported, following by a list prefaced with "The following are modeled, but currently are not rendered." In that list is the float property.
So, Swing HTML rendering does not currently support the float property. At all.
There is a long-standing open bug, Java Bug ID 4296022, for this issue.
I have a label with style class "test" in my javafx application.
I wanted to add white border around this label, so in the css file I tried:
-fx-border-width: 2;
-fx-border-color: white;
but that didnt worked so then i tried to add:
-fx-border-style: solid;
but that didnt worked either, following javafx css reference I didn't find anything useful.
what am I doing wrong?
Can you try:
System.out.println(label);
it should print something like
Label#1858c80c[styleClass=label]
Is your css class printing too after styleClass=label ... ?
Or can you remove css class of the label and try setting the label style in code directly by:
label.setStyle("-fx-border-color: white;");
if you can see the changes then maybe you are unintentionally overriding css class definiton in css file. Check it.