Search code examples
eclipsejavafxtextfield

JavaFX TextField setStyle of -fx-text-fill sometimes ignored


The Scene contains a VBox, which contains an HBox, which has 3 children. Each of the 3 children is a subclassed StackPane containing a TextField. the HBox has setStyle("-fx-background-color:magenta;");, each of the 3 TextFields has setStyle("-fx-text-fill:yellow;-fx-background-color:pink;");. The 1st subclassed StackPane has setStyle(""); the 2nd has setStyle("-fx-background-color:green;"); and the 3rd has setStyle("-fx-font-size:20;");

public class Test extends Application {  
  static public void main(String[] args) { Application.launch(args); }
  public Test(){ } 
  public class S1 extends StackPane {
    public S1(String style, String text) {
      TextField textField = new TextField(text);
      textField.setEditable(false);
      textField.setStyle("-fx-text-fill:yellow;-fx-background-color:pink;");
      getChildren().setAll(textField);
      setStyle(style);
    }
  }
  @Override
  public void start(Stage stage) {
    VBox vb = new VBox();
    stage.setScene(new Scene(vb, 600, 100));
    stage.show();
    vb.requestFocus();
    HBox hb1 = new HBox(new S1("", "AA "), new S1("-fx-background-color:green;", "BB"), new S1("-fx-font-size:20;", "CC"));
    hb1.setStyle("-fx-background-color:magenta;");
    vb.getChildren().add(hb1);
  }
  @Override
  public void stop() { }
}

Expected Results: all 3 TextFields to have yellow text

Actual Results: 1st TextField has yellow text, the 2nd and 3rd have black text

I am a long retired assembler programmer, teaching myself Java and Object Oriented programming for 'fun', and I really can't figure out why the above code doesn't work... is it something I am missing or have I found a bug ?

Actual results image

enter image description here

Windows 11, Java jdk-17.0.4.1, javafx-sdk-20.0.2 (currently latest version), Eclipse Version: 2021-12 (4.22.0).


Solution

  • When I read the comment from @Slaw (which showed his software) I realized I had only tried running via Eclipse launch, so I exported code and ran without Eclipse. When the exported code worked, I decided to upgrade Eclipse.

    Upgrading Eclipse from 2021-12 to 2023-06 by itself did not solve it, but additionally upgrading Java from 17.0.4.1 to 20.0.2 did.

    Conclusion: Upgrading Java was required to fix it, and upgrading Eclipse may or may not have been required.