JavaFX combobox css styling - css

I'm trying to customize a combo box in JavaFX through css. I can't customize the "arrow button" on the right (I want it to disappear, or to have a custom graphic, for example).
I have been checking the default caspian.css, but no matter what modifications I do to the .combo-box section, the arrow button is not affected.
Any idea of where this can be edited?

Using the following CSS in the style sheet will get rid of all of the ComboBox arrows
.combo-box .arrow, .combo-box .arrow-button{
-fx-background-color: transparent;
}

Use the CSS Analyser option in Scenebuilder to get the CSS of any node that you want to play with
After that just select any node and you'll see all the classes which you can modify using CSS.
Now that I know the class I can make my changes accordingly in my CSS file
.combo-box{
-fx-border-color:#E6E6E6;
-fx-border-style:solid;
-fx-border-width:1;
}
.combo-box .arrow{
-fx-background-color:#2478E9;
}
.combo-box .arrow-button{
-fx-background-color:white;
-fx-border-style:none;
}
.combo-box .arrow-button{
-fx-background-color:white;
}
.combo-box .list-cell{
-fx-background-color:white;
}
Which gives me an end result like this.
For more advanced analysis of CSS and other Events occurring when the application is running, you can try Scenic View.

I wanted it to disappear too. But nothing worked for me on javafx 8 until I tried the following:
css:.combo-box.REFERENCEDONLY .arrow-button {
-fx-padding: 0 0 0 -7;
}
basically it says: use negative padding on the arrow-button's left side to effectively shrink it.
javafx code:myCombobox.getStyleClass().add("REFERENCEDONLY");

Related

JavaFX CSS Styling from Java

I want to assign a style class to an element.
I have a css:
.text-field{
-fx-background-color: #333333;
-fx-background-insets: 0 -1 -1 -1, 0 0 0 0, 0 -1 3 -1;
-fx-text-fill: white;
-fx-prompt-text-fill: white;
-fx-font-family: Trebuchet MS;
-fx-font-size: 14px;
}
.text-field:focused{
-fx-focus-color: white;
-fx-prompt-text-fill: white;
-fx-highlight-fill: grey ;
}
And the java code I´m using:
JFXTextField textField = new JFXTextField();
textField.getStylesheets().add(this.getClass().getResource("/css/TextField_Style_Sheet.css").toExternalForm());
textField.getStyleClass().clear();
textField.getStyleClass().add("text-field");
The problem I´m having is that the ":focused" style is not being applied to this element.
What am I doing wrong?
Already tested it using directly scenebuilder in the element and it seems that the setting in "Focus Color" and "UnFocus Color" options override the text-field:focused style.
As the JFXTextField is created in runtime it seems the default Focus Color overrides the css text-field:focused style.
How can this be solved?
You replace all the style classes of the node with a single style class: text-field.
Take a look at modena.css to see that the style of TextFields is defined in rules for style class text-input.
These rules are also the only place where the CSS variable -fx-focus-color is used for TextFields. Since you make sure those rules are no longer applied you don't see any visible effect when modifying the -fx-focus-color variable.
If you do want to keep the parts of the old style you should not remove the style classes that are added when creating the node and modify properties that don't suit your needs in your own style. If you clear the style classes you're responsible for rebuilding the look from scratch.
Change focused to hover
like this:
.text-field:hover {
..}

How to change the color of menu control in javafx?

in an another question I find this possibilty with css
.menu .label
{
-fx-text-fill: black;
}
but it doesn't work with the setStyle method
menu.setStyle("-fx-text-fill: black");
The CSS applies the style to each Label below a Menu.
Whereas menu.setStyle(...) will apply only to the menu itself. And the menu itself does not have a -fx-text-fill property.
If you change your CSS to:
.menu
{
-fx-text-fill: blue;
}
then it will be the same as your code ... and also stop to show the menu in color.
Menus don't support setting their font color like this. The CSS solution relies on an implementation detail.
If you don't want to do that you must use menu.setGraphic(...) to set a node, e.g:
Menu menuFile = new Menu("");
Label t = new Label("File");
t.setStyle("-fx-text-fill: blue;");
menuFile.setGraphic(t);

