public void init(); // the JApplet is loaded public void start(); // the browser applet is viewed public void stop(); // the browser leaves the applet page public void destroy(); // the JApplet is unloadedAn applet is activated through HTML-specific tags, often in a HTML file called as stub, whose sole purpose is to cause the browser/viewer to load the applet. Here is a sample stub file:
<applet code="MyApplet"
width="300" height="100"
>
</applet>
The width and height attributes are required.
In order to run the MyApplet class we would co-locate
MyApplet.class along with MyApplet.html
in the same directory and then point our browser
to MyApplet.html, or, via appletviewer:
appletviewer MyApplet.htmlAlternatively, one can use the attribute
codebase="directory-where-applet-code-is"
to make
the HTML stub file's location independent of the code's location.
HtmlConverter MyApplet.htmlA backup copy of MyApplet.html is made in a separate directory and MyApplet.html is converted to the object form. You can also simply run HtmlConverter without parameters and allow it to choose the target applet stub file.
<applet code="MyApplet" archive="JarFile1.jar,JarFile2.jar" width="300" height="100" > Applet Didn't Load </applet>This is particularly useful for accessing the MySQL driver. Furthermore, the class MyApplet.class itself may be part of a JAR file and we can thereby avoid any providing any class files, per se, with the applet.
<applet code="MyApplet" width="300" height="100" > <param name="param_name1" value="param_value1" /> <param name="param_name2" value="param_value2" /> Applet Didn't Load </applet>Within the applet itself, these parameters can then be read using the getParameter function as follows:
String value = getParameter("param_name1");
...
setDefaultCloseOperation, setSize, setVisible
keytool -keystore mystore -genkeyThis invokes a dialog in which you need to enter two passwords (they can be the same, but we'll make the different to illustrate how they're used). You can make the passwords whatever you want.
Enter keystore password: keystorePwd (at least 6 chars) Now you are asked for 6 pieces of information of which the default is "Unknown". For simplicity take this default. Is CN=Unknown, OU=Unknown, O=Unknown, L=Unknown, ST=Unknown, C=Unknown correct? [no]: yes Enter key password for (RETURN if same as keystore password): keyPwdThis generates a keystore which is a file whose name is mystore containing one digital signature key, which uses the default name mykey. The first password entered, keystorePwd, is needed to unlock the keystore file and the second (keyPwd) grants usage of the individual key.
You can validate the contents of mystore as follows:
keytool -keystore mystore -listThe listing of contents requires the keystore password:
Enter keystore password: keystorePwd
Keystore type: jks
Keystore provider: SUN
Your keystore contains 1 entry
mykey, ...
jarsigner -keystore mystore SOMETHING.jar mykeyYou're signing the JAR file using the key mykey held in nystore. You'll need both the passwords to do so:
Enter Passphrase for keystore: keystorePwd Enter key password for mykey: keyPwdUse the "jar tf" command to observe the contents of the JAR file to see that the signature information is stored in the META-INF directory which is part of every JAR file:
jar tf SOMETHING.jar META-INF/MANIFEST.MF META-INF/MYKEY.SF META-INF/MYKEY.DSA META-INF/ ...Afterwards, when you run the applet in a browser it will query you about accepting the signature.
jarsigner -keystore mystore WordSearchApplet.jar mykeyin the dist folder.
In this case we really need to deal with the security restrictions. Running appletviewer probably will not work without extra effort. Try running it wordsearchapplet.html in a browser.
<applet code="views.Applet"
codebase="dist"
archive="WordSearchApplet.jar"
width="450" height="450" >
</applet>
Afterwards, go back to the Projects window.
import java.awt.event.*; import javax.swing.text.Highlighter;and these member functions:
public String getKeyFieldText() {
return keyfield.getText();
}
public void addSearchActionListener(ActionListener al) {
search.addActionListener(al); // Search button pressed
keyfield.addActionListener(al); // return typed in textfield
}
public Highlighter getDisplayHighlighter() {
return display.getHighlighter();
}
public String getDisplayText() {
return display.getText();
}
public void setDisplayText(String text) {
display.setText(text);
display.setCaretPosition(0);
}
public void addOpenActionListener(ActionListener al) {
open.addActionListener(al);
}
and add these lines to the init function:
final Applet gui = this;
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
new wordsearchapplet.Main(gui);
}
});
package wordsearchapplet;
import views.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.util.regex.*;
import javax.swing.text.*;
import java.awt.Color;
public class Main {
private JFileChooser fc = new JFileChooser();
private Applet frame;
private String readTextFile(File f) throws Exception {
FileInputStream istr = new FileInputStream(f);
InputStreamReader irdr = new InputStreamReader(istr); // promote
int size = (int) f.length(); // get the file size (in bytes)
char[] data = new char[size]; // allocate char array of right size
irdr.read(data, 0, size); // read into char array
irdr.close();
String contents = new String(data);
return contents;
}
private void highlightWord(String keyword) {
String patternStr = "\\b" + keyword + "\\b";
Pattern pattern = Pattern.compile(patternStr, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(frame.getDisplayText());
Highlighter.HighlightPainter myPainter
= new DefaultHighlighter.DefaultHighlightPainter(Color.yellow);
Highlighter displayHighlighter = frame.getDisplayHighlighter();
displayHighlighter.removeAllHighlights();
while (matcher.find()) {
int start = matcher.start(), end = matcher.end();
try {
displayHighlighter.addHighlight(start, end, myPainter);
} catch (Exception x) {
x.printStackTrace(); // we did something wrong!
}
}
}
public Main(Applet gui) {
this.frame = gui;
frame.addOpenActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
int status = fc.showOpenDialog(frame);
if (status != JFileChooser.APPROVE_OPTION) {
return;
}
try {
frame.setDisplayText(readTextFile(fc.getSelectedFile()));
} catch (Exception x) {
JOptionPane.showMessageDialog(frame, x);
}
}
});
frame.addSearchActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String keyword = frame.getKeyFieldText().trim();
if (!keyword.matches("\\w+")) {
JOptionPane.showMessageDialog(frame, "illegal keyword");
} else {
highlightWord(keyword);
}
}
});
}
public static void main(String[] args) {
}
}