D
D
Dmitry Borisenko2017-02-01 15:40:56
Java
Dmitry Borisenko, 2017-02-01 15:40:56

Why is the ProgressBar not showing on button click?

Good afternoon! I'm just learning to write in Java, so don't blame me if the question is not very deep and intricate)
Can someone tell me why the ProgressBar does not work normally in the code below? In theory, when you click on the btnCalc button, there should be an imitation of serious salary calculations))), but the ProgressBar appears only simultaneously with the display of information in the text field after the loop ( txtAr.setText(txtOutput), and immediately from 100%, that is, there is no dynamics Changes! Separately, the code fragment with the progressbar works. I can't figure out what's wrong
public void actionPerformed(ActionEvent event){
if (event.getSource() == btnCalc){
txtAr.setText("");
summaDayHours = CalcOfHours.howManyDaysHours(sutki,nochi, dni,d89);
summaNightHours = CalcOfHours.howManyNightsHours(sutki,nochi);
txtOutput = "....................................................... .;
prBar.setVisible(true); // This is a progressbar
for (int i = 0; i <= 100; i++){
prBar.setValue(i);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
txtAr.setText(txtOutput);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Kornachev, 2017-02-01
@zelan

The fact is that your calculations ("heavy imitation") are performed in a thread that is responsible for the operation and rendering of graphics (components). While your simulation cycle is running, the ENTIRE program interface is blocked and not rendered. Heavy calculations should be done in separate threads.
Read about SwingWorker, and in general about interface synchronization.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question