CSS doesn't have effect on JavaFX GUI

My CSS code isn't doing anything to the look of the elements for the JavaFX application I have. I've noticed that the lines of code in the CSS document say "Unknown property" and are highlighted in yellow. I tried to uninstall and then reinstall e(fx)clipse but that didn't help. Here's the code
CSS
.header-one {
-fx-stroke-width: 4;
-fx-fill: 99000;
}
Java
Label patronHeader = new Label("Current Patron");
patronHeader.getStyleClass().add("header-one");
What should I do to fix the problem?
In your case, you use css elements, that a Label does not support:
Visit this site for more information on what you can set on Labeled controls:
http://docs.oracle.com/javase/8/javafx/api/javafx/scene/doc-files/cssref.html#labeled
-fx-stroke-width and -fx-fill are not supported CSS properties for a Label.
You need
.header-one {
-fx-text-fill: #990000 ;
}
.header-one .text {
-fx-stroke-width: 4 ;
}

How to change combobox appearence with css

I am working on combobox with javafx 2 and I would like to change the background color of the menu/list in the css. I tried with the following code and some others but nothing work. Do you have any ideas ? Do you think it is possible ?
.combo-box .list-view{
-fx-background-color:linear-gradient(#efefef, #b5b5b5, efefef);
}
.combo-box .cell{
-fx-background-color:linear-gradient(#efefef, #b5b5b5, #efefef);
}
Use
.combo-box-popup .cell{
-fx-background-color:linear-gradient(#efefef, #b5b5b5, #efefef);
}
ok it works, thanks !!!
Also I have change one thing. In a first place, I included the css from the fxml file. Now I include it in the main class :
String css = "application/application.css";
scene.getStylesheets().clear();
scene.getStylesheets().add(css);

how to style a prompt text of combobox in java FX application?

I want to modify the css style of my combobox to reduce the text-size of its promptText.
How can this be done?
If you only want to change size of the prompt text, and not the other text, I don't think there is any way: there's simply no hook into the text node for the prompt text that distinguishes it from the displayed text.
You can change the color via a special css property:
.combo-box .text-field {
-fx-prompt-text-fill: rgba(255, 0, 0, 0.5) ;
}
.combo-box .text-field:focused {
-fx-prompt-text-fill: transparent ;
}
but I don't see any way to change any other style properties.
just add the following codes at your css
.combo-box-base .text{
-fx-fill: rgb(130.0, 130.0, 130.0);
}
Have you checked this out? Try something like:
.combo-box .list-view {
-fx-font-size: xxxx;
}
You may need to specify additional properties/and or additional selectors to achieve the desired effect (e.g. -fx-cell-size). See the relevant section in caspian.css (located inside the jfxrt.jar in the lib folder of your JRE).
A bit of a hack using Scenic View
.combo-box .list-cell {
-fx-font-size: xxxx;
}
As James D said this piece of code work in certain condition only
.combo-box .text-field {
-fx-prompt-text-fill: rgba(255, 0, 0, 0.5) ;
}
.combo-box .text-field:focused {
-fx-prompt-text-fill: transparent ;
}
but only if you active editable property of your combobox.
If you add
.combo-box-base .text
it will apply even for the element of your combobox
If you don't want your combobox editable JavaFx don't take this into account. I think you have to manage it yourself by adding a PseudoClass and activate it yourself ...
Or put your combobox editable but forbid keyboard
Or You'll have to take another ComboBox from another Interface framework
Those solutions are workaround and not definitive solution i hope JavaFx developpers will improve this.
I know this is old, someone might get helped.
For default combo box:
.combo-box{
-fx-prompt-text-fill: #ffffff;
}
For JFoenix Combo box:
.jfx-combo-box {
-fx-prompt-text-fill: #ffffff;
}
You can also style the .list-cell .label .text-field sub-property(ies) if need be.
With this code I change styles of comboboxes: font, font-size, text color etc.
ComboBox<String> comboBox= new ComboBox<String>();
comboBox.setPromptText("Text");
comboBox.setButtonCell(new ListCell<String>(){
#Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
setStyle("-fx-font-size: 17.0");
if(!(empty || item==null)){
setText(item.toString());
}
}
});

Resources