Ingo
Ingo

Reputation: 635

Keep Selection in JavaFX Textfield even if application is not focussed

Background: I'm trying to implement a find/replace function for java-Fx table view. Whenever I find an occurance of the text to find, I switch the table view into edit mode and select the text found programatically in the textfield which is used during edit mode.

The focus in this situation remains in the modal find/replace dialog, so the selection in the textfield is not visible until I close the modal dialog.

The problem: A textfield in Java-Fx shows it's selection only while it has the focus (as its standard behaviour). When the Java-Application loses focus the selection becomes invisible, when it gets the focus back the selection is visible again.

Here is the question: Is it possible to keep the selection visible in a textfield though focus is lost?

What I tried:

I expected that CSS could help here:

textField.setStyle("-fx-highlight-fill: lightgray");

This changes the color of the highlighting but

What is the correct way? Or is there a way to highlight text in a TableView without activating the TextField?

Thanks

Ingo

Upvotes: 1

Views: 608

Answers (1)

Cobbles
Cobbles

Reputation: 1798

If I am understanding correctly, the solution will be in the modality of your find/replace dialog.

It works for me when I do the following: Note that the java file this is written in extends Stage. If you are not extending stage then just call the methods on your find/replace stage.

owner = myApplicationStage;
initModality(Modality.NONE);//important for the solution!
initOwner(owner);
initStyle(StageStyle.UTILITY);
setScene(myFindAndReplaceScene);
stage = this;
stage.show();

This works perfectly for me, if you have any problems then hopefully I can help.

Upvotes: 1

Related Questions