some css properties are not applied while using javafx 8 - css

When I was using jdk1.7 and javafx 2 my css was working properly but when I upgraded my jdk version to 1.8 some css properties are working and some are not.
For example as in fxml I have used button and its style class as .newRedButton which is defined in css. As in .newRedButton various properties are defined like padding background-color etc.so in my case only 3 properties are applied padding,size and text-fill,rest of the properties(background-image) are not applied. below is the css along with fxml
.newRedButton {
-fx-padding: 4 0 2 0;
-fx-background-color: #c1272d;
-fx-background-radius: 3px;
-fx-font-family:'QuicksandBold-Regular';
-fx-font-size: 14;
-fx-text-fill:#c1272d;
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.HBox?>
<fx:root type="javafx.scene.layout.VBox" fx:id="noCardSelectedRegion" xmlns:fx="http://javafx.com/fxml">
<padding>
<Insets top="156"/>
</padding>
<HBox alignment="CENTER">
<HBox fx:id="thumbnailBox" styleClass="thumbnailBox" prefWidth="65" prefHeight="65"
alignment="CENTER"/>
</HBox>
<HBox fx:id="flightPlannerMsgBox" styleClass="flightPlannerMsgBox" alignment="CENTER">
<Label fx:id="flightPlannerMsg" prefWidth="300" styleClass="flightPlannerMsg"
text="Insert SD card"/>
</HBox>
<HBox fx:id="buttonGroup" styleClass="buttonGroup" spacing="8" alignment="CENTER">
<Button fx:id="buttonSelectSDCard"
defaultButton="false"
styleClass="newRedButton"
text="SELECT CARD"
onAction="#onSelectCard"
minWidth="120"
prefWidth="150"
prefHeight="35"/>
</HBox>
</fx:root>

Related

How to add Hover to ToggleButton in Fxml file (Java17)

<ToggleButton fx:id="toggleButton" text="xxxxxxxx" style="-fx-background-color: transparent;">
<tooltip>
<Tooltip text="xxxxxxx"></Tooltip>
</tooltip>
</ToggleButton>
How can highlight it`s background blue when mouse is on it?
I tried to use css but since i don`t really know the language im not sure if im doing it properly.
You would have to define the styling for the hover action in an external styles.css file as suggested by James_D. Simply apply toggle button style like below.
Attach a stylesheet with your container or the control in the .fxml file (scene builder can bee used):
<VBox alignment="CENTER" prefHeight="480.0" prefWidth="640.0" spacing="20.0" stylesheets="#../../../styles.css" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.javafxdemo.HelloController">
<StackPane fx:id="myPane" prefHeight="150.0" prefWidth="200.0">
<children>
<ToggleButton fx:id="myToggleButton" mnemonicParsing="false" prefHeight="26.0" prefWidth="137.0" text="My toggle button" />
</children></StackPane>
</VBox>
styles.css file:
.toggle-button {
/* default color here */
}
.toggle-button:hover {
-fx-background-color: #178dfc;
}

set JavaFX .root styles in css

I want to apply several styles (font, font-size) to my components initially via css-file.
(I am using the SceneBuilder 2.0 from oracle.)
So I added my css-file to the top component.
My css-File looks like this:
Application.css
.root {
-fx-font: 14px Arial;
}
.button {
-fx-font: 28px Arial;
}
My Application.fxml looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="240.0" prefWidth="320.0" stylesheets="#Application.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="MyText" />
<Label text="MyLabel" />
<Button mnemonicParsing="false" text="MyButton" />
</children>
</VBox>
now I add a Text-Element or Label-Element to my pane, the font doesn't get affected by the root node. But if I add a Button-Element to my pane, the style for .button gets applied.
I expected the .root node would affect the style of any other elements. like in this answered question but it did not work:
Set Font globally in JavaFX
can anyone maybe explain what am I doing wrong?
the only way to archive somthing similar is: setting the style directly as:
-fx-font: 14px Arial;
under Style which i wish i could store in a css file.
I deleted my previous answer. It was not accurate and it was a little confusing.
A container control doesn't have a property -fx-font. So when you define:
.root {
-fx-font: 14px Arial;
}
it has no effect at all.
If you want to set the font for all children controls you should do:
.root * {
-fx-font: 14px Arial;
}
or if you want to change only specific controls of a container:
.table-view .label {
-fx-font: 14px Arial;
}
or all labels of the application:
.label {
-fx-font: 14px Arial;
}

Remove insets in JavaFX TitledPane with CSS not working

Based on a solution of James_D (How to set/remove insets in JavaFX TitledPane) that I've tried, it seems that removing insets from a JavaFX TitledPane through CSS does not work? It does update correctly in Scene Builder, but at runtime the insets remain unchanged. Even Scenic View 8.0 reports a padding of 9.6.
FXML example:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" stylesheets="#newCascadeStyleSheet.css" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="test3.FXMLDocumentController">
<children>
<Accordion layoutX="14.0" layoutY="14.0" prefHeight="270.0" prefWidth="320.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<panes>
<TitledPane animated="false" text="untitled 1">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<Button fx:id="button" layoutX="9.600000381469727" layoutY="9.600000381469727" prefHeight="124.0" prefWidth="318.0" text="Click Me!" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
</content>
</TitledPane>
<TitledPane animated="false" text="untitled 2">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</TitledPane>
<TitledPane animated="false" text="untitled 3">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</TitledPane>
</panes>
</Accordion>
</children>
</AnchorPane>
CSS:
.titled-pane {
-fx-text-fill: rgb(0,100,157);
}
.titled-pane:focused {
-fx-color: -fx-base;
-fx-text-fill: white;
}
.titled-pane > .title {
-fx-text-fill: rgb(0,100,157);
-fx-font-weight: bold;
}
titled-pane > .title > .label{
-fx-text-fill: rgb(0,100,157);
-fx-font-weight: bold;
}
.titled-pane:focused > .title {
-fx-color: rgb(0,100,157);
-fx-text-fill: white;
}
.titled-pane > .title:hover {
-fx-color: lightgrey;
}
.titled-pane > * > * > AnchorPane {
-fx-padding: 0px ;
}
View in Scene Builder: (Preview)
View at runtime:
So it seems that the padding is not applied for some reason. In my main application I use a lot of Accordion containers. The other option was to add the padding in FXML code to the AnchorPane of the TitledPane, this works but is a time consuming job. Am I missing something in the CSS?
Set the -fx-padding to 0 on the following style classes.
.titled-pane .content AnchorPane {
-fx-padding: 0px;
}
To extend #NDY's answer:
If you do not use AnchorPane as the TitledPane's content, but some other node, you have to adjust the CSS accordingly.
An universal CSS solution for removing padding on a TitledPane's content, no matter which type of Node is used as content, is the following:
.titled-pane > .content > * {
-fx-padding: 0px;
}
This sets the padding for any child of the TitledPane's content to 0px.

Add a label on top of an imageview that is on a TilePane

I was making an app that allows users to create custom shops from a game graphically but I was having troubles as I wanted to add a label which shows how much of an item is in a shop but since the imageview is already on a Tile pane, it creates a label beside it but I wanted to create a label on top of it.
You could make your items labels and apply the images as graphic nodes to the labels. When the items are selected, you can invoke label.setText("text string") on the label to change the label's text or css style as needed. If the items need to be interactive, you might want to make the buttons instead of labels.
The approach below has a bit of trickery in it by using a combination of contentDisplay="TOP" graphicTextGap="-40.0" and -fx-padding: 0 0 20 0; to position text, which is a little counterintuitive.
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<StackPane id="StackPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="-1.0" prefWidth="-1.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2">
<children>
<TilePane hgap="10.0" prefColumns="3" prefHeight="-1.0" prefRows="1" prefWidth="-1.0" style="-fx-background-color: linear-gradient(palegreen, forestgreen);
-fx-padding: 20px;" vgap="10.0">
<children>
<Label contentDisplay="TOP" graphicTextGap="-40.0" style="-fx-font-size: 20px;
-fx-text-fill: gold;
-fx-padding: 0 0 20 0;" text="Roast Beef">
<graphic>
<ImageView fitHeight="0.0" fitWidth="0.0" mouseTransparent="true" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="http://icons.iconarchive.com/icons/rockettheme/ecommerce/256/piggy-bank-icon.png" backgroundLoading="true" />
</image>
</ImageView>
</graphic>
</Label>
<Label contentDisplay="TOP" graphicTextGap="-40.0" style="-fx-font-size: 20px;
-fx-text-fill: gold;
-fx-padding: 0 0 20 0;
-fx-border-color: palegoldenrod;
-fx-border-width: 5px;
-fx-border-radius: 10px;
-fx-border-insets: -5px;
" text="Bag 'o' Love">
<graphic>
<ImageView fitHeight="0.0" fitWidth="0.0" mouseTransparent="true" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="http://icons.iconarchive.com/icons/rockettheme/ecommerce/256/bag-icon.png" backgroundLoading="false" />
</image>
</ImageView>
</graphic>
</Label>
<Label contentDisplay="TOP" graphicTextGap="-40.0" style="-fx-font-size: 20px;
-fx-text-fill: gold;
-fx-padding: 0 0 20 0;" text="Show Me">
<graphic>
<ImageView fitHeight="0.0" fitWidth="0.0" mouseTransparent="true" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="http://icons.iconarchive.com/icons/rockettheme/ecommerce/256/wallet-icon.png" backgroundLoading="true" />
</image>
</ImageView>
</graphic>
</Label>
</children>
</TilePane>
</children>
</StackPane>
<!-- icon usage license:
Creative Commons with attribution,
back link to http://www.rockettheme.com required -->
This is perhaps not the best way to achieve what you want, as you could create a custom ShopItem component derived from Region. With the custom component, you would provide layout logic to internally layer your text (and other graphics as needed) over the top of your item image. This layout logic should be a bit more intuitive than the mechanism provided above.
Another approach would be to make each shop item an AnchorPane which contained the image and text label.
The custom component approach, though more flexible, is a bit more complicated, so I just provided the simple label based solution here.

Button style with image

I was wondering How one can achieve following button style where text resides underneath of image in JavaFx?
I tried a lot but all in vain. Any help would be appreciated.
The key is the contentDisplay property, set it to "TOP".
With fxml:
<Button contentDisplay="TOP" layoutX="101.0" layoutY="51.0" mnemonicParsing="false" text="Button">
<graphic>
<ImageView mouseTransparent="true" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#image.png" preserveRatio="false" smooth="false" />
</image>
</ImageView>
</graphic>
</Button>
Or CSS:
.your-selector {
-fx-content-display: top;
}
Check the CSS reference here.
You can simply achieve that just by doing
Button b = new Button("text", graphics);
b.setContentDisplay(ContentDisplay.TOP);
And for cool icons, take a look at http://fxexperience.com/controlsfx/features/ it include icone from FontAwesome and IcoMoon

Resources