prompttext in textfield of javafx 2 hides on focus

2019-07-01 11:37发布

i've been making simple javafx 2 gui application and found that prompttext in textfield of javafx 2 hides as soon as the textfield gets focus.

this wasn't this way some updates back. prior to this update, the textfield showed the prompttext until some text is typed into.

this isn't good and really need a workaround for this.

2条回答
做个烂人
2楼-- · 2019-07-01 11:59

I just solved this same issue by applying particular CSS rules to text inputs. Here's what I used (which should apply to all text-inputs)

.text-input, .text-input:focused { -fx-prompt-text-fill:darkgray; }
查看更多
甜甜的少女心
3楼-- · 2019-07-01 12:10

Erem Boto's answer is just fine and should solve your problem!

Please see my answer here to see how to solve the problem if you use proper Java code (no FXML and CSS file).
(And also how to get the other behavior back).

In short, this is the solution:

In case your application interface is written using proper Java code.

Java Code:

textField.setStyle("-fx-prompt-text-fill: derive(-fx-control-inner-background, -30%);");

Where textField is your TextField component.


And in case your application interface is written using FXML and CSS, add the following to your CSS file.

JavaFX FXML (CSS):

.text-input, .text-input:focused {
    -fx-prompt-text-fill: derive(-fx-control-inner-background, -30%);
}
查看更多
登录 后发表回答