Close

Java Swing - Using ProgressMonitorInputStream to show an InputStream reading progress

[Last Updated: Jul 4, 2018]

ProgressMonitorInputStream creates an instance of ProgressMonitor (last example). The progress of reading an input stream is automatically shown when necessary.

Example

public class ProgressMonitorInputStreamExample {

    public static void main(String[] args) throws IOException {
        JFrame frame = createFrame("ProgressMonitorInputStream Example");
        JButton button = new JButton("read file");
        button.addActionListener(createReadFileListener(frame));
        button.setEnabled(true);

        frame.add(button, BorderLayout.NORTH);
        frame.setVisible(true);
    }

    private static ActionListener createReadFileListener(Component parent) throws IOException {
        File file = createTestFile();
        file.deleteOnExit();
        //for progress dialog title
        UIManager.put("ProgressMonitor.progressText", "Reading a test file");
        return (ae) -> {
            new Thread(() -> {
                ProgressMonitorInputStream pMonitorInputStream;
                try (BufferedInputStream bis = new BufferedInputStream(
                        pMonitorInputStream = new ProgressMonitorInputStream(
                                parent,
                                "Reading " + file.getName(),
                                new FileInputStream(file)))) {
                    ProgressMonitor progressMonitor = pMonitorInputStream.getProgressMonitor();
                    progressMonitor.setMillisToDecideToPopup(100);
                    progressMonitor.setMillisToPopup(100);

                    byte[] buffer = new byte[2048];
                    while ((bis.read(buffer)) != -1) {
                        System.out.println(new String(buffer));
                        progressMonitor.setNote(bis.available()/1000+ " more kb to read.");
                        //simulating some processing delay
                        Thread.sleep(2);
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }).start();
        };
    }

    private static File createTestFile() throws IOException {
        Path testPath = Files.createTempFile("test", ".txt");
        String textData = IntStream.range(1, 1000000)
                                   .mapToObj(Integer::toString)
                                   .collect(Collectors.joining());
        Files.write(testPath, textData.getBytes());
        return testPath.toFile();
    }

    public static JFrame createFrame(String title) {
        JFrame frame = new JFrame(title);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(new Dimension(800, 700));
        return frame;
    }
}

Output

On clicking 'read file' button:

Example Project

Dependencies and Technologies Used:

  • JDK 10
  • Maven 3.3.9

ProgressMonitorInputStream Example Select All Download
  • swing-progress-monitor-input-stream
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • ProgressMonitorInputStreamExample.java

    See Also