java - Updating Swing GUI from thread -


I have a simple application from some thread, I need some thread from swing GUI (I'm using Netbeans).

This is my main form that I need to update:

  public class mainForm javax.swing.JFrame {Private GameControl OBG; Public Mainform () {initComponents (); } Public Zero Runto () {Thread-test th1 = New Thread Test (1, 1, OBJ); Thread Test th8 = New Thread Test (8, 95000, OBJ); Thread Test Th 2 = New Thread Test (2, 100000, OBJ); Thread Test Th3 = New Thread Test (3, 120000, OBJ); Thread Test Th 4 = New Thread Test (4, 140000, OBJ); Thread Test Thub 22 = New Thread Test (22, 1000, OBJ); Thread thread 1 = 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 Zero Main (string algos []) {SwingUtilities.invokeLater (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:

  implements a public square thread test Runnable {public static mainForm main = new mainForm (); Private finals function nimbar; Private Final In Time 2 Start; Public Thread Test (int function number, int time 2 start, game control obje) {this.functionNumber = functionNumber; This.time2start = time2start; } @ Override public wide run (try.leep.sleep (time2start);} hold (exception before) {} / time delay before function 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 (); } Hold (IOException pre) {Logger.getLogger (ThreadTest.class.getName ()). Log (Level.SEVERE, null, ex); } }         break; ...  

I have in another category:

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

Each part of the code is in a different file (class). How do I apply swing workers to be able to show text on my main GUI form?

I'm going to be able to show the text on my main GUI form How do I apply?

Based on this list of features viewed in your code: / P>

  • You want to delay the initial delay to work.
  • The task has to be done in sequential order.
  • The objective of the task is to update the GUI components but not to do heavy work (i.e.: set the label's text).

The IMO suits better than the swing worker in this case. Rather than implementing the class, in the Runnabel interface you define several timers with the use of MainForm class instances to complete and update many tasks to the desired tasks (You currently create a new form as a class member of Thread Test class). For example:

  timer timer 1 = new timer (1, new editorist) {@ Override Public Voice Action Crore (Actionfree Event) {Mainframe.Runfst ();}}); Timer1.setRepeats (wrong); Timer Timer 2 = New Timer (1000, New Exhibitist) {@ Override Public Voice Action Crore (Action Avenger E) {Mainframe.resacond ();}}); Timer2.setRepeats (wrong); ... timer 1. Start (); Timer2.start (); ...  

Where mainframe # run first () should look like this:

  public zero run first () {Jlabel5 SetText ("Updated with label # 5 timer!"); }  

Comments