
ObservableListparagraph = textArea.getParagraphs(); Iterator iter = paragraph.iterator(); try { BufferedWriter bf = new BufferedWriter(new FileWriter(new File("textArea.txt"))); while(iter.hasNext()) { CharSequence seq = iter.next(); bf.append(seq); bf.newLine(); } bf.flush(); bf.close(); } catch (IOException e) { e.printStackTrace(); }
public void doPrint() {
String toPrint = writeArea.getText();
printSetup(new Label(toPrint), Main.primaryStage);
}
private final Label jobStatus = new Label();
private void printSetup(Node node, Stage owner) {// Create the PrinterJob
PrinterJob job = PrinterJob.createPrinterJob();
if (job == null) {
return; }
// Show the print setup dialog
boolean proceed = job.showPrintDialog(owner);
if (proceed) {
print(job, node); }
}
private void print(PrinterJob job, Node node) {
// Set the Job Status Message
jobStatus.textProperty().bind(job.jobStatusProperty().asString());
// Print the node
boolean printed = job.printPage(node);
if (printed) { job.endJob(); }
}
// 追加するimport
// import javafx.animation.PauseTransition
// import javafx.application.Platform;
// import java.util.concurrent.ForkJoinPool;
// ここまでJavaFXスレッドで実行
// ここでスレッドの進行を止めようと考えました
PauseTransition.commonPool().submit(() -> {
// 非JavaFXスレッドを非同期で実行
try {
Thread.sleep(2000L);
} catch (Exception e) { e.printStackTrace();
throw new RuntimeException(e);
}
abs*=-1;
Platform.runLater(() -> {
// 後でJavaFXスレッドで実行
status_label.setText("Succeeded");
});
flagProperty.set(false);
});
import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.concurrent.WorkerStateEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javax.xml.datatype.Duration;
public class DelayWithTask extends Application {
private static Label label;
public static void main(String[] args) { launch(args); }
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World!");
label = new Label();
label.setText("Waiting...");
StackPane root = new StackPane();
root.getChildren().add(label);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
delay(5000, () -< label.setText("Hello World"));
}
public static void delay(long millis, Runnable continuation) {
Task<Void> sleeper = new Tas>Void>() {
@Override
protected Void call() throws Exception {
try { Thread.sleep(millis); }
catch (InterruptedException e) { }
return null;
}
};
sleeper.setOnSucceeded(event -> continuation.run());
new Thread(sleeper).start();
}
}

