• 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

start an Application

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
setVisible( true ); ? (Called on the frame)
 
Roger Ma
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first line is :
public class StartFromWeb extends Applet implements Runnable{
the problem is
newframe=(JFrame)winClassObj.newInstance();
does not creat instance, but if the application is based on Frame this works.

Roger
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know if this will help much, but when I create GUIs with Java, I usually don't have much code in my class that extends JFrame. Instead, it simply adds an object of a class that extends JPanel and all of the user interface code is in the panel class. This way, I can easily convert to an applet by extending JApplet and adding an instance of my existing panel class to it. Is it possible to re-work your project in this manner? And more importantly, do you think it will solve your problem? This is just a suggestion, so take it worth a grain of salt.
Keep coding!
Layne
 
Roger Ma
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I DO the same way as well! But here I just want to start an application from a button applet in the homepage, that is different thing.
any other idea!

Roger
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahh, I see. I don't have a solution to your problem off the top of my head. Where is the application located? On the server or on the client machine? There are strict security restrictions with applets, as you are probably aware. I think you are probably running into one of these.
If the application is located on the server, perhaps you should change it to an applet. Then when the button is clicked in the original applet, it can open a new browser window and request the HTML page which contains this new applet. Will that work for you?
 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe your problem is here:
// wait until ask for creat a window
while (create_==false) {
try {
System.out.println("wait");
wait();
} catch (InterruptedException e) {
}

You are waiting for someone to click a button before creating, but how can anyone even see a button without a frame/window to put it on? The event is never kicked off and create_ is always false.
 
Roger Ma
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is with instance of my application class. I figure out a simple application, a text area copy demo, which can run on the click of the button of the applet.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class TextAreaDemo extends JFrame {
private JTextArea t1,t2;
private JButton copy;
public TextAreaDemo()
{
super("A TextArea Demo program");
Box b=Box.createHorizontalBox();
String s="This is a demo string to \n" +
"illustrate copying text \n" +
"from one TextArea to \n" +
"another TextArea using an\n"+
"external event\n";
t1=new JTextArea(s, 10, 15);
b.add(new JScrollPane(t1));
copy=new JButton("Copy >>>");
copy.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
t2.setText(t1.getSelectedText());
}
}
);
b.add(copy);
t2=new JTextArea(10,15);
t2.setEditable(false);
b.add(new JScrollPane(t2));
getContentPane().add(b);
setSize(500,200);
show();
}
// the main method
public static void main(String args[])
{
TextAreaDemo app=new TextAreaDemo();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
in the html:
<APPLET CODE="StartFromWeb.class" WIDTH=300 HEIGHT=100 CODEBASE="classes" ARCHIVE="###.jar">
<param name=WINDOWCLASS value="TextAreaDemo">
<param name=buttonText value="Start the program">
this will show a button applet starting an application by clicking.
But when I apply to my application which include some file operations like reading an icon, creating JFileChooser..., the applet does not run by explorer although can partly run by appletviewer(JFileChooser can not be creat by setting: netscape.security.PrivilegeManager.enablePrivilege). I wonder if this is because of the security problem since I am not familar with java 2 sercurity model.
it is good if any one can test the code I paste and gives some real comments.

Roger
reply
    Bookmark Topic Watch Topic
  • New Topic