How to change combobox appearence with css - 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);

Related

Angular Material mat-spinner custom color

Does anyone know how can I change mat-spinner color in Angular Material?
Overriding css doesn't work. I tried changing color in material files but they can only be imported, I can't change anything there.
I want it to be my custom color, not color from prebiult-themes.
Use this code for ** < mat-spinner >** add this code in your .css file
.mat-progress-spinner circle, .mat-spinner circle {
stroke: #3fb53f;
}
This answer will work for those who're looking for a flexible solution in Angular 4 / 6 / 7. If you wan't to change the color of a mat-spinner at a component level, you'll need to use the ::ng-deep selector. Knowing this, the solution is quite easy.
In your html file:
<div class="uploader-status">
<mat-spinner></mat-spinner>
</div>
In your css / scss file:
.uploader-status ::ng-deep .mat-progress-spinner circle, .mat-spinner circle {
stroke: #000000;
}
Notice that the .uploader-status css class encapsulates the component. You could just use ::ng-deep without using a class but then whatever changes you're doing to the mat-spinner will appear in other areas of the application. Check this to learn more.
Easy Fix!
Add custom css rules inside styles.css instead of component.css file
.mat-progress-spinner circle, .mat-spinner circle {
stroke: #2A79FF!important;
}
To your .css/.scss component file style add (it will works locally - in component only)
:host ::ng-deep .mat-progress-spinner circle, .mat-spinner circle {
stroke: #bada55;
}
If you don't want to mess around with the global css and need a way to set the spinner to different colors in different areas of your app, I would strongly recommend to create a directive for it.
import { Directive, Input, ElementRef, AfterViewInit } from '#angular/core';
#Directive({
selector: "[customSpinner]"
})
export class CustomSpinnerDirective implements AfterViewInit{
#Input() color: string;
constructor(
private elem: ElementRef
){}
ngAfterViewInit(){
if(!!this.color){
const element = this.elem.nativeElement;
const circle = element.querySelector("circle");
circle.style.stroke = this.color;
}
}
}
Then the spinner should work like this:
<mat-spinner diameter="22" customSpinner color="#fff"></mat-spinner>
mat-spinner html code :
<mat-spinner color="accent" diameter="20" class="loading"></mat-spinner>
And now sass code :
.mat-spinner {
::ng-deep circle {
stroke: #33dd82;
}
}
Color is build in.
Theming
The color of a progress-spinner can be changed by using the color property. By default, progress-spinners use the theme's primary color. This can be changed to 'accent' or 'warn'.
https://material.angular.io/components/progress-spinner/overview
example
<mat-spinner color="warn"></mat-spinner>
I think the key here is that is must be in the GLOBAL styles.css file. The below solution does work if placed there (should be the CSS file affected when material was added to the project if added with ng add:
.mat-progress-spinner circle, .mat-spinner circle {
stroke: #b68200;
}
Of course you could also add classes to the component and specify different selectors if you want distinctly styled spinners. However, it seems the classes must be in the global CSS file.
Late to the game, but this worked well in my .scss file today...
.parent-element-class {
::ng-deep
.mat-progress-spinner,
.mat-spinner {
circle {
stroke: white;
}
}
}
In your styles.css file, add...
::ng-deep .mat-progress-spinner circle, .mat-spinner circle {
stroke: #2A79FF!important;
}
As you might have guessed, I have just made a simple modification to Nitin Wahale's answer. I have prefixed his answer with ::ng-deep and it worked in my case as I had the same issue.
I hope this helps somebody
By default angular material would give your spinner default color of primary.
You can use 3 colors available in pallet that would be primary, accent, warn.
However, if your needs are of different color please consider anyone of the below options.
Easy way(not recommended)
You can use any of method to override css forcefully mention in other answers. I would recommend using parent class above spinner element if you do not want spinner to be of same color throughout the application.
The correct and recommended approach would we to use custom-theme for material. If you already have custom you can just
do like creating a custom mixin called
//here $primary-color is the color you want your spinner to be
#mixin spinner-custom-theme($primary-color, $accent-color, $warn-color) {
$custom-spinner-theme-primary: mat-palette($primary-color);
$custom-spinner-theme-accent: mat-palette($accent-color, A200, A100, A400);
$custom-spinner-theme-warn: mat-palette($warn-color);
$custom-spinner-theme: mat-light-theme($custom-theme-primary, $custom-theme-accent, $custom-theme-warn);
#include mat-progress-spinner-theme($custom-spinner-theme);
}
Now go to file where #include angular-material-theme($custom-theme);
is written
and #include your mixin just below the #include angular-material-theme($custom-theme);
To know more on how to create custom theme you can check this blog here
Sample Color, strokeWidth, diameter and title
<mat-spinner strokeWidth="24" [diameter]="85" color="warn" title="Tooltip text here"></mat-spinner>
In your css file mention like below:
::ng-deep.mat-progress-spinner circle,.mat-spinner circle {stroke: #f2aa4cff !important;}
Here, ::ng-deep will be used to force a style.
!important here what says is that "this is Important",you ignore all other rules and apply this rule.
.mat-mdc-progress-spinner { --mdc-circular-progress-active-indicator-color: white; }
This worked for me using Angular 15.
This is best achieved by doing a custom theme.
https://material.angular.io/guide/theming
use this code
<md-progress-circular md-diameter="20px"></md-progress-circular>
md-progress-circular path {
stroke: purple;
}
In case you guys want to customize each spinner on your webpage. You can do it this way:
svg .mat-progress-spinner circle, .mat-spinner circle {
stroke: inherit;
}
And now on mat-spinner add class:
<mat-spinner class="custom-spinner-color"></mat-spinner>
And in css file:
.custom-spinner-color {
stroke: #234188;
}
That was what I wanted to achieve. I suppose if you look for this question you probably want the same.
Mat progress spinner custom timer, I changed to 3 different colors based on the value passed to mat spinner. Pls refer : https://material.angular.io/components/progress-spinner/examples
<mat-progress-spinner class="mat-spinner" [color]="progressColor"
[diameter]="170" [strokeWidth]="14"[mode]="'determinate'"
[value]="progressLabel">
</mat-progress-spinner>
Ts file
timer: number = TIMER; // say 60 seconds
progressColor: ThemePalette = 'accent';
timerPercent: number = 0;
progressLabel: number = 100;
startTimer() {
this.timer = TIMER;
this.timerInterval = setInterval(() => {
if (this.timer <= 0) {
clearInterval(this.timerInterval);
this.timerFinish();
}
if (this.timer > 0) {
this.progressColor =
this.timerPercent > 69
? 'warn'
: this.timerPercent > 49
? 'primary'
: 'accent';
this.timer--;
this.timerPercent = (100 * (TIMER - this.timer)) / TIMER;
this.progressLabel = 100 - this.timerPercent;
}
}, 1000);
}
For me this is how I do it clean without messing with anything globally:
in my .css
::ng-deep .customColorSpinner circle {stroke: #4e1e1e!important;}
in my .html
<mat-spinner class="customColorSpinner"></mat-spinner>
You can use a custom Angular Directive to solve this problem. The directive allows you to set a custom color on the mat-spinner like this:
<mat-progress-spinner spinnerColor="#09ff00"></mat-progress-spinner>
I have an article here where I explain this and thoroughly show you how to solve it
In component.scss where your mat-spinner exists, just add this :
::ng-deep .mat-mdc-progress-spinner {
--mdc-circular-progress-active-indicator-color: #7D469A;
}

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 refer to an anchor pane in css?

is there a possibility to refer to an anchor pane in css?
If the anchor pane happens to be the root, than it's ok:
.root{
-fx-background-image: url("xxx.yy");
}
It works. But if not, then how to do this? I tried .anchor-pane{}, it didn't work. Then I read, that anchor pane has everything that Pane has. So I tried .pane{} too... It didn't work.
How can I set the background to a none root anchor pane?
Thank you!
You can always assign a css class and add it to the AnchorPane explicitly
anchorPane.getStyleClass().add("pane");
In your css :
.pane{
-fx-background-image: url("xxx.yy");
}
You can perform the same by adding a css id
anchorPane.setId("pane");
In you css :
#pane{
-fx-background-image: url("xxx.yy");
}
This answer is the same as Itachi's I just wrote it at the same time..
You use CSS selectors in a CSS stylesheet to select nodes.
A pane is a node. A node can have a css id set on it node.setId("xyzzy-id"), or it can have style classes set on it node.getStyleClass().add("xyzzy-class").
For the provided examples you could select the pane in either of these ways:
Select by ID:
#xyzzy-id {
-fx-background-color: palegreen;
}
Select by Class:
.xyzzy-class {
-fx-background-color: papayawhip;
}
You can also set FXML attributes on the node for the id and style class (rather than doing this in code as explained above). SceneBuilder has fields for this, so if you are writing FXML using SceneBuilder, just fill in the appropriate fields and it will add the required attributes to your FXML file.

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());
}
}
});

JavaFX combobox css styling

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");

Resources