When user presses a button in my JavaFX2.2 UI, a dark blue halo appears to show it was clicked. During the course of events, my program may want to 'unclick' it to show it is no longer selected.
I expected a button.setSelected(false);
I remember Swing used to have this, but there isn't such a call in JavaFx.
This article discusses drop shadows but I want to simply use the same appearance used when a button is clicked which isn't really a drop shadow. setEffect(new DropShadow())
Using setStyle()
is ok, but what values to use?
From a css stylesheet this command also works. There is no need for a toggle button. One you press a different button, focus changes to the new one.
I think for your description, that you actually want a ToggleButton rather than a standard Button.
There is a nice Oracle tutorial on ToggleButtons.
Addressing Items from the Question
The blue halo is actually a focus ring indicating that a control has focus. To focus a control, you can invoke the requestFocus method.
To remove focus from a control, you can call requestFocus on another control to focus on that control instead.
To have a button which can toggle between a selected and unselected state, use a ToggleButton. A
ToggleButton
has a setSelected method.css style values for drop shadow effects are define in the JavaFX CSS Reference Guide.
It is visually equivalent to define the drop shadow effect in code via
setEffect
or via css withsetStyle
or applying a style class from a stylesheet. Of the three approaches, I would never recommend thesetStyle
approach, but only the css from stylesheet or thesetEffect
from code approach.Related
Note there is an additional related property - armed:
A button in an armed state has a slightly different look than one which is not in an armed state. Most programs never need to interact with the armed state of buttons.
Sample for styling the selected state
The standard way to differentiate a selected ToggleButton from one which is not Selected is to darken it to give it a depth style effect and make it appear further away when it has been pushed. Here is the standard toggle button css for JavaFX 2.2:
You can override this default behavior by defining your own stylesheet that provides an alternate definition for the selected state. The sample below will ensure that the selected state display is much more subtle than the standard, only darkening the selected state color a small fraction rather than a lot.
Unselected
Selected
Associated css:
Source file: