【问题标题】:How do i print something on focusOut on TextField in javafx2如何在 javafx2 的 TextField 上的 focusOut 上打印一些东西
【发布时间】:2013-05-11 17:31:16
【问题描述】:

我有一个 TextField 并想对 textfiels 执行一些操作

TextField textField = new TextField();

如何打印

System.ou.prinln("Focus Out");

当焦点离开文本字段时。

【问题讨论】:

    标签: javafx-2 javafx java java-7 javafx-8


    【解决方案1】:

    JavaFX 2.2 TextField 有一个 public focusedProperty 继承自 Node 类,只需添加一个 ChangeListener 并实现 changed 方法即可。

    public class App extends Application {
    
        @Override
        public void start(Stage primaryStage) {
            TextField txt1= new TextField("text 1");
            TextField txt2= new TextField("text 2 ");
            Button btn= new Button("hello");
    
            StackPane root = new StackPane();
            VBox vBox= VBoxBuilder.create()
                    .children(txt1, btn, txt2)
                    .build();
    
            txt1.focusedProperty().addListener(new ChangeListener<Boolean>() {
    
                @Override
                public void changed(ObservableValue<? extends Boolean> ov, Boolean t, Boolean t1) {
                    if (t1) {
                        System.ou.prinln("Focus In");
                    } else {
                        System.ou.prinln("Focus Out");
                    }
    
                }
            });
    
            root.getChildren().add(vBox);       
            Scene scene = new Scene(root, 500, 400);
    
            primaryStage.setTitle("Hello World!");
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    
        /**
         * The main() method is ignored in correctly deployed JavaFX application.
         * main() serves only as fallback in case the application can not be
         * launched through deployment artifacts, e.g., in IDEs with limited FX
         * support. NetBeans ignores main().
         *
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-06
      • 2018-04-17
      • 1970-01-01
      • 2019-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多