2013-05-16 49 views
0

打印事件的內容的東西在文本字段我有一個文本字段,並要對其執行textfiels一些動作集中了我如何javafx2

TextField textField = new TextField(); 

如何打印

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

當從集中了文本域。

回答

2

JavaFX 2.2TextField公共focusedPropertyNode類繼承,只需添加一個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); 
    } 
} 
+0

thanx tarrsalah .... :)這個例子真的幫助我。 –

+0

不客氣。 – tarrsalah