Applet into Jframe

how could i call applet to JFrame?for example: i want to call a clock applet to my frame.
[103 byte] By [student14a] at [2007-11-26 18:23:24]
# 1
"Call a clock applet to my frame"? What does that mean? Are you saying that you want to adapt an applet and convert its functionality to appear in a JFrame?
DrLaszloJamfa at 2007-7-9 5:57:18 > top of Java-index,Java Essentials,New To Java...
# 2
i mean that i need a clock to appear into my JFrame.
student14a at 2007-7-9 5:57:18 > top of Java-index,Java Essentials,New To Java...
# 3
> i mean that i need a clock to appear into my JFrame.With a properly written JApplet, that should be a snap.After all, JApplet and JFrame are very similar -- both are RootPaneContainers. On the other hand, if the JAppletwas poorly written -- what can one
DrLaszloJamfa at 2007-7-9 5:57:18 > top of Java-index,Java Essentials,New To Java...
# 4

this is the applet which i want to add it to my JFrame:

import java.awt.Graphics;

import java.util.Date;

public class Clock extends java.applet.Applet implements Runnable {

Thread clockThread = null;

public void start() {

if (clockThread == null) {

clockThread = new Thread(this, "Clock");

clockThread.start();

}

}

public void run() {

// loop terminates when clockThread is set to null in stop()

while (Thread.currentThread() == clockThread) {

repaint();

try {

clockThread.sleep(1000);

} catch (InterruptedException e){

}

}

}

public void paint(Graphics g) {

Date now = new Date();

g.drawString(now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds(), 5, 10);

}

public void stop() {

clockThread = null;

}

}

student14a at 2007-7-9 5:57:18 > top of Java-index,Java Essentials,New To Java...
# 5

> this is the applet which i want to add it to my

> JFrame:

>

> import java.awt.Graphics;

> import java.util.Date;

>

> public class Clock extends java.applet.Applet

> implements Runnable {

>

>Thread clockThread = null;

>public void start() {

>if (clockThread == null) {

>clockThread = new Thread(this, "Clock");

>clockThread.start();

>}

>}

>public void run() {

> // loop terminates when clockThread is set to

> null in stop()

> while (Thread.currentThread() == clockThread)

> {

>repaint();

> try {

>clockThread.sleep(1000);

> atch (InterruptedException e){

> }

>}

> public void paint(Graphics g) {

>Date now = new Date();

> g.drawString(now.getHours() + ":" + now.getMinutes()

> + ":" + now.getSeconds(), 5, 10);

>}

> public void stop() {

>clockThread = null;

> }

>

replace 'java.applet.Applet' with 'javax.swing.JPanel' , after that

add that clock object to your JFrame

p_epia at 2007-7-9 5:57:18 > top of Java-index,Java Essentials,New To Java...
# 6
i did like , and add the clock object to the Jframe but i have and exception :IllegalArgumentException: adding a window to a containerMessage was edited by: student14
student14a at 2007-7-9 5:57:18 > top of Java-index,Java Essentials,New To Java...
# 7

> i did like , and add the clock object to the Jframe

> but i have and exception :

>

> IllegalArgumentException: adding a window to a

> container

>

> Message was edited by:

> student14

to add object to frame such as jpanel , you cannot use add method .

first your should use method getContentPane() , and next step add that object

for example in jframe class

this.getContentPane().setLayout(new BorderLayout());

this.getContentPane().add(clock, BorderLayout.CENTER);

p_epia at 2007-7-9 5:57:18 > top of Java-index,Java Essentials,New To Java...
# 8
thats wgat i exactly did,,but i have the same exception
student14a at 2007-7-9 5:57:18 > top of Java-index,Java Essentials,New To Java...
# 9
I haven't heard of anyone adding an JApplet to a JFrame!
qUesT_foR_knOwLeDgea at 2007-7-9 5:57:18 > top of Java-index,Java Essentials,New To Java...
# 10
may be its not acceptable,,but i exactly need to add a clock to my JFram .
student14a at 2007-7-9 5:57:18 > top of Java-index,Java Essentials,New To Java...
# 11
Take a JPanel.Add the clock to the JPanel and then add the JPanel to the JFrame.
qUesT_foR_knOwLeDgea at 2007-7-9 5:57:18 > top of Java-index,Java Essentials,New To Java...
# 12
i also did that but the result is the same;;may be i have a problem in the clock class?
student14a at 2007-7-9 5:57:18 > top of Java-index,Java Essentials,New To Java...
# 13

> > i did like , and add the clock object to the

> Jframe

> > but i have and exception :

> >

> > IllegalArgumentException: adding a window to

> a

> > container

> >

> > Message was edited by:

> > student14

>

> to add object to frame such as jpanel , you cannot

> use add method .

> first your should use method getContentPane() , and

> next step add that object

>

> for example in jframe class

>

> > this.getContentPane().setLayout(new BorderLayout());

> this.getContentPane().add(clock,

> BorderLayout.CENTER);

>

If I read doc-api , if you call method

this.getContentPane().add(clock, BorderLayout.CENTER)

will throws 'NullPointerException' not

'IllegalArgumentException: adding a window to'

p_epia at 2007-7-9 5:57:18 > top of Java-index,Java Essentials,New To Java...