Am trying to get my program to update the progress bar values constantly within a method while performing some operations. However this does not happen until the end, and the UI freezes.
After looking around to similar questions with my problems, I tried to implement the accepted solutions (Using threads) however I cannot get it to work correctly. Is just like if they where not there.
My program contains several classes, the Main
being the one automatically created by netbeans on the JFrame Design mode, so there are certain things such as the static void main
and the public Main
that am not really sure of some of its contents. Under i will put the snippets of those methods, together with my thread implementation.
public class Main extends javax.swing.JFrame implements ActionListener, Runnable{
// I added implements ActLis, Runn.....
...
static Main _this; // I included this variable
...
public static void main(String args[]) {
Main m = new Main(); // Added by me
new Thread(m).start(); // Added by me
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Main().setVisible(true);
}
});
}
...
public Main() {
initComponents();
_this = this; // Added by me
}
...
// I also included these 2 methods in the class
public void actionPerformed(ActionEvent e) {
synchronized(this){
notifyAll();
}
}
public void run() {
try{synchronized(this){wait();}}
catch (InterruptedException e){}
progressBar.setValue(50);
}
...
private void buttonPressed(java.awt.event.MouseEvent evt) {
for(int i=0; i<=100; i++) {
for(int j=0; j<=5; j++) {
// does some work
}
run();
}
}
All the things that I commented as I added...
are things that I putted according to tutorials and answers I have seen online, but nothing seems to work and it feels like I have tried close to a million different combinations...
Thanks in advance for helping out.