2
I have a Choice-box. In my Choice-box there is a list of colors to be selected by the user through the interface. Every time the user selects a color, the background of that same Color Box gets the same color as the user selected. By default, the font color of the items in the Choice-box is white, but among the options for the user to choose, there is the white color... so when he selects "White", the Choice-box background also turns white... and as the default font is white, you already know... everything turns white, "no text".
Here as it stands:
In the Stylesheet file, referring to what I said, the following:
.choice-box {
-fx-background-color: black;
-fx-mark-color: #950005;
-fx-border: 1px;
}
.choice-box > .label {
-fx-text-fill: white;
}
Up there, as you can see, the font color of the Choice-box list item is set by default as white in the second class of CSS, that is, in the subclass "label" within the ". Choice-box".
When the guy selects a color item in the menu, the following action is executed in the "Onaction" of the Choice-box (after doing all the blablabla color checking):
choiceBox.setStyle("-fx-background-color: " + selectedColor + ";");
So far so good, the Choice-box changes color everything fillet, but I want to put the following condition, to avoid the white font on a light/white background:
if(selectedColor.equalsIgnoreCase(BRANCO) ||
selectedColor.equalsIgnoreCase(AMARELO) ||
selectedColor.equalsIgnoreCase(CINZA) ||
selectedColor.equalsIgnoreCase(PRATA))
{
//ação para fazer a cor da fonte do item selecionado no menu ser preta quando a cor selecionada pelo usuário for branca, amarela, cinza ou prata
}
But I’m not being able to access, through the java, this subclass ". label" that is inside the Choice-box. And you can’t create a @FXML Label meuLabel
referencing to this text. You could do this if what I wanted to change was the label that is above each menu, saying what it is. The idea would be to do something like:
meuCoiceBox.getValue().setStyle("-fx-text-fill: black;");
or a
meuChoiceBox.getSelectionModel().getSelectedItem().setStyle("-fx-text-fill: black;");
...to change the color of the text this item with light background.
Sorry it took me so long to give you feedback. I haven’t been able to make it work yet... I must be doing something wrong. As soon as I get it, I’ll let you know. Thank you for the reply!
– Marcelino Borges
I think it is conflicting with the "fx:Id" that I have set for each menu of this. I can set an id pro css and another pro fx ?
– Marcelino Borges
You didn’t tell me you were using FXML, I’m changing to suit the answer.
– Gustavo Fragoso
In fact, I managed to make it work in parts... It only works properly the first time I choose a color from the menu. Every time you change the menu color, the menu onAction calls a method that changes the color of a rectangle (which sits on an image of a resistor). However, this method takes a choicebox type parameter, and saves the name of the menu id before going to a switch-case where it "asks" the name of the interacted menu and the selected color and changes the color of the rectangle. After I interact the first time, the menu gets another id (to change the css id) and the rectangle stops changing color..
– Marcelino Borges
I edited taking out the conflict of Ids, look at the code and css again.
– Gustavo Fragoso
I put down my final solution. I ended up changing some things, but the main thing was to have learned the use of this method "setStyleClass()". Thanks!
– Marcelino Borges