다른 스레드의 텍스트를 업데이트하는 JavaFx TextArea가 있습니다.항상 TextArea의 마지막 줄 표시
/**
* Start a new thread to update the text in the textarea. The progressmanager is called in a loop to deleiver the updated text
*/
private void startTextAreaUpdate() {
final Task<Void> updateTextAreaTask = new Task<Void>() {
@Override
protected Void call() throws Exception {
// Start updating the progresstext
while (!prog.getIsDone()) {
updateMessage(prog.getProgressText());
Thread.sleep(100);
}
return null;
}
};
//Here the TextArea text is bound to the task
taProgressText.textProperty()
.bind(updateTextAreaTask.messageProperty());
// Start the action in a new thread
Thread th = new Thread(updateTextAreaTask);
th.setDaemon(true);
th.start();
}
텍스트가 텍스트 영역에 맞지 않기 때문에 항상 텍스트의 마지막 줄을 표시하려고합니다. 주 스레드에서 ChangeListener를 추가합니다.
taProgressText.textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable,
String oldValue, String newValue) {
int lastPos = taProgressText.getText().length();
taProgressText.positionCaret(lastPos);
}
});
그러나 위치는 변경되지 않습니다. 여기서 뭐가 잘못 됐어?
정말 귀하의 질문에 관련되지 있지만 updateMessage 방법의 구현을 표시하지 않습니다 : 대신 바인딩과 현재 청취자가 당신이의 (... 추한 해킹)이보십시오. FX 응용 프로그램 스레드에서 실행해야한다고 생각합니다. – Boomah
@Boomah ['updateMessage'] (http://docs.oracle.com/javase/8/javafx/api/javafx/concurrent/Task.html#updateMessage-java.lang.String-)는 Task API의 일부입니다. , "updateMessage에 대한 호출은 병합되어 나중에 FX 응용 프로그램 스레드에서 실행됩니다." – jewelsea