Counter: Swing and thread : Swing Thread « Threads « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. Email
14. Event
15. File Input Output
16. Game
17. Generics
18. Hibernate
19. I18N
20. J2EE
21. J2ME
22. JDK 6
23. JSP
24. JSTL
25. Language Basics
26. Network Protocol
27. PDF RTF
28. Reflection
29. Regular Expressions
30. Scripting
31. Security
32. Servlets
33. Spring
34. Swing Components
35. Swing JFC
36. SWT JFace Eclipse
37. Threads
38. Tiny Application
39. Velocity
40. Web Services SOA
41. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Threads » Swing ThreadScreenshots 
Counter: Swing and thread
Counter: Swing and thread

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.DecimalFormat;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class SecondCounterDemo extends JPanel {
  private SecondCounterRunnable sc = new SecondCounterRunnable();

  private JButton startB = new JButton("Start");

  private JButton stopB = new JButton("Stop");

  public SecondCounterDemo() {
    stopB.setEnabled(false)// begin with this disabled

    startB.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        startB.setEnabled(false);

        Thread counterThread = new Thread(sc, "Counter");
        counterThread.start();

        stopB.setEnabled(true);
        stopB.requestFocus();
      }
    });

    stopB.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        stopB.setEnabled(false);
        sc.stopClock();
        startB.setEnabled(true);
        startB.requestFocus();
      }
    });

    JPanel innerButtonP = new JPanel();
    innerButtonP.setLayout(new GridLayout(0103));
    innerButtonP.add(startB);
    innerButtonP.add(stopB);

    JPanel buttonP = new JPanel();
    buttonP.setLayout(new BorderLayout());
    buttonP.add(innerButtonP, BorderLayout.NORTH);

    this.setLayout(new BorderLayout(1010));
    this.setBorder(new EmptyBorder(20202020));
    this.add(buttonP, BorderLayout.WEST);
    this.add(sc, BorderLayout.CENTER);
  }

  public static void main(String[] args) {
    SecondCounterDemo scm = new SecondCounterDemo();

    JFrame f = new JFrame();
    f.setContentPane(scm);
    f.setSize(320200);
    f.setVisible(true);
    f.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
  }

  class SecondCounterRunnable extends JComponent implements Runnable {
    private volatile boolean keepRunning;

    private Font paintFont = new Font("SansSerif", Font.BOLD, 14);

    private volatile String timeMsg = "never started";

    private volatile int arcLen = 0;

    public SecondCounterRunnable() {
    }

    public void run() {
      runClock();
    }

    public void runClock() {
      DecimalFormat fmt = new DecimalFormat("0.000");
      long normalSleepTime = 100;

      int counter = 0;
      keepRunning = true;

      while (keepRunning) {
        try {
          Thread.sleep(normalSleepTime);
        catch (InterruptedException x) {
          // ignore
        }
        counter++;
        double counterSecs = counter / 10.0;
        timeMsg = fmt.format(counterSecs);
        arcLen = (((intcounterSecs60360 60;
        repaint();
      }
    }

    public void stopClock() {
      keepRunning = false;
    }

    public void paint(Graphics g) {
      g.setColor(Color.black);
      g.setFont(paintFont);
      g.drawString(timeMsg, 015);

      g.fillOval(020100100);

      g.setColor(Color.white);
      g.fillOval(3239494);

      g.setColor(Color.red);
      g.fillArc(222969690, -arcLen);
    }
  }

}

           
       
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. Thread accuracy: Swing and threadsThread accuracy: Swing and threads
10. Swing and thread: invoke and waitSwing and thread: invoke and wait
11. Swing and threads: invoke laterSwing and threads: invoke later
12. Swing and threads: slideSwing and threads: slide
13. Swing and threads: scroll textSwing and threads: scroll text
14. Animation: Swing and threadAnimation: Swing and thread
15. Swing and Thread for length operationSwing and Thread for length operation
16. Swing and Thread: cancel a lengthy operationSwing and Thread: cancel a lengthy operation
17. Swing and Thread: repaintSwing and Thread: repaint
18. Swing Thread Test
19. Thread and Swing 1Thread and Swing 1
20. Thread and Swing 2Thread and Swing 2
21. Thread and Swing 3Thread and Swing 3
22. Swing Type Tester 10Swing Type Tester 10
ww__w._j___a__v_a__2__s_.___com_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.