Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the return value of call method of Task class in Javafx

Tags:

javafx-2

I am using Task class to run background task in javafx application to fetch the data from the database.

public class CustomTask extends Task<ObservableList<ObservableList>> {
    TableView tableview;
    ObservableList<ObservableList> data;

    public CustomTask(TableView tableview) {
        this.tableview = tableview;
    }

    @Override
    protected ObservableList<ObservableList> call() throws Exception {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String SQL = "SELECT * from sell where Date='" + dateFormat.format(date) + "'";
        ResultSet rs = DBConnect.getResultSet(SQL);
        data = DBConnect.generateListDateFromTable(rs, true);

        return data;
    }
}    

How to use the data object.

like image 935
Deepak Goel Avatar asked Jul 28 '12 18:07

Deepak Goel


People also ask

How do you use a value returned from a method?

You declare a method's return type in its method declaration. Within the body of the method, you use the return statement to return the value. Any method declared void doesn't return a value and cannot contain a return statement. Any method that is not declared void must contain a return statement.

How do you call a method return in Java?

To use the return value when calling a method, it must be stored in a variable or used as part of an expression. The variable data type must match the return type of the method.

What does platform runLater do?

runLater. Run the specified Runnable on the JavaFX Application Thread at some unspecified time in the future. This method, which may be called from any thread, will post the Runnable to an event queue and then return immediately to the caller. The Runnables are executed in the order they are posted.

What is concurrency in JavaFX?

The javafx. concurrent package consists of the Worker interface and two basic classes, Task and Service , both of which implement the Worker interface. The Worker interface provides APIs that are useful for a background worker to communicate with the UI. The Task class is a fully observable implementation of the java.


2 Answers

Example 1 addEventHandler

MyResultObjectType result;
CustomTask task = new CustomTask();
task.addEventHandler(WorkerStateEvent.WORKER_STATE_SUCCEEDED, 
        new EventHandler<WorkerStateEvent>() {
    @Override
    public void handle(WorkerStateEvent t) {
        result = task.getValue();
    }
});

Example 2 setOnSucceeded

MyResultObjectType result;
CustomTask task = new CustomTask();
task.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
    @Override
    public void handle(WorkerStateEvent t) {
        result = task.getValue();
    }
});

Example 3 addListener

task.valueProperty().addListener(new ChangeListener<Task>() {
    @Override
    public void changed(ObservableValue<? extends mytype> obs, 
                        mytype oldValue, mytype newValue) {
        if (newValue != null) {
            System.out.println("Result = " + newValue);
        }
    }
});
like image 87
l --marc l Avatar answered Oct 13 '22 17:10

l --marc l


Bind to the Task's value property OR provide a task.setOnSucceeded() event handler and call task.getValue() in the provided event handler.

like image 19
jewelsea Avatar answered Oct 13 '22 18:10

jewelsea