3
My project in Java is a lighthouse control with Arduino. Up to there everything right. However I made a function in which the leds is automatic in a loop after a RadionButton
be selected. My problem is that when I select the RadionButton
mine JFrame
freeze and don’t let me take out the selection of RadionButton
.
In other words, selecting the button causes an active loop. The idea would be to uncheck the button, stop the loop.
Follows my code.
Here is where the button will be selected and send the command to the function:
if ( teste.isSelected() ) {
try {
utiArduino.enviarDados( "automatico" );
} catch (InterruptedException | IOException ex) {
Logger.getLogger(Jframe_farol.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
try {
utiArduino.enviarDados( "DesligaAuto" );
} catch (InterruptedException | IOException ex) {
Logger.getLogger(Jframe_farol.class.getName()).log(Level.SEVERE, null, ex);
}
}
Here is the function of the loop:
public void enviarDados(String status) throws InterruptedException, IOException{
int i = 8;
if ( "automatico".equals(status) ) { // caso do automatico
while ( true ) {
output.write(i);
Thread.sleep(1000);
i++;
if ( i > 10 ){
i = 8;
}
}
}
}
Read this: http://answall.com/a/2095/132 - This one
Thread.sleep(1000)
should be done on the AWT thread.– Victor Stafusa
In addition to what @Victorstafusa said, I also think the approach of checking if the component is set to interrupt the loop is not correct, there are listeners to do this kind of monitoring. It only becomes difficult until you suggest an alternative with the snippet of past code.
– user28595