Total members 11891 |It is currently Thu Jul 18, 2024 10:31 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





load class to applet- load frame class to applet with threading.
java code
import java.awt.*;
import java.util.*;
import java.applet.Applet;

public class AppletButton extends Applet implements Runnable {
int frameNumber = 1;
String loadedClass;
String btnString;
String newTitle;
int widthVar = 0;
int heightVar = 0;
Button button;
Thread frameThread;
Label labelObj;
boolean loadedBoolean = false;

public void init() {
loadedClass = getParameter("WINDOWCLASS");
if (loadedClass == null) {
loadedClass = "simpleFrame";
}

btnString = getParameter("BUTTONTEXT");
if (btnString == null) {
btnString = "Click here to bring up a " + loadedClass;
}

newTitle = getParameter("WINDOWTITLE");
if (newTitle == null) {
newTitle = loadedClass;
}

String windowWidthString = getParameter("WINDOWWIDTH");
if (windowWidthString != null) {
try {
widthVar = Integer.parseInt(windowWidthString);
} catch (NumberFormatException e) {
//Use default width.
}
}

String windowHeightString = getParameter("WINDOWHEIGHT");
if (windowHeightString != null) {
try {
heightVar = Integer.parseInt(windowHeightString);
} catch (NumberFormatException e) {
//Use default height.
}
}

setLayout(new GridLayout(2,0));
add(button = new Button(btnString));
button.setFont(new Font("Arial", Font.PLAIN, 14));

add(labelObj = new Label("", Label.CENTER));
}

public void start() {
if (frameThread == null) {
frameThread = new Thread(this, "Bringing Up " + loadedClass);
frameThread.start();
}
}

public synchronized void run() {
Class classNameToLoad = null;
Class tmp = null;
String name = null;

// Check class
try {
classNameToLoad = Class.forName(loadedClass);
} catch (Exception e) {
// The specified class isn't anywhere that we can find.
labelObj.setText("Can't create window: Couldn't find class "
+ loadedClass);
button.disable();
return;
}

// Find out whether the class is a Frame.
for (tmp = classNameToLoad, name = tmp.getName();
!( name.equals("java.lang.Object") ||
name.equals("java.awt.Frame") ); ) {
tmp = tmp.getSuperclass();
name = tmp.getName();
}
if ((name == null) || name.equals("java.lang.Object")) {

labelObj.setText("Can't create window: "
+ loadedClass +
" isn't a Frame subclass.");
button.disable();
return;
} else if (name.equals("java.awt.Frame")) {

while (frameThread != null) {
while (loadedBoolean == false) {
try {
wait();
} catch (InterruptedException e) {
}
}

//We've been asked to bring up a window.
loadedBoolean = false;
Frame window = null;
try {
window = (Frame)classNameToLoad.newInstance();
} catch (Exception e) {
labelObj.setText("Couldn't create instance of class "
+ loadedClass);
button.disable();
return;
}
if (frameNumber == 1) {
window.setTitle(newTitle);
} else {
window.setTitle(newTitle + ": " + frameNumber);
}
frameNumber++;

//Set the window's size.
window.pack();
if ((widthVar > 0) | (heightVar > 0)) {
window.resize(Math.max(widthVar,
window.size().width),
Math.max(heightVar,
window.size().height));
}

window.show();
labelObj.setText("");
}
}
}

public synchronized boolean action(Event event, Object what) {
if (event.target instanceof Button) {
//signal the window thread to build a window
labelObj.setText("Please wait until we load the class");
loadedBoolean = true;
notify();
}
return true;
}
}

class simpleFrame extends Frame {
public simpleFrame() {
resize(400, 400);
}
}




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : load class to applet- load frame class to applet
 Define class helper class to check the method existance     -  
 java abstract class,concrete class and interface     -  
 help me How do I load image from my pc to matlab     -  
 Load tweets using JQuery     -  
 How to Load Files using JFileChooser!!     -  
 relationship between the Canvas class and the Graphics class     -  
 J2me Image load Exception     -  
 Load RSS feeds and parse it with JQuery     -  
 calculate the load time for a page     -  
 Load Content to Tabs using AJAX and JQuery     -  



Topic Tags

Java Applet
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com