-1

this code at the moment allows me to select multiple text files which are then analysed into a bar chart, my problem is when for example 2 files are selected from the JFileChooser the bar charts are opened one after another, so first one will open then when OK is pressed the second will open, i need them both to open simultaneously side by side? If anyone can give me any pointers it would be really appreciated.

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));
                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);
                reader.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720

2 Answers2

1

JOptionPane is intended for blocking popups. Things like error messages etc. For your purpose, you should probably rely on modeless JDialogs.

Ray
  • 3,084
  • 2
  • 19
  • 27
  • Hi, is there no way to use JOptionPane as i do not have time to research and try implement JDialog s as my assignment is due in a few hours. Sorry i am a beginner so it will take me longer trying to implement JDialog s.. – user3392311 Mar 07 '14 at 11:28
1

As Ray suggested just use JDialogss so you can control the modality. Remember JDialog works just like JFrame, so if you know how to make a JFrame, then JDialog should be easy. But with JDialog you get to control the modality.

for (File file : files) {
    try {
        ...
        HistogramPanel panel = new HistogramPanel(width, counts, height, horizon);
        JDialog dialog = new JDialog();
        dialog.setModal(false);        <---- very important
        dialog.setLayout(new BorderLayout());
        dialog.add(panel);
        dialog.pack();
        dialog.setLocationByPlatform(true);
        dialog.setVisible(true);
    } catch (...)
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • if this helps you solve your problem, please accept @Ray answer. I'm just trying to help you out with an example since your project is due soon. – Paul Samsotha Mar 07 '14 at 11:45
  • that works great thanks, with JDialogs s is it possible to display content below the histogram for the mean, median, variance and modal length? or will that be best to be displayed in the applet itself? – user3392311 Mar 07 '14 at 11:49
  • It works just like a `JFrame`. Add whatever you need to the `BorderLayout.SOUTH`, like a `JPanel` with your details. `dialog.add(detailPanel, BorderLayout.SOUTH);`. The `HistogramPanel` is already added to the `BorderLayout.CENTER` by default, since you didn't specify a position. – Paul Samsotha Mar 07 '14 at 11:51
  • hi, i need to put it in the actual applet now on a label that i have created called meangraphlength, will i need to add my calculating mean code into the jfilechooser? i have tried to implement it, the code is above.. – user3392311 Mar 07 '14 at 13:31
  • What you need to understand about this site is that it isn't a help desk. It's a Q&A site, not just for the question asker, but for all who come across different answers/question. How it works is that you ask a question, you get it answered, you accept an answer. If you have another question, the you post another question. You don't keep asking the same person for help on different topics. – Paul Samsotha Mar 07 '14 at 13:34
  • And don't change posts after they have been answered an accepted. It may invalidate answers. – Paul Samsotha Mar 07 '14 at 13:34
  • the reason why i changed the post for now is because my assignment is due in today and i have had several people copying my assignment which can lead to all of us getting zero for plagarism, i am going to put the code back on by the end of today when the assignment deadline is over.. – user3392311 Mar 07 '14 at 13:37
  • and sorry for asking so many questions, thank you very much for your help – user3392311 Mar 07 '14 at 13:38
  • i will post new questions, i didnt know i couldnt keep asking on here, thanks – user3392311 Mar 07 '14 at 13:44