BlankName
BlankName

Reputation: 5

Updating Swing GUI from thread

I have a simple application with some threads, I need tu update SWING GUI (I'm using Netbeans) from some thread.

This is my main form which i need to update:

public class mainForm extends javax.swing.JFrame {
private gameControll obj;
  public mainForm() {

    initComponents();
}
 public void runAuto(){
ThreadTest th1 = new ThreadTest(1, 1, obj);
ThreadTest th8 = new ThreadTest(8, 95000, obj);
ThreadTest th2 = new ThreadTest(2, 100000, obj);
ThreadTest th3 = new ThreadTest(3, 120000, obj);
ThreadTest th4 = new ThreadTest(4, 140000, obj);
ThreadTest th22 = new ThreadTest(22, 1000, obj);

Thread thread1 = new Thread(th1);
Thread thread2 = new Thread(th2);
Thread thread3 = new Thread(th3);
Thread thread4 = new Thread(th4);
Thread thread22 = new Thread(th22);
Thread thread8 = new Thread(th8);

thread1.start();
thread2.start();
thread3.start();
thread4.start();
thread22.start();
thread8.start();

}
public static void main(String args[]) {

SwingUtilities.invokeLater(new Runnable() {
  public void run() {
    mainForm app = new mainForm();
    app.setVisible(true);
        }
    });
}
    private javax.swing.JButton jButton1;
private javax.swing.JButton jButton10;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField pathtbox;                 
}

Now i have some threads:

public class ThreadTest implements Runnable {
public static mainForm main = new mainForm();
private final int functionNumber;
private final int time2start;
public ThreadTest(int functionNumber, int time2start, gameControll obj){
this.functionNumber = functionNumber;
this.time2start = time2start;

}

   @Override
   public void run(){

try{Thread.sleep(time2start);}catch(Exception ex){}//Time Delay before executing methods
switch(functionNumber){
    case 1:
//System.out.println("case 1");
        obj.runFirst();
        break;
    case 2:
   //     System.out.println("case 2");
        obj.runSecond();

        break;
    case 3:

{
    try {
        obj.runThird();
    } catch (IOException ex) {
        Logger.getLogger(ThreadTest.class.getName()).log(Level.SEVERE, null, ex);
    }
}
        break;
...

ANd in some other class i have:

  public void runFirst(){
   System.out.println("I need to show this text on jlabel5");
   }

Each part of the code is in different file (class). How would i implement Swing worker here to be able to show text on my main GUI form?

Upvotes: 0

Views: 170

Answers (1)

dic19
dic19

Reputation: 17971

How would i implement Swing worker here to be able to show text on my main GUI form?

Based on this list of features observed in your code:

  • You want an initial delay to perform tasks.
  • Tasks are intended to be performed in a sequential order.
  • Tasks are intended to update GUI components but not to perform heavy tasks (i.e.: set a label's text).

IMO Swing Timer suits better than Swing worker in this case. Instead of having a class implementing Runnable interface you could define several timers with access to the MainForm class instance in order to perform the desired actions and update the very same form (right now you create a new form as a class member of ThreadTest class). For example:

    Timer timer1 = new Timer(1, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            mainFrame.runFirst();
        }
    });
    timer1.setRepeats(false);

    Timer timer2 = new Timer(1000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            mainFrame.runSecond();
        }
    });
    timer2.setRepeats(false);
    ...
    timer1.start();
    timer2.start();
    ...

Where MainFrame#runFirst() should look like this:

public void runFirst() {
    jlabel5.setText("Label#5 updated from Timer!");
}

Upvotes: 1

Related Questions