0

I want to update a JLabel through a separate function class. I'm doing a live bubblesort using a thread, but it's not updating even though the value has changed; the text on the JLabel remains the same.

 public class bubbleSort extends Main implements Runnable{

       @Override
        public void run(){
            String holder;
            for(int i=0;i<5;i++){
                System.out.println( Main.str1.getText().compareTo(Main.str2.getText()));
                if(Main.str1.getText().compareTo(Main.str2.getText())>0){
                    holder=Main.str1.getText();
                    Main.str1.setText(Main.str2.getText());
                    Main.str2.setText(holder);
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e){
                }  
            }
        }
    }
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 1
    So, you're going to want to have a look at [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/), [Worker Threads and SwingWorker](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html) and [How to use Swing Timers](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) and it wouldn't hurt to have a look at [Model-View-Controller](http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller) – MadProgrammer Mar 04 '17 at 06:48
  • 1
    You may also want to have a look at [this example](http://stackoverflow.com/questions/15756210/java-multiple-graphics/15756352#15756352) – MadProgrammer Mar 04 '17 at 06:49
  • This related [example](http://stackoverflow.com/a/13205322/230513) animates Newton's method for finding roots. – trashgod Mar 04 '17 at 09:25
  • Are you actually creating a separate Thread, or just invoking the run() method directly? If you invoke run() directly then the code is probably executing on the `Event Dispatch Thread (EDT)` meaning the label can't repaint itself until the loop finishes executing. Post a proper [mcve] when you ask a question so we don't have to guess what you may or may not be doing. – camickr Mar 04 '17 at 15:58

0 Answers0