← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #667296]: Java FX app: using SikuliX features in handlers freezes UI

 

Question #667296 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/667296

    Status: Solved => Open

Julian is still having a problem:
I sadly run into a new problem:

public class Controller{


    //This Listview is used as log
    public ListView<String> listView_log;
    public static ObservableList<String> listView_log_text = FXCollections.observableArrayList();


    //starts a the new Thread "Workroutine" every 20 seconds and  activates the actualizer method every 3 seconds
    public void initialize() {

        debug_log("Started the Work!");

        //starts a the new Thread "Workroutine" every 20 seconds
        Timeline timeline_workRoutineStarter = new Timeline(new KeyFrame(Duration.seconds(20), new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                Thread t = new Thread(new Workroutine());
                t.start();
            }
        }));
        timeline_workRoutineStarter.setCycleCount(Timeline.INDEFINITE);
        timeline_workRoutineStarter.play();

        //activates the actualizer method every 3 seconds, the actualizer method refreshes listView_log
        Timeline timeline_refresh_listView_log = new Timeline(new KeyFrame(Duration.seconds(3), new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                ui_actualizer();
            }
        }));
        timeline_refresh_listView_log.setCycleCount(Timeline.INDEFINITE);
        timeline_refresh_listView_log.play();

    }

 //refreshes the UI, is triggered every 3 seconds by timeline_refresh_listView_log in the initialize() function
    public void ui_actualizer() {

        listView_log.setItems(listView_log_text);
       }

 //refreshes only the listView_log
    public void debug_log(String text) {

        String currentTime = new SimpleDateFormat("HH:mm:ss").format(Calendar.getInstance().getTime());
        listView_log_text.add(currentTime + ": " + text);
        System.out.println(currentTime + ": " + text);
        listView_log.setItems(listView_log_text);

    }

---------------------------------------------------------------------------------------------------------------

public class Workroutine extends Thread {

 public void run() {
// stuff happens, searches for images, works fine now
//if i want to print a result to the Textview from the controller, i call
debug_log("Important stuff!")
}

public void debug_log(String text) {
        String currentTime = new SimpleDateFormat("HH:mm:ss").format(Calendar.getInstance().getTime());
        // Avoid throwing IllegalStateException by running from a non-JavaFX thread.
        Platform.runLater(
                () -> {
                    Controller.listView_log_text.add(currentTime + ": " + text);
                }
        );
    }


So obv the problem was that now that the workroutine is in another thread, i can't access my Listview from the Controller anymore. I can't make it just static, as then the whole ListView doesn't work anymore.
So i made the ObservableList static and i change just the ObservableList from my other thread; the ui_actualizer-function refreshes this every 3 seconds.

I am pretty sure there is a way more elegant solution to this, but i couldn't find it yet.
Thanks for your support!

-- 
You received this question notification because your team Sikuli Drivers
is an answer contact for Sikuli.