2014-09-09 6 views
0

Scene Builder에서 생성 된 TextField의 텍스트를 조작하려고합니다. 내 컨트롤러는 다음과 같습니다.JavaFX Scene 빌더 컨트롤러

@FXML 
private TextField textDescr; 

public void initialize(URL fxmlFileLocation, ResourceBundle resources) { 
    textDescr = new TextField(); 
    assert textDescr != null : "fx:id=\"textDescr\" was not injected: check your FXML file 'provingGroundsUI.fxml'."; 
    Game.mainFSM.enter(); 
} 
public void setText(String s) { 
    // TODO Auto-generated method stub 
    textDescr.setText(s); 
} 

NullPointerException이 발생합니다. 나는 textDescr = new TextField(); 부분과 함께 그리고없이 봇을 시도했다. JavaFX가 프로그램 시작시 모든 UI 변수를 초기화했다고 생각했습니다.

+0

는'FX합니까 : 다음 fxml에서 텍스트 필드의 id' 컨트롤러의 필드 이름과 일치 바인더 제본 StringProperty를 사용하는 것이 좋습니다? 즉, fxml에는'

+0

예 fx : id가 정확합니다. – pieAre5quare

+0

@를 확인하여 initialize 메소드를 확인하십시오. – Wamasa

답변

1
  1. FXML은 어떻게 보이나요?
  2. setText 함수에서 textDescr을 조작하려면 많은 위험이 있습니다.


    @FXML 
    private Text textDescr; 

    private StringProperty textProperty = new SimpleStringProperty(); 

    @FXML 
    void initialize() { 
      assert textDescr != null : "fx:id=\"textDescr\" was not injected: check your FXML file 'TestView.fxml'."; 
      textDescr.textProperty().bind(textProperty); 
    } 

    public ReadOnlyStringProperty textProperty(){ 
      return textProperty; 
    } 

0

컨트롤러 클래스는 Initializable

@FXML 주석 필드가 자바 FX에 의해 초기화됩니다 것을 보여준다 구현해야합니다. 따라서 new TextField 건을 제거하십시오.

이 컨트롤러를 FXML에 할당 하시겠습니까?

+0

컨트롤러가'Initializable'을 구현하고 컨트롤러가 FXML에 저장되었습니다. – pieAre5quare

+0

그런 다음 TextField의 가져 오기는 무엇입니까? 나는 그것이'java.awt.TextField'가 아니라고 희망한다. – galovics

+0

'import javafx.scene.control.TextField;' – pieAre5quare

관련 문제