JavaFX - 关闭对 TextArea 的可能关注

JavaFX - Turning off possible focus on TextArea

我的应用程序中有 TextAreaTextField。我设法在开始时将焦点放在 TextField 上,并使 TextArea 无法编辑。我还想以某种方式关闭通过鼠标单击或 TAB 循环来聚焦它的可能性。

有什么合适的方法吗?

您需要使用:

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

示例演示:

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

/**
 * @author Whosebug
 *
 */
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);
    }