2017-09-24 38 views
0

我有兩個TextField的QntMatr是指物質的數量和UniteMatr是指單位quantity.I需要,當用戶把光標放在QntMatrUniteMatr,按鈕addMatrButton應禁用,而當QntMatr和UnitrMatr不爲空,將啓用他們都試過。我嘗試過UniteMatrQntMatr之間的綁定,但我不知道確切的方法。JavaFx:如何使用重點屬性?

代碼

QntMatr.focusedProperty().addListener(new ChangeListener<Boolean>() { 
      @Override 
      public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) { 
       if (newValue) { 
        if (QntMatr.getText().isEmpty() && UniteMatr.getText().isEmpty()) { 
         AddMatrButton.setDisable(true); 

        } else { 
         AddMatrButton.setDisable(false); 

        } 

       } 

      } 
     }); 
     UniteMatr.focusedProperty().addListener(new ChangeListener<Boolean>() { 
      @Override 
      public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) { 
       if (newValue) { 
        if (QntMatr.getText().isEmpty() && UniteMatr.getText().isEmpty()) { 
         AddMatrButton.setDisable(true); 

        } else { 
         AddMatrButton.setDisable(false); 

        } 

       } 

      } 
     }); 
+0

會發生什麼,如果「TextField」之一是重點,但兩個「TextField」都是非空的?對我來說,用戶需要關注某些其他控件才能啓用「Button」,這似乎是不好的用戶體驗...... – fabian

+0

不,「QntMatr」和「UnitMatr」是可選字段。用戶無法添加數量和離開單位爲空。 –

回答

1

如果你想依賴於文本字段的內容禁用的按鈕(true,如果是空的,否則爲false),那麼你應該綁定按鈕與TextField的textProperty disableProperty。這裏有一個例子:

import javafx.application.Application; 
import javafx.beans.binding.Bindings; 
import javafx.geometry.Insets; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.TextField; 
import javafx.scene.layout.HBox; 
import javafx.stage.Stage; 

public class TestClass extends Application { 

    public static void main(String[] args) { 
     launch(args); 
    } 

    @Override 
    public void start(Stage stage) throws Exception { 

     HBox box = new HBox(10); 
     box.setPadding(new Insets(10)); 

     TextField field = new TextField(); 
     Button button = new Button("OK"); 

     button.disableProperty().bind(Bindings.isEmpty(field.textProperty())); 

     box.getChildren().addAll(field, button); 

     stage.setScene(new Scene(box)); 

     stage.show(); 
    } 

} 

如果你想更復雜的東西結合實例,以檢查是否該領域的重點是不是你可以做這樣的事情:

import javafx.application.Application; 
import javafx.beans.binding.BooleanBinding; 
import javafx.geometry.Insets; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.TextField; 
import javafx.scene.layout.HBox; 
import javafx.stage.Stage; 

public class TestClass extends Application { 

    public static void main(String[] args) { 
     launch(args); 
    } 

    @Override 
    public void start(Stage stage) throws Exception { 

     HBox box = new HBox(10); 
     box.setPadding(new Insets(10)); 

     TextField field = new TextField(); 
     Button button = new Button("OK"); 

     button.disableProperty().bind(new BooleanBinding() { 
      { 
       bind(field.textProperty()); 
       bind(field.focusedProperty()); 
      } 

      @Override 
      protected boolean computeValue() { 

       // you can check if the field is focused 
       // of if it's content is empty etc. 
       return field.getText().isEmpty(); 

      } 

     }); 

     box.getChildren().addAll(field, button); 

     stage.setScene(new Scene(box)); 

     stage.show(); 
    } 

}