File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Applets and the fly likes need help with applet ActionListener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "need help with applet ActionListener" Watch "need help with applet ActionListener" New topic
Author

need help with applet ActionListener

Jesse Crockett
Ranch Hand

Joined: Feb 03, 2005
Posts: 129
how do I get populateArrays() to load the files? they are in the same directory that appletviewer is using.. also, even when i clear my history and cache the page still loads an obsolete class. how is this fixed?

do I need a package declaration to include the files i need to load? how should i then compile it?

is there no way for my applet to load these String arrays from files on the server? no way around that?

[CODE]// applet for web page
//text randomizer

import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class TestGen extends JApplet implements ActionListener {
String text, gen = "", word;
JTextArea output;
JTextField input;
JButton run; // reset button later

Scanner scanVT, scanVI, scanN, scanA;

String[] vt = new String[18390];
String[] vi = new String[7230];
String[] n = new String[87284];
String[] a = new String[35347];

public void populateArrays() throws FileNotFoundException {
// initialize arrays and input string

scanVT = new Scanner(new FileReader("vt.set"));
for(int i = 0; scanVT.hasNext(); i++)
vt[i] = scanVT.next();
scanVT.close();

scanVI = new Scanner(new FileReader("vi.set"));
for(int i = 0; scanVI.hasNext(); i++)
vi[i] = scanVI.next();
scanVI.close();

scanN = new Scanner(new FileReader("n.set"));
for(int i = 0; scanN.hasNext(); i++)
n[i] = scanN.next();
scanN.close();

scanA = new Scanner(new FileReader("a.set"));
for(int i = 0; scanA.hasNext(); i++)
a[i] = scanA.next();
scanA.close();

}// populateArrays

public void init() {// initialize gui

JLabel ready, aa, bb, cc, dd;//, generate;

Container top = getContentPane();
Container middle = getContentPane();
Container bottom = getContentPane();
Container sub = getContentPane();

top.setLayout(new FlowLayout());
middle.setLayout(new FlowLayout());
bottom.setLayout(new FlowLayout());
sub.setLayout(new FlowLayout());

ready = new JLabel("/words are kept, n/words are changed : v/n/a recognized",
SwingConstants.CENTER);

aa = new JLabel(" ");
bb = new JLabel(" ");
cc = new JLabel(" ");
dd = new JLabel(" ");

input = new JTextField(35);
input.setHorizontalAlignment(JTextField.LEFT);

run = new JButton("run");

output = new JTextArea(10, 35);
output.setLineWrap(true);
output.setWrapStyleWord(true);

top.add(ready);
middle.add(input);
bottom.add(run);
sub.add(output);

run.addActionListener(this);
//reset.addActionListener(this);

} // init


public void actionPerformed(ActionEvent e ) {

if(e.getSource() == run) {

text = input.getText();
Scanner src = new Scanner(text);

while(src.hasNext()) {

word = src.next();

if(word.charAt(0) == '/') {
//keep word
gen += word + " ";
continue;
}

if(word.charAt(0) == 'v' && word.charAt(1) == '/') {

if((int)(Math.random() * 5) != 0) { // import vt

int a1 = (int)(Math.random() * 18391); //vt.set count + 1, &c.
gen += vt[a1] + " ";

} else {// import vi

int a2 = (int)(Math.random() * 7231);

gen += vi[a2] + " ";
}


} else if (word.charAt(0) == 'n' && word.charAt(1) == '/') {
// import n

int a3 = (int)(Math.random() * 87285);
gen += n[a3] + " ";


} else if (word.charAt(0) == 'a' && word.charAt(1) == '/') {
//import a
int a4 = (int)(Math.random() * 35348);
gen += a[a4] + " ";

} else {
//keep word
gen += word + " ";
continue;
}

}//while

input.setText(text);
output.setText(gen);
}// if event == run

else { // for event == reset
input.setText("");
output.setText("");
}

} // actionPerformed

}// TestGen

[ July 13, 2006: Message edited by: Jesse Crockett ]

[ July 13, 2006: Message edited by: Jesse Crockett ]

[ July 13, 2006: Message edited by: Jesse Crockett ]
[ July 13, 2006: Message edited by: Jesse Crockett ]
Jesse Crockett
Ranch Hand

Joined: Feb 03, 2005
Posts: 129
whoops, just had two identifiers mixed up..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need help with applet ActionListener
 
Similar Threads
word switcher porlebm
need some help understading why this is not working (palindrome loop)
palindrome checker, tried to debug, keeps looping why?
How we can Count the no.of words in a given String
How to get substrings between sentinel characters?