15

Basically I have changed the css for a text field in javafx by adding a style class like this:

textfield.getStyleClass().add("textfieldstyle");

But then I want to be able to revert it back to its original appearance. But since the original appearance in this case is the default skin for JavaFX, I can't find the original layout for the textfields. I found the textfieldskin properties here, but its a jungle, and I can't find anything about the color of the -fx-control-inner-background, -fx-text-box-border and -fx-focus-color, which is what I want to know.

I've triedtextfield.getStyleClass().remove("textfieldstyle"); and think that does remove the new css, but it doesn't apply the old one again.

5
  • 3
    After removing the styleclass, System.out.println(textfield.getStyleClass()); to be sure. Since you may have added it more than once. Commented Jun 10, 2015 at 14:35
  • Is it version 2 or 8 of JavaFX? Commented Jun 10, 2015 at 14:36
  • 2
    or you can use textField.getStyleClass().clear(); textField.getStyleClass().add("text-field"); textField.getStyleClass().add("text-input"); Here is demo gist.github.com/varren/3872d5611a2726271be4 Commented Jun 10, 2015 at 22:05
  • @UlukBiy It's JavaFx 8. Commented Jun 11, 2015 at 7:45
  • @Uluk Biy Thanks you just solved me a week long bug... Why the heck isn't the styleclass list, an unique one? q_q Commented Sep 29, 2017 at 5:10

4 Answers 4

16

Thanks to the comments by @UlukBiy and @varren I solved the issue. System.out.println(textfield.getStyleClass()); was of great use since it allowed me to check which style classes were applied on the text field as default. And as it is pointed out in the comments those where text-input and text-field.

So to restore the text field's css to its default value I just did:

textfield.getStyleClass().clear();
textfield.getStyleClass().addAll("text-field", "text-input");
Sign up to request clarification or add additional context in comments.

1 Comment

Directly setting the classes works too : textfield.getStyleClass().setAll("text-field", "text-input");
11

To reset an element's default styling after setting it using .setStyle("css settings....."); you can simply use -

textfield.setStyle(null);

I'm not sure if this would work on it's own for an element that's had a class applied using .getStyleClass().add("textfieldstyle"); but if not you could do something like -

textfield.getStyleClass().clear();
textfield.setStyle(null);

Comments

7

A short way to effictively remove your style class even in case of duplicates, with a lambda :

textfield.getStyleClass().removeIf(style -> style.equals("textfieldstyle"));

Comments

2

The following test code works for me when adding and removing a class from a control such as Textfield:

import javafx.scene.control.TextInputControl;

public class test
{
  protected void setEditable(final TextInputControl toControl, final boolean tlEditable)
  {
    toControl.setEditable(tlEditable);
    if (tlEditable)
    {
      if (toControl.getStyleClass().contains("non-editable-class"))
      {
        toControl.getStyleClass().removeAll("non-editable-class");
      }
    }
    else if (!toControl.getStyleClass().contains("non-editable-class"))
    {
      toControl.getStyleClass().add("non-editable-class");
    }
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.