Hi,
I have an application program built on JFrame. And I want to start the program from the
applet but it can not start my application:
public StartFromWeb extends Applet implements Runnable{
private int frameNO_=1;
private
String winClass_;
private String buttonText_;
private String winTitle_;
private int reqWidth_=0;
private int reqHeight_=0;
private Button button_;
private
Thread winThread_;
private JLabel label_;
private boolean create_=false;
/**
* initial work by thread
*/
public void init() {
// deal with input parameters for applet
winClass_ = getParameter("WINDOWCLASS");
if (winClass_ == null) {
winClass_ = "TestWindow";
}
buttonText_= getParameter("BUTTONTEXT");
if (buttonText_== null) {
buttonText_ = "Click here to bring up a " + winClass_;
}
winTitle_ = getParameter("WINDOWTITLE");
if (winTitle_ == null) {
winTitle_ = winClass_;
}
String windowWidthStr = getParameter("WINDOWWIDTH");
if (windowWidthStr!= null) {
try {
reqWidth_ = Integer.parseInt(windowWidthStr);
} catch (NumberFormatException e) {
//Use default width if needed
}
}
String windowHeightStr = getParameter("WINDOWHEIGHT");
if (windowHeightStr!= null) {
try {
reqHeight_ = Integer.parseInt(windowHeightStr);
} catch (NumberFormatException e) {
//Use default height if needed
}
}
setLayout(new GridLayout(0,2));
this.add(button_=new Button(buttonText_));
button_.setFont(new Font("Time New Roman", Font.PLAIN, 12));
add(label_=new JLabel("", JLabel.CENTER));
System.out.println("Initialize...");
}
/**
* start the thread
*/
public void start() {
if (winThread_ == null) {
winThread_=new Thread(this, "Launching Up " + winClass_);
winThread_.start(); // calling run method
System.out.println("Start......");
}
}
public synchronized void run() {
Class winClassObj=null;
Class tmp=null;
String className = null;
// Make sure the window class exists and is really a JFrame.
// This has the added benefit of pre-loading the class,
// which makes it much quicker for the first window to come up.
try {
winClassObj =Class.forName(winClass_);
} catch (Exception e) {
// The specified class isn't anywhere that we can find.
label_.setText("Can't create window: Couldn't find class "
+ winClass_);
button_.disable();
return;
}
//System.out.println(winClassObj);
// Find out whether the class is a JFrame.
for (tmp=winClassObj, className=tmp.getName();
!( className.equals("java.lang.Object") ||
className.equals("javax.swing.JFrame") )
{
tmp=tmp.getSuperclass();
className = tmp.getName();
}
if ((className==null) || className.equals("java.lang.Object")) {
// error notation
label_.setText("Can't create window: "+winClass_+
" isn't a JFrame subclass!");
button_.disable();
return;
}
else if (className.equals("javax.swing.JFrame")) {
//create a window
while (winThread_!=null) {
// wait until ask for creat a window
while (create_==false) {
try {
System.out.println("wait");
wait();
} catch (InterruptedException e) {
}
}
System.out.println(className);
//asked to bring up a window.
create_=false;
Frame newframe=null;
try {
System.out.println("good");
newframe=(JFrame)winClassObj.newInstance();
System.out.println("very good");
} catch (Exception e) {
label_.setText("Can't create the application");
button_.disable();
return;
}
if (frameNO_==1) {
newframe.setTitle(winTitle_);
} else {
newframe.setTitle(winTitle_ + ": "+frameNO_);
}
frameNO_++;
//Set the window's size.
newframe.pack();
//if ((reqWidth_ > 0) | (reqHeight_ > 0)) {
// newframe.resize(Math.max(reqWidth_, newframe.size().width),
// Math.max(reqHeight_, newframe.size().height));
//}
newframe.show();
label_.setText("");
}
}
}
public synchronized boolean action(Event event, Object what) {
if (event.target instanceof Button) {
//signal the window thread to build a window
System.out.println("HI");
label_.setText("Starting up...");
create_=true;
notify();
}
return true;
}
}
class TestWindow extends JFrame {
public TestWindow() {
setSize(100, 100);
}
}
any help? thanks,
Roger
[ March 25, 2003: Message edited by: Roger Ma ]