2014-11-14 5 views
0

콤보 박스 안에 텍스트 입력을 양수로 제한해야합니다. 나는 이것에 대한 stackoverflow 검색 및 비슷한 질문을 발견했다 : Recommended way to restrict input in JavaFX textfield콤보 상자 안에 텍스트 입력 제한

유일한 차이점은 베어 텍스 필드를 언급 한 것입니다. javafx 디자이너가 승인 한 대답은 TextField 클래스를 확장하고 두 가지 메소드 인 : replaceTextreplaceSelection을 재정의하는 것입니다. 이 해킹은 콤보 박스와 함께 작동하지 않습니다. TextField 인스턴스가 내부에 저장되어 있고 읽기 전용 속성이 editor 인 경우 사용할 수 있습니다.

그래서 javafx 콤보 박스 내에서 텍스트 입력을 제한하는 권장 방법은 무엇입니까?

답변

0

편집기 속성에 등록 방법을 등록하여 입력이 허용되는지 확인할 수 있습니다.

여기서는 편집이 가능하지만 값이 항목 목록에없는 경우 빨간색 프레임을 그립니다.

ObservableList<String> myChoices = FXCollections.observableArrayList(); 

void testComboBoxCheck(VBox box) { 
    myChoices.add("A"); 
    myChoices.add("B"); 
    myChoices.add("C"); 

    ComboBox<String> first = new ComboBox<String>(); 
    first.setItems(myChoices); 
    first.setEditable(true); 
    first.editorProperty().getValue().textProperty().addListener((v, o, n) -> { 
     if (myChoices.contains(n.toUpperCase())) { 
      first.setBackground(new Background(new BackgroundFill(Color.rgb(30,30,30), new CornerRadii(0), new Insets(0)))); 
     } else { 
      first.setBackground(new Background(new BackgroundFill(Color.RED, new CornerRadii(0), new Insets(0)))); 
     } 
    }); 

    box.getChildren().addAll(first); 
} 

enter image description hereenter image description here

+0

TextField 클래스와 관련된 비슷한 질문을하는 많은 논평자가 이러한 방식을 권장하지 않습니다. 나는 이유를 모른다. – ayvango

0

어떻게 콤보 상자에서 텍스트 편집기를 decouplign 그 값을 연결 어떻습니까?

HBox combo = new HBox(); 

    TextField editor = new TextField(); 

    ComboBox<String> first = new ComboBox<String>(); 
    first.setItems(myChoices); 
    first.setButtonCell(new ComboBoxListCell<String>(){ 
     @Override public void updateItem(String s, boolean empty) { 
      super.updateItem(s, empty); 
      setText(null); 
     } 
    }); 

    editor.textProperty().bindBidirectional(first.valueProperty()); 

    combo.getChildren().addAll(editor, first); 
    box.getChildren().addAll(combo); 

이제이 문제의 질문은 내가에 사용자를 제한하는 I 구현 한 솔루션을 추가 해요 적절한 대답을 얻었다 결코 때문에 등

0

어떤 메소드를 오버라이드 (override) 할 수 있도록 텍스트 필드에 대한 모든 conroll이 정규 표현식과 일치하고 특정 길이보다 짧은 입력입니다 (선택 사항 임). 이것은 ChangeListener을 편집기 TextField에 추가하여 이루어집니다. 일치하지 않는 입력은 편집기 TextField에 기록되지 않습니다.

이 예에서는 사용자를 최대 두 개의 숫자로 제한합니다.

ComboBox<Integer> integerComboBox = new ComboBox<Integer>(); 
integerComboBox.setEditable(true); 
integerComboBox.getEditor().textProperty() 
      .addListener(new ChangeListener<String>() { 

       // The max length of the input 
       int maxLength = 2; 

       // The regular expression controlling the input, in this case we only allow number 0 to 9. 
       String restriction = "[0-9]"; 

       private boolean ignore; 

       @Override 
       public void changed(
         ObservableValue<? extends String> observableValue, 
         String oldValue, String newValue) { 
        if (ignore || newValue == null) { 
         return; 
        } 

        if (newValue.length() > maxLength) { 
         ignore = true; 
         integerComboBox.getEditor().setText(
           newValue.substring(0, maxLength)); 
         ignore = false; 
        } 

        if (!newValue.matches(restriction + "*")) { 
         ignore = true; 
         integerComboBox.getEditor().setText(oldValue); 
         ignore = false; 
        } 
       } 
      }); 
관련 문제