Styling javafx checkbox - java

I've tried to apply the following styles to my CheckBox
.check-box .box {
-fx-background-color: -fx-shadow-highlight-color, -fx-outer-border, -fx-inner-border, -fx-white;
}
.check-box:selected .mark {
-fx-background-color: -fx-colour-white;
}
.check-box:selected .box {
-fx-background-color: -fx-blue;
}
The goal is to have a white checkbox with grey border and when the checkbox is checked to have a blue background and white tick. When I apply these styles I get the result I want. However an odd thing happens when doing the following steps:
1- click on checkbox to select
2- click on checkbox to deselect
The checkbox disappears and will only appear again if I click on somewhere else in the pane.
You can see the images below of the checkbox when it is first rendered then when it is checked.

I do not think that the problem comes from your StyleSheets, I think it's due to a bad manipulation in your Java code, you said :
The checkbox disappears and will only appear again if I click on somewhere else in the pane
So start with your Pane, look at whether there is a related event, and concerning the style, your method is almost correct, this is how I would have proceeded :
.check-box .box {
-fx-background-color: white;
-fx-border-color:grey;
-fx-border-radius:3px;
}
.check-box:selected .mark {
-fx-background-color: white;
}
.check-box:selected .box {
-fx-background-color: #28a3f4;
}
Good luck !

Related

How to disable selected button hover in java-fx

I am trying to make selected button look completely like unselected. All I need is to be able to check whether button was clicked or not. I have tried almost everything but still can't do what I want. Now I have this css. If button is selected its
lower border becomes thicker than others. Is there any possibility to get rid of that, so all the borders of selected button will be the same size?
.button {
-fx-focus-color: transparent;
}
.button:focused {
-fx-focus-color: transparent;
-fx-faint-focus-color: transparent;
}
.button:hover {
-fx-focus-color: transparent;
-fx-faint-focus-color: transparent;
}
This is how it looks like

JavaFX TextArea style with css

I want to know how to change the inside border color that appears in the text area. I want to delete de blue transparent border inside the red border of the text area. I have checked the modena.css file but I can not find the solution.
What I have in my css file:
.text-area:focused {
-fx-background-color: white;
-fx-border-color: rgba(229,0,0,0.3);
}
TextArea has an additional border/background on its content. To change/get rid of it, you need an additonal style in your css.
Something like
.text-area:focused .content {
-fx-background-color: white;
}
not sure if that's safe enough: it is not documented (or at least I could not find any doc for it), only available as implementation, f.i. in the extracted modena.css
I use the following code to get rid of the auto-generated focus effects of a textarea:
* {
-fx-focus-color: transparent;
-fx-border-style: none;
}
or
.textarea {
-fx-focus-color: transparent;
-fx-border-style: none;
-fx-background-radius: 0.0px;
-fx-border-radius: 0.0px;
}
To get rid of all the default styling, which includes a rounded border...

JavaFX 8: Putting a default "x" mark via css if the checkbox is deselected

I want to be able to control my JavaFX checkbox mark shape on states selected and deselected via css.
I can control the mark properties in selected state using this:
.check-box:selected .mark {
-fx-background-color: gb-control-btn-color;
}
How do I do it if it's unselected? Bottomline, I just want X if it's deselected and check (default) if it's selected.
I already tried doing this to no avail:
.check-box:normal .mark {
-fx-shape: "M2,0L5,4L8,0L10,0L10,2L6,5L10,8L10,10L8,10L5,6L2,10L0,10L0,8L4,5L0,2L0,0Z";
-fx-background-color: gb-control-btn-color;
}
Appreciate the help!
There is no pseudo class :normal. Simply leave it off, to style the unselected Checkbox.
.check-box:selected > .box > .mark {
-fx-shape: "M-0.25,6.083c0.843-0.758,4.583,4.833,5.75,4.833S14.5-1.5,15.917-0.917c1.292,0.532-8.75,17.083-10.5,17.083C3,16.167-1.083,6.833-0.25,6.083z";
}
.check-box > .box > .mark {
-fx-shape: "M2,0L5,4L8,0L10,0L10,2L6,5L10,8L10,10L8,10L5,6L2,10L0,10L0,8L4,5L0,2L0,0Z";
-fx-background-color: black;
}
There is an invaluable resource when styling JavaFx components at modena.css, which is a copy of the default JavaFx stylesheet. Search for "checkbox" to see how the default checkbox is styled.

JavaFX ToggleButton set color and restore default color

What is normal background color and border color on toggle button? example:
i.toggle11.setOnAction(e->{
if(i.toggle11.isSelected()){
i.toggle11.setStyle("-fx-background-color:red");
i.toggle12.setStyle("-fx-background-color:white");
i.toggle13.setStyle("-fx-background-color:white");
}
else {
i.toggle11.setStyle("-fx-background-color:white");
}
});
I want to after this action to put other toggles 'normal color'(same when togglebutton was created)
To answer how to add/remove styles
The simplest approach if you color your ToggleButtons by adding a CSS styleclass, then when it is not needed anymore you remove them, therefore it will have the same style as before your formatting.
Example
Code to add
i.toggle11.setOnAction(e->{
if(i.toggle11.isSelected()){
i.toggle11.getStyleClass().add("red-button");
i.toggle12.getStyleClass().add("white-button");
i.toggle13.getStyleClass().add("white-button");
}
else{
i.toggle13.getStyleClass().add("white-button");
}
});
Then Code to remove
i.toggle11.getStyleClass().removeAll("red-button", "white-button");
CSS
.red-button {
-fx-background-color: red;
}
.white-button {
-fx-background-color: white;
}
Just make sure that you have added the stylesheet which contains the CSS styles like:
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
But
I don't know what you want to achieve, but if you want to just simply color your ToggleButton when it is selected, you can also overwrite .toggle-button:selected in your stylesheet and no other styling needed:
.toggle-button:selected {
-fx-background-color: red;
}
It is always the simplest way to overwrite existing CSS classes and pseudoclasses. This is a good starting point to learn working with ToggleButtons and to style them: Using JavaFX UI Controls.
You can for example check the available style classes for .toggle-button here and also you can check the CSS reference guide.

style pressed button in JavaFX

I've got a Button in my FXML file and i give a style to it by below CSS
.button {
-fx-background-color: linear-gradient(#ff5400, #be1d00);
-fx-background-radius: 30;
-fx-background-insets: 0;
-fx-text-fill: white;
}
as you can see the button has a new awesome style, but whenever i click on it, it remains as like as before and you can't understand is it clicked or not...
As I searched, I found one solution in this link: Pressed CSS, but if you notice it is The CSS that is used by Web Browsers and JavaFX does not support it.
so what is the solution? I want my button changes its appearance when the users hit or click it.
You need to add the psuedoclass state pressed to you css and add new css to it, which will differentiate your current button css with that when pressed :
.button:pressed {
// Your new css
}
For changing the style while hovering the button use :
.button:hover {
// Your new css
}
For better understanding of what style you can add for styling the button, you can go through How to make a button appear to have been clicked or selected?
for arrayLists on javaFX, try this on the CSS to change colors on mouse click:
.list-cell:selected { -fx-background-color: yellow; }

Categories