23

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.

Community
  • 1
  • 1
Deepak Goel
  • 5,624
  • 6
  • 39
  • 53

2 Answers2

36

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);
        }
    }
});
marc-medley
  • 8,931
  • 5
  • 60
  • 66
19

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

jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • does just onSucceeded returns value or onCanceled/onFailed? I wonder if I cancel task how to get current value returned but not sure if binding is the only way. thanks – Ewoks Dec 21 '17 at 12:36
  • [setOnSucceeded](https://docs.oracle.com/javase/8/javafx/api/javafx/concurrent/Task.html#setOnScheduled-javafx.event.EventHandler-) has nothing to do with the value of the task, it is just an event handler to be executed when the task succeeded. If you want to get the corresponding value, then you can execute [task.getValue()](https://docs.oracle.com/javase/8/javafx/api/javafx/concurrent/Task.html#getValue--) in the body of event handler (like marc's answer to this question). – jewelsea Dec 21 '17 at 18:54
  • You could make the same task.getValue() call in an [onCancelled](https://docs.oracle.com/javase/8/javafx/api/javafx/concurrent/Task.html#onCancelledProperty) handler if you wished. Depending on how you Task is supposed to function, getting a value from a cancelled Task may or may not make sense. As you mention, binding to the valueProperty will also work. – jewelsea Dec 21 '17 at 18:56