This may be a basic question but one that I am stuck on.
I would like some more understanding on why the GUI is blocked when I start a new thread(runnable) from a button click event? and how do I overcome that? What am I doing wrong?
The code below starts a new thread when it is clicked, however I would like to change the background color of a textbox when that button is clicked but I am unable to do that, also the main ui is unresponsive whilst that thread is running, I believed that I was implementing it correctly by starting a new thread so as to NOT block the main ui but I must have missed something out as this obviously not desired behaviour.
Code:
private void startButtonEvent(ActionEvent evt) {
ntmStatusText.setBackground(Color.green);// textbackground I want to change
//Start Ntm Thread
Thread thread = new Thread(new NtmThread());
thread.start();
while(thread.isAlive()){
System.out.println("thread still working");
}
System.out.println("thread stopped");
}
How do I stop my Ui from becoming unresponsive when running threads?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…