Can Some One please help me?!

Here is my problem. It has taken me severial weeks to figure out how to update my GUI with the progress of the application. I had done so by running a javax.swing.Timer in the main thread and running the west of the application in a second thread, using SwingWorker class. And all things work fine. Except, when ever the code in second thread stops, as in it is completed or interrupted. I can not get the application to run again from the start button. I have to completely close the application.
ie, w/ code:
   //Create a timer.
   timer = new Timer(10, new ActionListener() {
     public void actionPerformed(ActionEvent evt) {
       String prevtext = " ";
       if(setprogresstext != " ") {
         if(prevtext != setprogresstext) {
        frame.progresstextarea.append(setprogresstext);
        prevtext = setprogresstext; 
        setprogresstext = " ";
         } else {
        setprogresstext = " ";
       if (done) {
         done = false;
         Toolkit.getDefaultToolkit().beep();
         timer.stop();
         frame.startbutton.setEnabled(true);
         frame.exitbutton.setVisible(true);
         frame.exitbutton.setEnabled(true);
         frame.cancelbutton.setVisible(false);
         frame.cancelbutton.setEnabled(false);
      frame.progresstextarea.setText(" ");
   final SwingWorker worker = new SwingWorker() {
    public Object construct() {
      start1();
      finished();
      return setprogresstext;
  frame.exitbutton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent event) {
      start_button1 = false;
      stop_button1 = true;
      System.exit(0);
  frame.startbutton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent event) {
      setprogresstext = " ";
      done = false;
      frame.startbutton.setEnabled(false);
      frame.exitbutton.setVisible(false);
      frame.exitbutton.setEnabled(false);
      frame.cancelbutton.setVisible(true);
      frame.cancelbutton.setEnabled(true);
      start_button1 = true;
      stop_button1 = false;
      timer.start();
      worker.start();
  frame.cancelbutton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent event) {
      setprogresstext = " ";
      start_button1 = false;
      stop_button1 = false;
      frame.startbutton.setEnabled(true);
      frame.exitbutton.setVisible(true);
      frame.exitbutton.setEnabled(true);
      frame.cancelbutton.setVisible(false);
      frame.cancelbutton.setEnabled(false);
     /worker.interrupt();
      timer.stop();
}If I click on the start button, as is above, and let everything run through to it's end completion. Or if I click on the start button and then cancel the task some time after it starts. And then click on the start button again, once it is re-activated, nothing happens. It just hangs there. The start button it locked, but nothing happens. It won't even print to the system.out the test point information I have setup, for debuging.
And yes, I am using the timer.stop() and worker.interrupt() to proper cancel/stop the timer and thread.
If I remove the timer and place the working code back in the main thread, everything works. (Of course I don't not have any updates to the progress window.) And I am able to press start button again and the code will execute again w/o any problems.
Nothing is changed in the working code, except to run it as a second thread using SwingWorker.
Can anyone tell me what is causing this problem?
The only thing I can think of is that I need to have the timer in the second thread and the working code in the main thread. But, I can not figure out how to do that.
Or can someone show me how to implement my timer in a second thread, while keeping my working code in the main thread?

Hi,
Not exactly sure what you are doing, but the code below does what you want. At least it sounds like it. It starts a second thread that performs something in the background while the GUI remains active to user inputs (hence the Stop button!).
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Progressor extends JFrame
  private JProgressBar progressBar;
  private int workProgress;
  private Timer timer;
  public Progressor()
    super( "Progressor!" );
    progressBar = new JProgressBar( 0, 1000 );
    progressBar.setValue( 0 );
    progressBar.setStringPainted( true );
    JPanel f = new JPanel( new FlowLayout() );
    Container cont = getContentPane();
    f.add( progressBar );
    cont.add( f, "North" );
    JPanel p = new JPanel( new GridLayout( 0, 3 ) );
    JButton b = new JButton( "Start" );
    b.addActionListener(
      new ActionListener()
        public void actionPerformed( ActionEvent ae )
          final SwingWorker worker = new SwingWorker()
                                       public Object construct()
                                         return( new Task() );
          worker.start();
          timer.start();
    p.add( b );
    b = new JButton( "Stop" );
    b.addActionListener(
      new ActionListener()
        public void actionPerformed( ActionEvent ae )
          timer.stop();
          workProgress = 0;
    p.add( b );
    b = new JButton( "Exit" );
    b.addActionListener(
      new ActionListener()
        public void actionPerformed( ActionEvent ae )
          System.exit( 0 );
    p.add( b );
    f = new JPanel( new FlowLayout() );
    f.add( p );
    cont.add( f, "South" );
    pack();
    //Create a timer.
    timer = new Timer( 100,
                       new ActionListener()
                        public void actionPerformed(ActionEvent evt)
                          progressBar.setValue( workProgress );
                          if( workProgress >= 1000 )
                            Toolkit.getDefaultToolkit().beep();
                            timer.stop();
                            progressBar.setValue( progressBar.getMinimum() );
  // Main method for running this class.
     public static void main(String[] args)
          new Progressor().setVisible( true );
  // Inner class to fake a task being performed in background.
  class Task
    Task()
      //Fake a long task,
      //making a random amount of progress every second.
      while( workProgress < 1000 )
        try
          Thread.sleep( 1000 ); //sleep for a second
          workProgress += Math.random() * 100; //make some progress
          if( workProgress > 1000 )
            workProgress = 1000;
        catch (InterruptedException e) {}
}I hope it helps you with your problem ...
Regards,
Manfred.

Similar Messages

Maybe you are looking for

  • How to use discrete PID in FPGA module?

    Hi, I want to implementa PID controller for a Two tank set up, where my aim is to control the level in the tank.so my process variable will be in centimeters and my set point will also be in centimeters.The control output calculated will be milli amp

  • FLB1- Lockbox Processing

    Hello Folks- When we view Post with Clearing:  Process open items (FB05), we cannot see the Amount entered, Assigned, Difference postings or Not assigned without having to drag the bar over.  Is there a way to modify the settings so we can avoid this

  • How to remove dg broker and create 2nd physical standby?

    We have a dg broker setup 2 years ago for our production DB for DG standby. But we never used it. It kept down since then. However, we still have the configuration in the database like this: Primary: log_archive_config string DG_CONFIG=(dxp1p, DXP1P_

  • Infoobject not available in the query

    hai gurus We have appended a cube with info object that had a query already on it but that infoobject is not available in query hw to refresh the query designer to get new appended info object TQ KEERTHi

  • Change Amount depending on number of decimals for a given currency

    Hi All, I have a requirement to change the amount field based on the number of decimals. I am using 'BAPI_CURRENCY_GETDECIMALS' to get the number of decimals for the given currency. I am having an amount field(say 3000.00), that needs to be changed.