2017-09-24 3 views
0

나는이 TextField의 QntMatr이 물질의 수량을 의미하고 QntMatr 또는 UniteMatr의 사용자 넣어 커서 버튼 addMatrButton가 비활성화해야 할 때 UniteMatr이 quantity.I 필요의 단위를 의미 있고, 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) 다음은 텍스트 필드의 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(); 
    } 

} 
관련 문제