Swing and Thread: repaint : Swing Thread « Threads « Java






Swing and Thread: repaint

Swing and Thread: repaint
  

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.lang.reflect.InvocationTargetException;
import java.text.DecimalFormat;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

public class DigitalTimer extends JLabel {
  private volatile String timeText;

  private Thread internalThread;

  private volatile boolean noStopRequested;

  public DigitalTimer() {
    setBorder(BorderFactory.createLineBorder(Color.black));
    setHorizontalAlignment(SwingConstants.RIGHT);
    setFont(new Font("SansSerif", Font.BOLD, 16));
    setText("00000.0"); // use to size component
    setMinimumSize(getPreferredSize());
    setPreferredSize(getPreferredSize());
    setSize(getPreferredSize());

    timeText = "0.0";
    setText(timeText);

    noStopRequested = true;
    Runnable r = new Runnable() {
      public void run() {
        try {
          runWork();
        } catch (Exception x) {
          x.printStackTrace();
        }
      }
    };

    internalThread = new Thread(r, "DigitalTimer");
    internalThread.start();
  }

  private void runWork() {
    long startTime = System.currentTimeMillis();
    int tenths = 0;
    long normalSleepTime = 100;
    long nextSleepTime = 100;
    DecimalFormat fmt = new DecimalFormat("0.0");

    Runnable updateText = new Runnable() {
      public void run() {
        setText(timeText);
      }
    };

    while (noStopRequested) {
      try {
        Thread.sleep(nextSleepTime);

        tenths++;
        long currTime = System.currentTimeMillis();
        long elapsedTime = currTime - startTime;

        nextSleepTime = normalSleepTime
            + ((tenths * 100) - elapsedTime);

        if (nextSleepTime < 0) {
          nextSleepTime = 0;
        }

        timeText = fmt.format(elapsedTime / 1000.0);
        SwingUtilities.invokeAndWait(updateText);
      } catch (InterruptedException ix) {
        // stop running
        return;
      } catch (InvocationTargetException x) {
        x.printStackTrace();
      }
    }
  }

  public void stopRequest() {
    noStopRequested = false;
    internalThread.interrupt();
  }

  public boolean isAlive() {
    return internalThread.isAlive();
  }

  public static void main(String[] args) {

    JFrame f = new JFrame();
    f.getContentPane().setLayout(new FlowLayout());
    f.getContentPane().add(new DigitalTimer());
    f.setSize(250, 100);
    f.setVisible(true);
  }
}

           
         
    
  








Related examples in the same category

1.Is Event Dispatcher ThreadIs Event Dispatcher Thread
2.GUI clock
3.Race Conditions using Swing ComponentsRace Conditions using Swing Components
4.Eliminating race Conditions using Swing ComponentsEliminating race Conditions using Swing Components
5.Using the Runnable interfaceUsing the Runnable interface
6.Write your Beans this way so they can run in a multithreaded environmentWrite your Beans this way so they can run in a multithreaded environment
7.User interface responsiveness
8.InvokeExample: Swing and threadInvokeExample: Swing and thread
9.Counter: Swing and threadCounter: Swing and thread
10.Thread accuracy: Swing and threadsThread accuracy: Swing and threads
11.Swing and thread: invoke and waitSwing and thread: invoke and wait
12.Swing and threads: invoke laterSwing and threads: invoke later
13.Swing and threads: slideSwing and threads: slide
14.Swing and threads: scroll textSwing and threads: scroll text
15.Animation: Swing and threadAnimation: Swing and thread
16.Swing and Thread for length operationSwing and Thread for length operation
17.Swing and Thread: cancel a lengthy operationSwing and Thread: cancel a lengthy operation
18.Thread and Swing 1Thread and Swing 1
19.Thread and Swing 2Thread and Swing 2
20.Thread and Swing 3Thread and Swing 3
21.Swing Type Tester 10Swing Type Tester 10
22.Swing and Threading
23.SwingUtilities.invokeLater and swing thread
24.An abstract class to perform lengthy GUI-interacting tasks in a dedicated thread.
25.This program demonstrates that a thread that runs in parallel with the event dispatch thread can cause errors in Swing components