Sunday, August 25, 2013

Implement custom Properties for bind() and ChangeListener()

In the post of "JavaFX example of Service", the UI elements progressBar and labelCount are bind to build-in properties, progressProperty and messageProperty, of Service. Now I want to update progressBar and labelCount in 5 second; we can create our custom Properties, process5sec and Message5sec, such that we can bind them to UI elements or addListener() to trace the changing for them.

Implement custom Properties for bind() and ChangeListener()
Implement custom Properties for bind() and ChangeListener()

package javafx_service;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.concurrent.WorkerStateEvent;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFX_Service extends Application {

    MyService myService;

    @Override
    public void start(Stage primaryStage) {

        final ProgressBar progressBar = new ProgressBar();
        final Label labelCount = new Label();
        final Label labelState = new Label();
        final Label labelSucceeded = new Label();

        myService = new MyService();

        myService.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
            @Override
            public void handle(WorkerStateEvent t) {
                labelSucceeded.setText("OnSucceeded");
            }
        });

        myService.setOnRunning(new EventHandler<WorkerStateEvent>() {
            @Override
            public void handle(WorkerStateEvent t) {
                labelSucceeded.setText("OnRunning");
            }
        });

        myService.setOnFailed(new EventHandler<WorkerStateEvent>() {
            @Override
            public void handle(WorkerStateEvent t) {
                labelSucceeded.setText("OnFailed");
            }
        });

        myService.process5secProperty().addListener(new ChangeListener(){

            @Override
            public void changed(ObservableValue ov, Object t, Object t1) {
                
                double p = (double)(((Integer)t1).intValue());
                progressBar.setProgress(p/50);
            }
        });
        
        labelCount.textProperty().bind(myService.Message5sec);

        Button btnStart = new Button("Start Service");
        btnStart.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent t) {
                myService.start();
            }
        });

        Button btnReadTaskState = new Button("Read Service State");
        btnReadTaskState.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent t) {
                labelState.setText(myService.getState().toString());
            }
        });

        VBox vBox = new VBox();
        vBox.setPadding(new Insets(5, 5, 5, 5));
        vBox.setSpacing(5);
        vBox.getChildren().addAll(
                progressBar,
                labelCount,
                btnStart,
                btnReadTaskState,
                labelState,
                labelSucceeded);

        StackPane root = new StackPane();
        root.getChildren().add(vBox);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

    private class MyService extends Service<Void> {

        private final IntegerProperty process5sec = new SimpleIntegerProperty();

        public int getProcess5sec() {
            return process5sec.get();
        }

        public void setProcess5sec(int value) {
            process5sec.set(value);
        }

        public IntegerProperty process5secProperty() {
            return process5sec;
        }
        private final StringProperty Message5sec = new SimpleStringProperty();

        public String getMessage5sec() {
            return Message5sec.get();
        }

        public void setMessage5sec(String value) {
            Message5sec.set(value);
        }

        public StringProperty Message5secProperty() {
            return Message5sec;
        }

        @Override
        protected Task<Void> createTask() {
            return new Task<Void>() {
                @Override
                protected Void call() throws Exception {
                    int max = 50;
                    for (int i = 1; i <= max; i++) {
                        if (isCancelled()) {
                            break;
                        }

                        if (i % 5 == 0) {
                            final int i5sec = i;
                            Platform.runLater(new Runnable() {
                                @Override
                                public void run() {
                                    setProcess5sec(i5sec);
                                    setMessage5sec(String.valueOf(i5sec));    
                                }
                            });
                        }

                        Thread.sleep(100);
                    }
                    return null;
                }
            };
        }
    }
}



No comments:

Post a Comment