Using threads to make database requests

后端 未结 3 563
挽巷
挽巷 2020-11-22 00:39

I\'m trying to understand how threads works in java. This is a simple database request that returns a ResultSet. I\'m using JavaFx.

    package application;
         


        
3条回答
  •  你的背包
    2020-11-22 01:08

    This has nothing to do with database. JavaFx, like pretty much all GUI libraries, requires that you only use the main UI thread to modify the GUI.

    You need to pass the data from the database back to the main UI thread. Use Platform.runLater() to schedule a Runnable to be run in the main UI thread.

    public void getCourseNameOnClick(){
        new Thread(new Runnable(){
            public void run(){
                String courseName = requestCourseName();
                Platform.runLater(new Runnable(){
                    courseCodeLbl.setText(courseName)
                });
            }
        }, "Thread A").start();
    }
    

    Alternatively, you can use Task.

提交回复
热议问题