JavaFX - Turning off possible focus on TextArea

1.1k views Asked by At

I have TextArea and TextField in my app. I have managed to give focus to TextField on the start and made TextArea impossible to edit. I want to also somehow turn off possibility to focus it with forexample mouse click or TAB cycling.

Is there any suitable way to do it?

1

There are 1 answers

0
GOXR3PLUS On BEST ANSWER

You need to use:

 textArea.setFocusTraversable(false);
 textArea.setMouseTransparent(true);

Example demonstration :

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

/**
 * @author StackOverFlow
 *
 */
public class Sample2 extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        BorderPane pane = new BorderPane();

        // Label
        TextArea textArea1 = new TextArea("I am the focus owner");
        textArea1.setPrefSize(100, 50);

        // Area
        TextArea textArea2 = new TextArea("Can't be focused ");
        textArea2.setFocusTraversable(false);
        textArea2.setMouseTransparent(true);
        textArea2.setEditable(false);

        // Add the items
        pane.setLeft(textArea1);
        pane.setRight(textArea2);

        // Scene
        Scene scene = new Scene(pane, 200, 200);
        primaryStage.setScene(scene);

        // Show stage
        primaryStage.show();

    }

    /**
     * Application Main Method
     * 
     * @param args
     */
    public static void main(String[] args) {
        launch(args);
    }