Question

     if ("Analyze Text File".equals(command)) {
    JFileChooser chooser = new JFileChooser();
    chooser.setMultiSelectionEnabled(true);
    if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        File[] files = chooser.getSelectedFiles();
        for (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 am trying to get a JFileChooser to pop up once I click my "Analyze to text" button, which then will allow the user to select a text file which will be processed with my code and output a bar chart.
Everything is working except the button for some reason. If anyone can help it will be 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);

       }
   }
   }
Was it helpful?

Solution

"once I click my "Analyze to text" button,"

Could be maybe the actionCommand is not "Text" and you should be checking if "Analyze to text"

if ("Text".equals(command)) {if ("Analyze to Text".equals(command)) {

// or whatever the text/actionCommand of your button is

Another seemingly obvious problem, is you have declared JFileChooser chooser but you're trying to access filechooser


UPDATE

Use this code. Change your button text back. If it doesn't work, then you changed something else in your code, and I can't help, because this works fine for 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();
            }
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top