Pregunta

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();
            }
        }
    }
}
¿Fue útil?

Solución

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 (...)
}

Otros consejos

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top