3 回答

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超6個(gè)贊
線程“Thread A”中的異常java.lang.IllegalStateException:不在FX應(yīng)用程序線程上;currentThread=Thread A
courseCodeLbl.setText(rs.getString(1)); // <--- The culprit
如果我不能這樣做,我如何使用背景線程?
用Platform.runLater包裝場景圖元素
Plaform.runLater
Platform.runLater(() -> courseCodeLbl.setText(rs.getString(1)));
使用任務(wù)
updateMessage
courseCodeLbl
Task<Void> task = new Task<Void>() { @Override public Void call() { String courseName = ""; Course c = new Course(); c.setCCode(Integer.valueOf(courseId.getText())); mController = new ModelController(c); try { ResultSet rs = mController.<Course>get(); if(rs.next()) { // update message property updateMessage(rs.getString(1)); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }}public void getCourseNameOnClick(){ try { Thread t = new Thread(task); // To update the label courseCodeLbl.textProperty.bind(task.messageProperty()); t.setDaemon(true); // Imp! missing in your code t.start(); } catch (NumberFormatException e) { // TODO Auto-generated catch block e.printStackTrace(); }}

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超4個(gè)贊
public void getCourseNameOnClick(){ new Thread(new Runnable(){ public void run(){ String courseName = requestCourseName(); Platform.runLater(new Runnable(){ courseCodeLbl.setText(courseName) }); } }, "Thread A").start();}
添加回答
舉報(bào)