开发者

updating label text dynamically in Java Swing

I am creating a simple download manager in Java swing...! I have used different threads for different tasks in the download manager.

Thread1-> main Thread2-> downloadSpeedTracker Thread3-> SwingUIRendrer

In this application I am calculating the download speed by creating a thread in class downloadSpeedTracker and I am recalculating this speed after every second. I have created Thread3 in SwingUIRendrer class. Now I want to send this calculated speed to my SwingUIRendrer class.

I want to continuously update this calculated speed on the SwingUI. How do I achieve this...?


@Mohamed:

public void setUI(int speed)
{
    JLabel l1= new JLabel("Speed :"+speed);
    wind.add(l1); 
    wind.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    wind.setSize(200, 60);
}

I am calling the SetUI method in a while loop. Everytime the method is called, a new value for speed is sent to it; but when I run the program, the speed is getting overwritten on the earlier displyed speed. I w开发者_如何学运维ant to clear the earlier value of speed from the UI and then write the new one.


The 3rd thread is not needed because updating UI must be done in the event dispatch thread.

One solution could be:

  1. Define an interface ISpeedListener, something like below:

    public interface ISpeedListener {  
      public void onSpeedChange(long newSpeed);  
    }  
    
  2. In the DownloadSpeedTracker, create a list (ArrayList) to hold the listeners and add methods to allow it to add/remove speed listeners. When the speed changes, notify the listeners by calling the onSpeedChange method.

    private List<ISpeedListener> listeners = new ArrayList<ISpeedListener>();
    
    public void addSpeedListener(ISpeedListener l) {
      listeners.add(s)
    }
    
    public void removeSpeedListener(ISpeedListener l) {
      listeners.remove(s)
    }
    
    // call this method when the speed changes
    private void notifyListeners(long newspeed) {
      foreach (ISpeedListener l : listeners) {
        l.onSpeedChange(newspeed);
      }
    }
    
  3. In the SwingUIRendrer class, make it implement the ISpeedListener class, in the onSpeedChange method, update the related UI component using the SwingUtilites.invokeLater to make the operations on UI component on the event dispatch thread.

    public class SwingUIRendrer implements ISpeedListener {
      public void onSpeedChange(final long newSpeed) {
    
        SwingUtilites.invokeLater(new Runnable() {
          public void run() {
            //TODO.... update the label with new speed.                
          }
        }
        );
      }
    }
    

Before running the DownloadSpeedTracker, create an instance of SwingUIRendrer and add it to DownloadSpeedTracker as a listener.

SwingUIRendrer render = new SwingUIRendrer(<other parameter>);
DownloadSpeedTracker tracker = new DownloadSpeedTracker();
tracker.addSpeedListener(render);
render.start();


Save the calculated speed in a Class say like "Speed". Make that class is to be shared, and reading and writing that shared variable should be mutually exclusive. I mean synchronize that variable accessing point.

Hope this help.


Not updating of the gui is a general know issue, in this tutorial is explained exactly how it works with clear answers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