JavaFX - 关闭可能专注于TextArea

时间:2016-12-14 22:06:50

标签: javafx focus lost-focus

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

有没有合适的方法呢?

1 个答案:

答案 0 :(得分:1)

您需要使用:

 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 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);
    }