• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

date and time in applet

 
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
sorry for sending unformatted code.How could I get the output of this program.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, so that's what the code should do. Now, what does it do that deviates from that desired behavior?
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic