While working with JavaFX, I found the focus border to be hindering on some visual nodes, like buttons and certain panes. Other questions on this topic on SO often suggest adding the following css (overriding the defaults from Modena.css, the default stylesheet for JavaFX 8) to your style file:
-fx-focus-color: transparent;
-fx-faint-focus-color: transparent;
At first glance, this removes the focus borders, but after some use I found that some UI elements were missing more than just that blue glow. After going through modena, I found that this is because of the way a lot of the nodes are drawn: many of them have a background color consisting of multiple boxes stacked on top of each other with different insets and radii, resulting in a border-like look.
I have also found that this background color derives from the previously mentioned properties for drawing a border. Hence setting the colors to transparent has the unintended effect that certain nodes (like panes, comboboxes, ...) show a border when not focused, but not anymore when they are focused, due to the way the background color is derived for the :focused pseudo-class.
Is it possible to remove the focus border (and faint focus border) so they retain the look of an unfocused element when they are in fact focused?
I've compiled a solution where the focus border is removed by combining what I found on SO by overriding more parts of modena: I've chosen to override the properties of the ":focused" pseudo-class by those of the non-focused defaults for button-like and pane-like things (as stated in modena itself). The results are:
.button:focused,
.toggle-button:focused,
.radio-button:focused > .radio,
.check-box:focused > .box,
.menu-button:focused,
.choice-box:focused,
.color-picker.split-button:focused > .color-picker-label,
.combo-box-base:focused,
.slider:focused .thumb {
-fx-background-color: -fx-shadow-highlight-color, -fx-outer-border, -fx-inner-border, -fx-body-color;
-fx-background-insets: 0 0 -1 0, 0, 1, 2;
-fx-background-radius: 3px, 3px, 2px, 1px;
}
.scroll-pane:focused,
.split-pane:focused,
.list-view:focused,
.tree-view:focused,
.table-view:focused,
.tree-table-view:focused,
.html-editor:focused {
-fx-background-color: -fx-box-border, -fx-control-inner-background;
-fx-background-insets: 0, 1;
-fx-padding: 1;
}
.radio-button > .radio, .radio-button:focused > .radio {
-fx-background-radius: 1.0em; /* large value to make sure this remains circular */
-fx-padding: 0.333333em; /* 4 -- padding from outside edge to the inner black dot */
}
.split-menu-button:focused {
-fx-background-color: -fx-shadow-highlight-color, -fx-outer-border;
-fx-background-insets: 0 0 -1 0, 0;
-fx-background-radius: 3, 3;
}
.split-menu-button:focused > .label {
-fx-text-fill: -fx-text-base-color;
-fx-background-color: -fx-inner-border, -fx-body-color;
-fx-background-insets: 1 0 1 1, 2 1 2 2;
-fx-background-radius: 2 0 0 2, 1 0 0 1;
-fx-padding: 0.333333em 0.667em 0.333333em 0.667em; /* 4 8 4 8 */
}
.split-menu-button:focused > .arrow-button {
-fx-background-color: -fx-inner-border, -fx-body-color;
-fx-background-insets: 1, 2;
-fx-background-radius: 0 2 2 0, 0 1 1 0;
-fx-padding: 0.5em 0.667em 0.5em 0.667em; /* 6 8 6 8 */
}
.scroll-bar:focused {
-fx-background-color: derive(-fx-box-border,30%), linear-gradient(to bottom, derive(-fx-base,-3%), derive(-fx-base,5%) 50%, derive(-fx-base,-3%));
-fx-background-insets: 0, 1 0 1 0;
}
.scroll-bar:vertical:focused {
-fx-background-color: derive(-fx-box-border,30%), linear-gradient(to right, derive(-fx-base,-3%), derive(-fx-base,5%) 50%, derive(-fx-base,-3%));
-fx-background-insets: 0, 0 1 0 1;
}
.text-input:focused {
-fx-highlight-fill: -fx-accent;
-fx-highlight-text-fill: white;
-fx-background-color: linear-gradient(to bottom, derive(-fx-text-box-border, -10%), -fx-text-box-border),
linear-gradient(from 0px 0px to 0px 5px, derive(-fx-control-inner-background, -9%), -fx-control-inner-background);
-fx-background-insets: 0, 1;
-fx-background-radius: 3, 2;
-fx-prompt-text-fill: transparent;
}
.text-area:focused .content {
-fx-background-color:
linear-gradient(from 0px 0px to 0px 4px, derive(-fx-control-inner-background, -8%), -fx-control-inner-background);
-fx-background-radius: 2;
}
.titled-pane:focused > .title > .arrow-button > .arrow {
-fx-background-color: -fx-mark-highlight-color, -fx-mark-color;
-fx-background-insets: 1 0 -1 0, 0;
}
.color-picker.split-button:focused > .arrow-button {
-fx-background-color: -fx-outer-border, -fx-inner-border, -fx-body-color;
-fx-background-insets: 1 1 1 0, 1, 2;
-fx-background-radius: 0 3 3 0, 0 2 2 0, 0 1 1 0;
}
Basically what it does it change the rendering of the background color to draw the same look and feel whether the UI node has focus or not.
Related
Is it possible to add space between the button and its border? I want the black color to fill the button but not completely it should leave out a little spacing between the border.
The button layout is width:200, height:40
The CSS used:
.button:focused, .button:hover{
-fx-text-fill: aquamarine;
-fx-border-color: -fx-text-fill;
-fx-border-radius: 50px;
-fx-background-radius: 50px;
-fx-scale-x: 1.1;
-fx-scale-y: 1.1;
-fx-background-color: #34495E;
}
.button:hover{
-fx-cursor: hand;
-fx-background-color: black;
}
.button{
-fx-font-size: 20px;
-fx-background-radius: 5 5 5 5;
-fx-text-fill: #f0f0f0;
-fx-border-color: -fx-text-fill;
-fx-border-radius: 5 5 5 5;
-fx-border-width: 2px;
-fx-background-color: #34495e;
}
You can specify multiple backgrounds in JavaFX CSS, which are rendered one on top of the other in the order they are specified. Each background can have a corresponding insets, enabling you to see a portion of the backgrounds which come before it.
So, to leave a gap between the border and the background, you can specify a transparent background before the "main" background, with a positive insets for the latter:
.button {
-fx-background-color: transparent, black ;
-fx-background-insets: 0, 5 ;
}
I would like to apply the default "focus" style tab to my selected tab. Mainly I am trying to accomplish keeping the thin blue border around the tab regardless of whether or not the user is focused on the tab, but instead as long as it is the tab they have selected. I included an example of how it looks now when I am focused on something else and how I would like it to look when I am focused on something else.
/*Overrides the default and removes the 10px padding from the left most tab */
.tab-pane:top *.tab-header-area {
-fx-background-insets: 0, 0 0 1 0;
-fx-padding: 0.416667em 0.166667em 0.0em 0.0em; /* overridden as 5 2 0 0 */
}
.tab-header-background {
-fx-background-color: transparent;
}
.tab-pane{
-fx-tab-min-width:90px;
}
.tab-pane .tab{
-fx-background-insets: 0 0 0 0,0,0;
-fx-background-color: #c6c6c6;
}
.tab-pane .tab:selected{
-fx-background-color: #3c3c3c;
}
.tab .tab-label {
-fx-alignment: CENTER;
-fx-text-fill: #828282;
-fx-font-size: 12px;
-fx-font-weight: bold;
}
.tab:selected .tab-label {
-fx-alignment: CENTER;
-fx-text-fill: #96b946;
}
In my modena.css that selector looks like:
.tab-pane:focused > .tab-header-area > .headers-region > .tab:selected .focus-indicator {
...
}
Just use it without :focused:
.tab-pane > .tab-header-area > .headers-region > .tab:selected .focus-indicator {
-fx-border-width: 1, 1;
/*-fx-border-color: -fx-focus-color, -fx-faint-focus-color;*/
-fx-border-color: red;
-fx-border-insets: -4 -4 -6 -5, -2 -2 -5 -3;
-fx-border-radius: 2, 1;
}
Hi guys, do you know howto edit the TextField border-color and border-width (currently a thin red border) within the DatePicker control? ? BTW where can I find good reference to available CSS items of JavaFX controls?
Cheers!
In your CSS file change this properties (from modena.css):
.date-picker > .text-field {
-fx-background-color:
linear-gradient(to bottom, derive(-fx-text-box-border, -10%), -fx-text-box-border),
linear-gradient(from 0px 0px to 0px 5px, derive(-fx-control-inner-background, -9%), -fx-control-inner-background);
-fx-background-insets: 1 0 1 1;
-fx-background-radius: 2 0 0 2;
}
.date-picker:focused > .text-field,
.date-picker > .text-field:focused {
-fx-background-color:
-fx-control-inner-background,
-fx-faint-focus-color,
linear-gradient(from 0px 0px to 0px 5px, derive(-fx-control-inner-background, -9%), -fx-control-inner-background);
-fx-background-insets: 1 0 1 1, 1 0 1 1, 3 2 3 3;
-fx-background-radius: 2 0 0 2, 1 0 0 1, 0;
}
Good reference: JavaFX CSS Reference Guide
I wonder how to apply CSS settings to a TitledPane, but can't find any example
I would like to apply custom settings for TitledPane toolbar and background, but this does not work
.titled-pane
{
-fx-background-color: linear-gradient(aliceblue, lightslategray);
}
Default css of titled pane...change it according your need.
.titled-pane
{
-fx-skin: "com.sun.javafx.scene.control.skin.TitledPaneSkin";
-fx-text-fill: -fx-text-base-color;
}
.titled-pane:focused
{
-fx-text-fill: white;
}
.titled-pane > .title
{
-fx-background-color: -fx-box-border, -fx-inner-border, -fx-body-color;
-fx-background-insets: 0, 1, 2;
-fx-background-radius: 5 5 0 0, 4 4 0 0, 3 3 0 0;
-fx-padding: 0.166667em 0.833333em 0.25em 0.833333em; /* 2 10 3 10 */
}
.titled-pane:focused > .title
{
-fx-color: -fx-focus-color;
}
.titled-pane > .title > .arrow-button
{
-fx-background-color: null;
-fx-background-insets: 0;
-fx-background-radius: 0;
-fx-padding: 0.0em 0.25em 0.0em 0.0em; /* 0 3 0 0 */
}
.titled-pane > .title > .arrow-button .arrow
{
-fx-background-color: -fx-mark-highlight-color, -fx-mark-color;
-fx-background-insets: 1 0 -1 0, 0;
-fx-padding: 0.25em 0.3125em 0.25em 0.3125em; /* 3 3.75 3 3.75 */
-fx-shape: "M 0 0 h 7 l -3.5 4 z";
}
.titled-pane:collapsed > .title > .arrow-button .arrow
{
-fx-rotate: -90;
}
.titled-pane > *.content
{
-fx-background-color:
-fx-box-border,
linear-gradient(to bottom, derive(-fx-color,-02%), derive(-fx-color,65%) 12%, derive(-fx-color,23%) 88%, derive(-fx-color,50%) 99%, -fx-box-border);
-fx-background-insets: 0, 0 1 1 1;
-fx-padding: 0.167em;
}
.titled-pane:focused > .title > .arrow-button .arrow
{
-fx-background-color: white;
}
Only the following properties are available for titled pane( which include all labeled properties and font properties)
extra properties
-fx-animated
-fx-collapsible
labeled properties
-fx-alignment
-fx-text-alignment
-fx-text-overrun
-fx-wrap-text
-fx-font
-fx-underline
-fx-graphic
-fx-content-display
-fx-graphic-text-gap
-fx-label-padding
-fx-text-fill
-fx-ellipsis-strin
font properties
-fx-font
-fx-font-family
-fx-font-size
-fx-font-style
-fx-font-weight
The official documentation for CSS styling Titled Panes can be found here (see #siddharth gupta's answer regarding possible properties).
You may consider to style only a certain sub-component, e.g., .titled-pane > .title > .text. Titled panes have the following layout:
titled-pane - TitledPane
title — HBox
text — Label
arrow-button — StackPane
arrow — StackPane
content — StackPane
Here is the default formatting for the TitledPane's "header":
.titled-pane > .title
{
-fx-background-color: -fx-box-border, -fx-inner-border, -fx-body-color;
-fx-background-insets: 0, 1, 2;
-fx-background-radius: 5 5 0 0, 4 4 0 0, 3 3 0 0;
-fx-padding: 0.166667em 0.833333em 0.25em 0.833333em; /* 2 10 3 10 */
}
I want to have a textfield with three different borders for three cases:
a white border when not hovered or focused
a grey border when hovering
a blue border when focused and typing
I started like this:
#custom-text-field {
-fx-border-width: 2;
-fx-border-color: white;
}
#custom-text-field:hover{
-fx-border-width: 2;
-fx-border-color: #909090;
}
#custom-text-field:focused{
-fx-border-width: 2;
-fx-border-color: #0093EF;
}
The problem is that the border for focusing never shows up. How do it set it correctly?
I use it like this
.custom-text-field {
-fx-background-color:
#FFFFFF,
#FFFFFF;
-fx-background-insets: 0, 2;
-fx-background-radius: 0, 0;
}
.custom-text-field:focused {
-fx-background-color:
#0093EF,
#FFFFFF;
}
.custom-text-field:hover {
-fx-background-color:
#909090,
#FFFFFF;
}
.custom-text-field:focused:hover {
-fx-background-color:
#0093EF,
#FFFFFF;
}