0

I am writing code to read an Impulse Tracker module file into this object. It opens a JProgressBar while the file loads and displays which part of the file is being loaded.

The JFrame that displays the JProgressBar however is not showing any content (It gets called from another JPanel in a main JFrame) and if I put the JFrame's .setVisibility(true) method into an EventQueue.invokeLater() method, the frame doesn't show till the file is fully read and then it is too late for the bar to be useful.

An EventQueue.invokeAndWait() method will just throw a 'Cannot call invokeAndWait from the event dispatcher thread' exception.

How do I fix this that the JFrame sets up and shows the progress bar first, then the file is read and the progress bar is updated accordingly?

package Module.IT.Format;

import java.awt.BorderLayout;
import java.awt.Color;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

/**
 *
 * @author Edward Jenkins
 */
public class ITFile implements IModuleFile {

    // constants
    public static final int MOD_TYPE_ID = 4;

    // instance variables
    // it variables

    // constructor
    public ITFile(String file) {
        this.file = file;
    }
    
    // getters are here
    
    // method with issues
    @Override
    public boolean read() throws Exception {

        try {

            // progression bar
            JFrame progressFrame = new JFrame("Loading IT file...");
            JPanel loadPanel = new JPanel();
            JProgressBar fileLoader = new JProgressBar();
            fileLoader.setValue(0);
            fileLoader.setString("Loading Header...");
            fileLoader.setBounds(40, 40, 200, 20);
            fileLoader.setStringPainted(true);
            fileLoader.setVisible(true);

            // add progress bar to load panel
            loadPanel.add(fileLoader);

            // add load panel to frame
            progressFrame.setLocationRelativeTo(null);
            progressFrame.setResizable(false);
            progressFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
            progressFrame.setAlwaysOnTop(true);
            progressFrame.setSize(280, 80);
            progressFrame.add(loadPanel);

            java.awt.EventQueue.invokeLater(() -> {
                progressFrame.setVisible(true);
            });

            // read header
            boolean headerRead = readHeader();
            fileLoader.setValue(10);
            fileLoader.setString("Loading Instruments...");

            // read instruments
            boolean instrumentsRead = readInstruments();
            fileLoader.setValue(20);
            fileLoader.setString("Loading Samples...");

            // read samples
            boolean samplesRead = readSamples();
            fileLoader.setValue(90);
            fileLoader.setString("Loading Patterns...");

            // read patterms
            boolean patternsRead = readPatterns();
            fileLoader.setValue(100);
            fileLoader.setString("Done! ");
            progressFrame.dispose();

            return (headerRead && instrumentsRead && samplesRead && patternsRead);

        } catch (Exception e) {
            throw e;
        }
    }

    // write method

    // methods for reading each part of file using a DataInputStream.
}
  • You may like to refer to ProgressBar demo code at https://docs.oracle.com/javase/tutorial/uiswing/examples/components/ProgressBarDemoProject/src/components/ProgressBarDemo.java. – Hitesh A. Bosamiya Feb 09 '22 at 08:02
  • The code works fine if I call it on its own, but if I call it from the controller of another JFrame, the JFrame for the JProgressBar is invisible (without the invokeLater() method) – Edward Eddy67716 Feb 09 '22 at 08:07
  • Would it be different due to the fact I use an encapsulated file reader to deal with unsigned data types and endieness? – Edward Eddy67716 Feb 09 '22 at 08:17

0 Answers0