FXML_ThreadRunnable1
  • Using Scenebuilder
Step : 1 Coding can't more easier than using Scene builder. Click on JavaFxDoc1.fxml to open the scene builder canvas.

Code:

---main class ---

package javafxtemplate1;
//Grid_VBOx_Thread
import java.io.IOException;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;

public class JavaFXTemplate1 extends Application {
//String path ="C:/NetBean_Examples/JavaCore/JavaFXTemplate1/JavaFXMLDoc1.fxml";
@Override
public void start(Stage primaryStage) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("JavaFXMLDoc1.fxml"));
//Scene scene = new Scene(root);

//Group root = new Group();
Scene scene = new Scene(root, 450,300);
primaryStage.setScene(scene);
primaryStage.setTitle("FXML AnchorPane::List , ListView::Label, Button");
primaryStage.show();
}
//

//
public static void main(String[] args) {
Application.launch(args);
}

}



------JavaFXMLDoc1-------

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.control.Label?>

<AnchorPane id="AnchorPane" prefHeight="304.0" prefWidth="408.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxtemplate1.JavaFXMLDoc1Controller">
<stylesheets>
<URL value="@javafxmldoc1.css" />
</stylesheets>
<children>
<Button fx:id="button" layoutX="24.0" layoutY="14.0" onAction="#startTask" text="Click Me!" />
<Label fx:id="label1" layoutX="139.0" layoutY="18.0" minHeight="16" minWidth="69" />
<Button layoutX="31.0" layoutY="227.0" mnemonicParsing="false" onAction="#endTask" text="Exit" />
<TextArea fx:id="T1" layoutX="104.0" layoutY="61.0" prefHeight="143.0" prefWidth="181.0" />
</children>
</AnchorPane>


----JavaFXMLDoc1Controller ---

package javafxtemplate1;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;


/**
* FXML Controller class
*
* @author Manas9
*/
public class JavaFXMLDoc1Controller implements Initializable {

/**
* Initializes the controller class.
*/
@FXML
private Label label1, label2;
@FXML
TextArea T1 = new TextArea();

@FXML
Runnable task = () -> runTask();
@FXML
private void startTask(ActionEvent event) {
//
Thread backgroundThread = new Thread(task);
// Terminate the running thread if the application exits
label1.setText(backgroundThread.getName());
backgroundThread.setDaemon(true);
// Start the thread
backgroundThread.start();

//
}

@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
//label2.setText("Initialized ");
System.out.println("initialized!");

}


@FXML
private void endTask(ActionEvent event) {
//System.out.println("You clicked me!");
Platform.exit();
}

private void runTask() {
label1.setText(" started");
for(int i = 1; i <= 10; i++) {
try {
//final String status = "Processing " + i + " of " + 10+"\n";
String status = "Processing " + i + " of " + 10+"\n";
Platform.runLater(() ->T1.appendText(status) );

Thread.sleep(500);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
T1.appendText("Thread completed");

}

}


 

Displays: