setPromptText() function not working initially with TextField
Asked Answered
I

3

5
private VBox addVBox() {

    VBox vb1 = new VBox();
    vb1.setPadding(new Insets(40, 40, 20, 40));
    vb1.setSpacing(20);
    vb1.setStyle("-fx-background-color: #333333;");

    TextField txt1 = new TextField();
    txt1.setPromptText("Class Number");
    txt1.setPrefSize(70, 30);

    Button b1 = new Button("DELETE");
    b1.setFont(Font.font("Calibri", FontWeight.BOLD, 17));
    b1.setPrefSize(100, 30);
    b1.setStyle(" -fx-base: #ffffff;");
    b1.setTextFill(Color.BLACK);

    vb1.getChildren().addAll( txt1, b1);        
    return vb1;
}

This is my code. In it setPromptText() function is working, but not showing the specified text content. This is because when the program is run, the textfield is the first control in it and when the window opens the textfield will be selected and so it does not shows the prompt text. How can I make the prompt text visible when the window opens ?

Ideatum answered 13/4, 2014 at 6:27 Comment(3)
acutally, it is working - just not showing when the textfield is focused :-)Revis
after digging a bit: looks like a bug to me. According to the doc, the prompt should show when the text is empty. Actually it is hidden when focused. You might consider filing an issue.Revis
@Revis How to file an issue ?Ideatum
L
4

set FocusTraversable() method to false

try this...

private VBox addVBox() {

VBox vb1 = new VBox();
vb1.setPadding(new Insets(40, 40, 20, 40));
vb1.setSpacing(20);
vb1.setStyle("-fx-background-color: #333333;");

TextField txt1 = new TextField();
txt1.setPromptText("Class Number");
txt1.setPrefSize(70, 30);
txt1.setFocusTraversable(false); // set focus traversable false.

Button b1 = new Button("DELETE");
b1.setFont(Font.font("Calibri", FontWeight.BOLD, 17));
b1.setPrefSize(100, 30);
b1.setStyle(" -fx-base: #ffffff;");
b1.setTextFill(Color.BLACK);

vb1.getChildren().addAll( txt1, b1);        
return vb1;
}
Literal answered 13/4, 2014 at 8:1 Comment(1)
Removing the field from the focus cycle might appear to work, but has the horrible side-effect of no longer being reachable by keyboard tabbing ..Revis
R
8

Further digging revealed that it's a feature, not a bug - there are arguments for both:

  • not showing the prompt when the field is focused feels like missing crucial information at the time the user needs it most
  • ux argues that prompts shouldn't have crucial information, to start with

To serve both sides, the behaviour is configurable via css, f.i.

name.setStyle("-fx-prompt-text-fill: derive(-fx-control-inner-background,-30%); }");
Revis answered 13/4, 2014 at 13:50 Comment(0)
L
4

set FocusTraversable() method to false

try this...

private VBox addVBox() {

VBox vb1 = new VBox();
vb1.setPadding(new Insets(40, 40, 20, 40));
vb1.setSpacing(20);
vb1.setStyle("-fx-background-color: #333333;");

TextField txt1 = new TextField();
txt1.setPromptText("Class Number");
txt1.setPrefSize(70, 30);
txt1.setFocusTraversable(false); // set focus traversable false.

Button b1 = new Button("DELETE");
b1.setFont(Font.font("Calibri", FontWeight.BOLD, 17));
b1.setPrefSize(100, 30);
b1.setStyle(" -fx-base: #ffffff;");
b1.setTextFill(Color.BLACK);

vb1.getChildren().addAll( txt1, b1);        
return vb1;
}
Literal answered 13/4, 2014 at 8:1 Comment(1)
Removing the field from the focus cycle might appear to work, but has the horrible side-effect of no longer being reachable by keyboard tabbing ..Revis
P
-1

I had the same problem but after adding setFocusTraversable(false); it works

Plywood answered 13/11, 2020 at 12:48 Comment(1)
nothing new compared to the accepted answer - so naturally has the same caveat ;)Revis

© 2022 - 2024 — McMap. All rights reserved.