java - How to pass a file into another class when uploaded with JFileChooser -
i trying take in file jfilechooser can pass textstatistics class. can't seem keep reference file... appreciated.
thanks!
processtext.java:
public class processtext extends jpanel implements actionlistener { static private final string newline = "\n"; jbutton openbutton; jbutton calculate; jtextarea log; jfilechooser fc; public processtext() { super(new borderlayout()); log = new jtextarea(5, 20); log.setmargin(new insets(5, 5, 5, 5)); log.seteditable(false); jscrollpane logscrollpane = new jscrollpane(log); jpanel buttonpanel = new jpanel(); // use flowlayout buttonpanel.add(openbutton); buttonpanel.add(calculate); // add buttons , log panel. add(buttonpanel, borderlayout.page_start); add(logscrollpane, borderlayout.center); } public void actionperformed(actionevent e) { file file = null; textstatistics stat = null; if (e.getsource() == openbutton) { int returnval = fc.showopendialog(processtext.this); if (returnval == jfilechooser.approve_option) { file = fc.getselectedfile(); stat = new textstatistics(file); } } if (e.getsource() == calculate) { log.append(stat.tostring()); } } /** returns imageicon, or null if path invalid. */ protected static imageicon createimageicon(string path) { java.net.url imgurl = processtext.class.getresource(path); if (imgurl != null) { return new imageicon(imgurl); } else { system.err.println("couldn't find file: " + path); return null; } } private static void createandshowgui() { // create , set window. jframe frame = new jframe("filechooserdemo"); frame.setdefaultcloseoperation(jframe.exit_on_close); // add content window. frame.add(new processtext()); // display window. frame.pack(); frame.setvisible(true); } public static void main(string[] args) { // schedule job event dispatch thread: // creating , showing application's gui. swingutilities.invokelater(new runnable() { public void run() { // turn off metal's use of bold fonts uimanager.put("swing.boldmetal", boolean.false); createandshowgui(); } }); } } textstatistics.java
public class textstatistics implements textstatisticsinterface { public scanner filescan; public int[] countletters = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // gives starting values count of each letter. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; //initial amount of each letter. countletters[0] corresponds 'a' //countletters[1] 'b' , on. public int[] length = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //keeps word frequency lengths 0, 0, 0, 0, 0, 0, 0 }; int linecount = 0; //keeps track of lines int wordcount = 0; //keeps track of words int charcount = 0; //keeps track of characters double avg = 0; double avgfinal = 0; //average word length string strline = ""; string largestword; file file; arraylist<integer> line; //line longest word length /** * reads in 1 file @ time , 1 line @ time determine * statistics described above. * * @author ryleigh more */ public textstatistics(file file) { scanner scan; try { scan = new scanner(file); this.file = file; line = new arraylist<integer>(); int largestindex = 0; while (scan.hasnextline()) { strline = scan.nextline().tolowercase(); linecount++; charcount += strline.length() + 1; stringtokenizer tokenizer = new stringtokenizer(strline, " , .;:'\"&!?-_\n\t12345678910[]{}()@#$%^*/+-"); (int = 0; < strline.length(); i++) { char theletter = strline.charat(i); if (theletter >= 'a' && theletter <= 'z') countletters[theletter - 'a']++; while (tokenizer.hasmoretokens()) { string theword = tokenizer.nexttoken(); int currentwordlength = theword.length(); if (currentwordlength > largestindex) { largestword = theword; largestindex = currentwordlength; line.clear(); } if (largestword.equals(theword)) { line.add(linecount); } if (currentwordlength < 23 && currentwordlength > 0) { length[currentwordlength]++; } wordcount++; } } } (int j = 1; j < length.length; j++) avg += (length[j] * j); avgfinal = avg / wordcount; scan.close(); } catch (filenotfoundexception e) { system.out.println(file + " not exist"); } } /** * puts statistics in string printing processtext * class. * * @return s * @author ryleigh moore */ public string tostring() { decimalformat 2 = new decimalformat("#0.00"); string s = "statistics " + file + "\n" + "======================================================\n" + linecount + " lines\n" + wordcount + " words\n" + charcount + " characters\n" + "-----------------------------------------" + "\na= " + countletters[0] + "\t n= " + countletters[13] + "\nb= " + countletters[1] + "\t o= " + countletters[14] + "\nc= " + countletters[2] + "\t p= " + countletters[15] + "\nd= " + countletters[3] + "\t q= " + countletters[16] + "\ne= " + countletters[4] + "\t r= " + countletters[17] + "\nf= " + countletters[5] + "\t s= " + countletters[18] + "\ng= " + countletters[6] + "\t t= " + countletters[19] + "\nh= " + countletters[7] + "\t u= " + countletters[20] + "\ni= " + countletters[8] + "\t v= " + countletters[21] + "\nj= " + countletters[9] + "\t w= " + countletters[22] + "\nk= " + countletters[10] + "\t x= " + countletters[23] + "\nl= " + countletters[11] + "\t y= " + countletters[24] + "\nm= " + countletters[12] + "\t z: " + countletters[25] + "\n-----------------------------------------" + "\n length frequency" + "\n ------- ---------"; (int q = 1; q < length.length; q++) { if (length[q] > 0) s += "\n\t" + q + " =\t" + length[q]; } s += "\nthe average word length = " + two.format(avgfinal) + "\nthe longest word '" + largestword + "' , on line " + line + "\n======================================================"; return s; }
public void actionperformed(actionevent e) { file file = null; textstatistics stat = null; if (e.getsource() == openbutton) { int returnval = fc.showopendialog(processtext.this); if (returnval == jfilechooser.approve_option) { file = fc.getselectedfile(); stat = new textstatistics(file); } } if (e.getsource() == calculate) { log.append(stat.tostring()); } } the thing is, when press openbutton, creates new textstatisics that's it. doesn't append text the jtextarea. press of calculate. when openbutton pressed, local textstatistics created, nothing don't it, it's tossed out. when calculate pressed, log tries append null textstatistics.
so can either append textstatistics stat log inside of if (e.getsource() == openbutton) { or make textstatistics stats global class member persist between button presses. when openbutton pressed, textstatisitcs stat persist calculate button press.
Comments
Post a Comment