Is there any way to remove focus in javafx?

I know that you can focus on node in javafx by doing node.requestFocus (); but is there a way to remove focus from node in javafx or prevent focus on the object?

+6
source share
3 answers

I don’t think there is a guarantee that this will always work, but you can try adjusting the focus to something that in fact does not accept keyboard input (for example, the layout panel):

import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.TextField; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class NoFocusTest extends Application { @Override public void start(Stage primaryStage) { TextField tf1 = new TextField(); tf1.setPromptText("Enter something"); TextField tf2 = new TextField(); tf2.setPromptText("Enter something else"); VBox root = new VBox(5, tf1, tf2); primaryStage.setScene(new Scene(root, 250, 150)); primaryStage.show(); root.requestFocus(); } } 
+17
source
 node = new node() { public void requestFocus() { } }; 

Now this will override the focus, and the node will NEVER be able to focus. You can also (as indicated above) disable the node with:

 node.setDisable(true); 

If you need to focus later:

 node.setDisable(false); node.requestFocus(); 

I decided to update my answer to this question with another option. If at the beginning of the program you focus on another node, you can set that a certain node will not move and it will not receive focus.

 node.setFocusTraversable(false); 
+11
source

If you have another node, you can remove the focus from your node and transfer it to another.

 otherNode.requestFocus(); 

In this case, you will not need to disable or enable the source node.

Some nodes, such as a tag, will not look different if they have focus, so it may look as if the focus has been removed.

0
source

Source: https://habr.com/ru/post/1200859/


All Articles