Forgive me I am still really new to Java. I have this swing application which creates a small JFrame that when clicked, counts down from 2 minutes and stops at zero. I build a GUI in netbeans IDE and I read that in order to place this timer code into one of the JFrames on the GUI i would need to change this to an applet.

I don't really know how to go about this.

import javax.swing.JApplet;
public class Scoreclock extends JApplet {
}

import javax.swing.JApplet; public class Stopwatch extends JApplet { }

Netbeans generated this code while i tried to start of creating an Applet in my GUI package. when I inserted the code into the { } it came up with a build error. How would I go about doing this?

The code for my timer:

//***************Import java files necessary for this project********************//
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
//******************************************************************************//
public class Stopwatch extends JFrame implements ActionListener, Runnable
{	 
 	 // (25/15)=1.66
  	 int secondCount = 200; //integer must be a multiple of 25
     public long timeAdjust = (6200-(secondCount))*60*10;
     
     private long startTime;
     private final static java.text.SimpleDateFormat timerFormat = new java.text.SimpleDateFormat("mm : ss : SSS");
     private final JButton startStopButton= new JButton("Start/stop");
     private Thread updater;
     private boolean isRunning= false;
     private final Runnable displayUpdater= new Runnable()
     {
         public void run() // Here
         {
             displayElapsedTime(Math.max(Stopwatch.this.startTime - System.currentTimeMillis(),-timeAdjust));
         }
     };
//************************Action Performed Function***********************//
     public void actionPerformed(ActionEvent ae)
     {
         if(isRunning)
         {
             long elapsed= startTime - System.currentTimeMillis() ;
             
             isRunning= false;
             try
             {
                 updater.join();
                 // Wait for updater to finish
             }
             catch(InterruptedException ie) {}
             displayElapsedTime(elapsed);
             // Display the end-result
         }
         else
         {
  //*********************Start Command for the Function*********************//
  /*
   * Here I have built a function so that I can adjust secondCount to a number
   * and it will change the timer. When secondCount is at 25, the timer is displayed as 
   * 00:15:000 in mm:ss:sss format. When secondCount is at 200, the timer is displayed 
   * as 02:00:000 in mm:ss:sss format. 
  */
           		startTime= 2*60*1000+System.currentTimeMillis()-timeAdjust;
           		isRunning = true;
           		updater= new Thread(this);
           		updater.start();
 //***********************************************************************//
      	}
	}
//*********************Action Performed Function Ends**********************//  
//**************************Display Time Function**************************//
      private void displayElapsedTime(long elapsedTime)
      {
         startStopButton.setText(timerFormat.format(new java.util.Date(elapsedTime)));
      }
  //***********************Display Time Function Ends***********************//
  //**********************Function to Run the Timer*************************//
     public void run()
     {
         try
         {
             while(isRunning)
             {
                 SwingUtilities.invokeAndWait(displayUpdater);
                 Thread.sleep(50);
             }
         }
         catch(java.lang.reflect.InvocationTargetException ite)
         {
             ite.printStackTrace(System.err);
             // Should never happen!
         }
         catch(InterruptedException ie) {}
         // Ignore and return!
     }
 //************************Function to Run Timer Ends************************//
 //**********************Create the StartStop Button*************************//
     public Stopwatch()
         {
         startStopButton.addActionListener(this);
         getContentPane().add(startStopButton);
         setSize(100,50);
         setVisible(true);
     }
//********************Create the StartStop Button Ends***********************//     

	public static void main(String[] arg)
 	{
         new Stopwatch().addWindowListener(new WindowAdapter()
          {
             public void windowClosing(WindowEvent e)
             {
                 System.exit(0);
             }
         });
     }
}

Recommended Answers

All 4 Replies

Hi,

Basically you just have to remove the main method from your frame class
and your applet should look like this

public class Scoreclock extends JApplet {
// init is for applets same as main is for desktop applications
  public void init(){ 
     new Stopwatch();
  }
}

Scoreclock class and Stopwatch class are in the same package.

Here you can read more about Applets

GL

Thanks for you help. :)

It almost works perfectly.

import javax.swing.JApplet;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class Scoreclock extends JApplet {

class Stopwatch extends JFrame implements ActionListener, Runnable
{
 	 // (25/15)=1.66
  	 int secondCount = 200; //integer must be a multiple of 25
         public long timeAdjust = (6200-(secondCount))*60*10;

         private long startTime;
         private final static java.text.SimpleDateFormat timerFormat = new java.text.SimpleDateFormat("mm : ss : SSS");
         private final JButton startStopButton= new JButton("Start/stop");
         private Thread updater;
         private boolean isRunning= false;
         private final Runnable displayUpdater= new Runnable()
         {
          public void run() // Here
          {
             displayElapsedTime(Math.max(Stopwatch.this.startTime - System.currentTimeMillis(),-timeAdjust));
         }
     };
//************************Action Performed Function***********************//
     public void actionPerformed(ActionEvent ae)
     {
         if(isRunning)
         {
             long elapsed= startTime - System.currentTimeMillis() ;

             isRunning= false;
             try
             {
                 updater.join();
                 // Wait for updater to finish
             }
             catch(InterruptedException ie) {}
             displayElapsedTime(elapsed);
             // Display the end-result
         }
         else
         {
  //*********************Start Command for the Function*********************//
  /*
   * Here I have built a function so that I can adjust secondCount to a number
   * and it will change the timer. When secondCount is at 25, the timer is displayed as
   * 00:15:000 in mm:ss:sss format. When secondCount is at 200, the timer is displayed
   * as 02:00:000 in mm:ss:sss format.
  */
           		startTime= 2*60*1000+System.currentTimeMillis()-timeAdjust;
           		isRunning = true;
           		updater= new Thread(this);
           		updater.start();
 //***********************************************************************//
      	}
	}
//*********************Action Performed Function Ends**********************//
//**************************Display Time Function**************************//
      private void displayElapsedTime(long elapsedTime)
      {
         startStopButton.setText(timerFormat.format(new java.util.Date(elapsedTime)));
      }
  //***********************Display Time Function Ends***********************//
  //**********************Function to Run the Timer*************************//
     public void run()
     {
         try
         {
             while(isRunning)
             {
                 SwingUtilities.invokeAndWait(displayUpdater);
                 Thread.sleep(50);
             }
         }
         catch(java.lang.reflect.InvocationTargetException ite)
         {
             ite.printStackTrace(System.err);
             // Should never happen!
         }
         catch(InterruptedException ie) {}
         // Ignore and return!
     }
 //************************Function to Run Timer Ends************************//
 //**********************Create the StartStop Button*************************//
     public Stopwatch()
         {
         startStopButton.addActionListener(this);
         getContentPane().add(startStopButton);
         setSize(100,50);
         setVisible(true);
     }
//********************Create the StartStop Button Ends***********************//
    public void init()
    {
        // TODO start asynchronous download of heavy resources
        new Stopwatch().addWindowListener(new WindowAdapter()
          {
             public void windowClosing(WindowEvent e)
             {
                 System.exit(0);
             }
          });
    }
     // TODO overwrite start(), stop() and destroy() methods
}
}

The only problem I am having now is on line 14

private final static java.text.SimpleDateFormat timerFormat = new java.text.SimpleDateFormat("mm : ss : SSS");

the error that is constantly popping up is that inner classes cannot have static declarations. Did I just put it in the wrong spot of the applet code. (IE move it out of the inner class)?

Thanks

You don't have to declare the Stopwatch class as inner, you can move it in another file, in this way you can have static declarations.
And the init() method is part of the class that extends JApplet (Scoreclock in your case):
like this:

// file Stopwatch.java
public class Stopwatch extends JFrame implements ...{
   
   // code

}

// file Scoreclock.java
public class Scoreclock extends JApplet{

   public void init(){
      new Stopwatch();
      // you don't have to add the window listener for the applet      
   }
      // the method below is used by an applet to do
      // what is required when the applet is destroyed (page closed ...)
   public void destroy(){
      // code performed when applet is closed
   }

   // code ...

}

Check the tutorial posted before for more informations

It worked, thanks so much for your help :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.