swing - Java Applet JFileChooser button issue? -
if ("analyze text file".equals(command)) { jfilechooser chooser = new jfilechooser(); chooser.setmultiselectionenabled(true); if (chooser.showopendialog(null) == jfilechooser.approve_option) { file[] files = chooser.getselectedfiles(); (file file : files) { try { bufferedreader reader = new bufferedreader(new filereader(file)); sb.append(line); } string text = sb.tostring(); map<integer, integer> counts = getcounts(text) histogrampanel panel = new histogrampanel(width, counts, height, horizon } catch (exception ex) { ex.printstacktrace(); } } }
i trying jfilechooser
pop once click "analyze text" button, allow user select text file processed code , output bar chart.
working except button reason. if can appreciated.
// object textfile = null; else if("text".equals(command)) { jfilechooser chooser = new jfilechooser(); filenameextensionfilter filter = new filenameextensionfilter( "txt", "text", "docx"); chooser.setfilefilter(filter); int returnval = jfilechooser.showopendialog(null); if (returnval == jfilechooser.approve_option) { file file = jfilechooser.getselectedfile(); map<integer, integer> counts = getcounts(stext); int width = counts.size() * bar int horizon = height - 25; histogrampanel panel = new histogrampanel(width, counts, height, horizon); } } }
"once click "analyze text" button,"
could maybe actioncommand not "text"
, should checking if "analyze text"
if ("text".equals(command)) {
→if ("analyze text".equals(command)) {
// or whatever text/actioncommand of button is
another seemingly obvious problem, have declared jfilechooser chooser
you're trying access filechooser
update
use code. change button text back. if doesn't work, changed else in code, , can't help, because works fine me.
if ("analyze text file".equals(command)) { jfilechooser chooser = new jfilechooser(); int result = chooser.showopendialog(null); if (result == jfilechooser.approve_option) { try { file file = chooser.getselectedfile(); bufferedreader reader = new bufferedreader(new filereader(file)); stringbuilder sb = new stringbuilder(); string line; while ((line = reader.readline()) != null) { sb.append(line); } string text = sb.tostring(); map<integer, integer> counts = getcounts(text); int width = counts.size() * bar_width; int max = maxcount(counts); int height = max * increment + 100; int horizon = height - 25; histogrampanel panel = new histogrampanel(width, counts, height, horizon); joptionpane.showmessagedialog(null, panel); } catch (ioexception ex) { ex.printstacktrace(); } } }
Comments
Post a Comment