File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Applets and the fly likes date and time in applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "date and time in applet" Watch "date and time in applet" New topic
Author

date and time in applet

preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Hi,
I need to display current date and time in a applet.I tried the below program.

code:

import java.io.*;
import java.sql.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
import java.awt.*;
import java.util.Date;
import java.text.DateFormat;
import java.lang.Runnable;
import java.lang.Thread;
import java.lang.InterruptedException;
import java.awt.Graphics;
public class Clock extends Applet implements Runnable
{
protected Thread timerThread;
Date date = new Date();
protected DateFormat format = DateFormat.getTimeInstance();
public void start() {
timerThread = new Thread(this, "Clock");
timerThread.start();
}
public void stop() {
if (timerThread == null)
return;
timerThread = null;
}
public void run() {
while (timerThread != null) {
repaint(); // request a redraw
try {
timerThread.sleep(1000);
} catch (InterruptedException e){ /* do nothing*/ }
}

}

public void paint(Graphics g) {
date.setTime(System.currentTimeMillis());
g.drawString(format.format(date), 2, 10);

}
}.

It runs well.but when compiling it shows the following error:
Exception in thread "main" java.lang.NoSuchMethodError: main.please help me.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42950
    
  70
First off, please UseCodeTags when posting code of any length. Unformatted code is hard to read.

but when compiling it shows the following error:
Exception in thread "main" java.lang.NoSuchMethodError: main

This error happens during runtime -not during compiling- if you try to run a class (using the "java" command) that does not have a "main" method. The code you posted doesn't have this method, so it can't be run as an application (which is actually fine, since it is an applet). If you want to run it from the command line, you'll have to add a "main" method that instantiates the class and displays it in a Frame. But why would you want to do that?
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Hi,
sorry for sending unformatted code.How could I get the output of this program.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42950
    
  70
What do you mean by "the output"? The string being displayed in the GUI? If the code doesn't work as it is, tell us what it does, and what you were expecting it to do.
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
I am having an open source code which displays applet buttons.If a button is clicked the call will be disposed and the next call comes automatically.within that gap between the disposition of a call and the next incoming call,the page should display the time .how to do this?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42950
    
  70
OK, so that's what the code should do. Now, what does it do that deviates from that desired behavior?
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Now i am just trying to display a timer in an applet.for that i tried this code.but it displays nothing.I think i am going wrong.
.
I expect a timer in the applet frame.but it is not working.what should i do?please help me.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42950
    
  70
The class shouldn't extend JLabel. Labels are meant for displaying text, not for sticking other elements inside of them. You can add another label that shows the text that's currently in the super call.

The addMouseListener method should be called for the frame object instead.

I would construct the panel completely before adding it to the frame.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: date and time in applet