Created
July 7, 2014 11:41
-
-
Save skrb/65aab68d8370c7804d3f to your computer and use it in GitHub Desktop.
TextArea Demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.util.List; | |
import javafx.application.Application; | |
import javafx.beans.property.StringProperty; | |
import javafx.concurrent.Service; | |
import javafx.concurrent.Task; | |
import javafx.scene.Scene; | |
import javafx.scene.control.TextArea; | |
import javafx.scene.layout.AnchorPane; | |
import javafx.stage.Stage; | |
public class AsyncTextAreaUpdater extends Application { | |
@Override | |
public void start(Stage stage) throws Exception { | |
AnchorPane root = new AnchorPane(); | |
TextArea area = new TextArea(); | |
root.getChildren().add(area); | |
AnchorPane.setTopAnchor(area, 0.0); | |
AnchorPane.setBottomAnchor(area, 0.0); | |
AnchorPane.setLeftAnchor(area, 0.0); | |
AnchorPane.setRightAnchor(area, 0.0); | |
stage.setScene(new Scene(root, 400, 300)); | |
stage.show(); | |
Service<Void> service = new Service<Void>() { | |
@Override | |
protected Task<Void> createTask() { | |
return new Task<Void>() { | |
@Override | |
protected Void call() throws Exception { | |
// ファイル名は適当にかえてください | |
Path path = Paths.get("text.txt"); | |
List<String> lines = Files.readAllLines(path); | |
lines.forEach(line -> { | |
try { | |
updateMessage(line); | |
Thread.sleep(50l); | |
} catch (InterruptedException ex) { | |
} | |
}); | |
return null; | |
} | |
}; | |
} | |
}; | |
service.start(); | |
// メッセージが変化したら、テキストエリアに反映させる | |
service.messageProperty().addListener(o -> { | |
StringProperty prop = (StringProperty)o; | |
area.appendText(prop.get() + "\n"); | |
}); | |
} | |
public static void main(String... args) { | |
launch(args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment