How do I remove the default border glow of a JavaFX button (when selected)?
Solution 1
To remove the focus ring display from any control from within code:
control.setStyle("-fx-focus-color: transparent;");
To remove the focus ring for all controls, apply a stylesheet:
.root { -fx-focus-color: transparent; }
To only remove the ring for all buttons, use:
.button { -fx-focus-color: transparent; }
I find the -fx-focus-color
attribute setting more straight-forward than relying on some weird combination of insets to remove the focus ring.
In addition, you can use the same setting to change the focus ring to a different color, such as -fx-focus-color: firebrick
.
Update Jan 20, 2015
JavaFX 8 shipped with a new default user agent stylesheet (modena). This new user agent stylesheet shipped with an additional setting for focus highlights: -fx-faint-focus-color
. For Java 8 applications, it is necessary to set both -fx-focus-color
and -fx-faint-focus-color
to transparent to remove all traces of the focus ring. See good4m's answer to this question.
Update Dec 10, 2015
If you only set the focus colors to transparent as previously recommended in this answer, for some controls you may see some subtle differentiation between when a control is focused and when it is not. For many applications this will not be an issue and setting the focus colors to transparent will be sufficient.
For more information and alternate solutions, review James_D's answer to Remove blue frame from JavaFX input field and Jens Deter's blog post about How to get rid of focus highlighting in JavaFX. Be aware that the link to Jens Deter's blog unfortunately has some annoying pop-ups.
Solution 2
-fx-focus-color: transparent;
-fx-faint-focus-color: transparent;
Solution 3
There is several way to do this. You can try any of this.
button.setStyle("-fx-focus-color: transparent;");
or
.button{
-fx-focus-color: transparent;
}
or
.button:focused{
-fx-focus-color: transparent;
}
Solution 4
If you want to remove this focus ring in JavaFX 8, rewrite the :focus selector with the .button selector style from modena.css.
.button:focused {
-fx-background-color: -fx-outer-border, -fx-inner-border, -fx-body-color;
-fx-background-insets: 0, 1, 2;
-fx-background-radius: 5, 4, 3;
}
Solution 5
The answer from Stelios Adamantidis is correct, which is
.button:focused {
-fx-background-insets: 0, 0, 1, 2;
}
Here is my explanation:
For example the definition
-fx-background-color: red, green, deepskyblue, blue;
seems to define four layers of background colors, with red as the color for the backmost layer.
For example the definition
-fx-background-radius: 0, 1, 4, 10;
sets the radius for all corners for each color layer. Here, the red layer has all corners with the radius of 0, the green layer has all corners with the radius of 1 and so on.
For example the definition
-fx-background-insets: -10, 0, 3, 5;
sets the padding for the color layers. You can also set negative values then the color will be around the control.
The default values for a button seem to be something like this:
.button:focused {
-fx-background-color: <blueGlowingColor>, <?>, <?>, linear-gradient(to bottom, <?>, <?>);
-fx-background-insets: -1, 0, 1, 2;
}
Setting the first value of insets to 0 hides the glowing color behind the second color.
More about JavaFX CSS you can find here:
http://docs.oracle.com/javafx/2/api/javafx/scene/doc-files/cssref.html
Titus
I’m a senior software engineer and Top Innovator at PayPal. I’ve architected and launched products used by millions of people worldwide, and can even make a decent cold brew!
Updated on August 04, 2020Comments
-
Titus over 3 years
I'm trying to remove the border glow (please see screenshot below) that appears by default when a JavaFX button is selected:
I also want to do this using CSS, and not declaratively from within the main JavaFX script. However, I am having trouble figuring out what CSS property I need to use (er, set to 0?) in order to remove that border.
-
Alberto over 9 yearsThanks for referencing
-fx-faint-focus-color
-
Saturn over 8 yearsThis needs to be higher up. All other solutions here won't work for JavaFX 8.
-
Admin over 8 yearsThis one seems to come the closest, but the corners on a focused button have a different radius.
-
Asif Mushtaq almost 8 yearsHow I can remove radius too?
-
jewelsea almost 8 years@Unknown, the radius isn't related to the focus ring. See styling a button with css. If the link doesn't answer your question, ask a new question.
-
Asif Mushtaq almost 8 years@jewelsea no body is answering my already asked question. stackoverflow.com/questions/38545939/… That's why I tried to ask in comments. ( Not the same question but have a problem )
-
Leif Gruenwoldt over 7 yearsYa for me just the
-fx-background-color
line was needed. -
Adalberto José Brasaca over 3 yearsBetter answer for JavaFX 8+. May be adjusted in Scene Builder too.
-
Nicolas Gehlert over 3 yearsyes, this should be the accepted answer currently :)
-
Luciano Brum almost 2 yearsDoing this with ListView disable the ability to move the list with keyboard.