paulstgeorge
paulstgeorge

Reputation: 11

Java Swing ProgressBar does not update during long process

I have written a program that transmits blocks of data to a device over a TCP socket. As each block is written, the device returns an acknowledgement packet. The program writes the next block after each acknowledgement is received. This write/read sequence is not time/timer based. The program may proceed when the device returns the acknowledgement.

I want to update a JProgressBar. The program works fine, but the JProgressBar does not update as things proceed. The communication process is not timer-based so a timer-based thread to handle the JProgressBar will not work for me, at least that's what I think right now. Every solution that comes near to my need appears to be timer-based. I don't think these will work and my feable attempts to make them work have been entirely unsuccessful.

With this segment, I think I'm close: I'm trying to make the 'EventQueue' thread work to support the JProgressBar, but so far it's not working. I am not an expert on multi-threaded applications, so please be kind and please be specific. I'm really stumped.

int nFWIdx = 0;
final int NOFULLBLOCKS = (int)(arrFIRMWARE.length / BLOCK_INCREMENT);
jprgbarUpload.setMinimum(0);
jprgbarUpload.setMaximum(NOFULLBLOCKS + 1);

for (int blockno = 1; blockno <= NOFULLBLOCKS; blockno++) {
    StringBuilder sb = new StringBuilder();
    for (int loc = nFWIdx; loc < (nFWIdx + BLOCK_INCREMENT); loc++)
        sb.append(arrFIRMWARE[loc])
                .append(UtilityClass.TERMINATOR);
    nFWIdx += BLOCK_INCREMENT;
    byte[] bCOMMAND = sb.toString().getBytes(StandardCharsets.US_ASCII);

    outputstream.write(bCOMMAND);
    outputstream.flush();

    AtomicInteger aiBLOCKNO = new AtomicInteger(blockno);
    java.awt.EventQueue.invokeLater(() -> {
        jprgbarUpload.setValue(aiBLOCKNO.get());
    });

    boolean blnISACKRCVD = false;
    byte[] bINMESSAGE = new byte[TCP_SINGLE_FRAME_SIZE];
    while (!blnISACKRCVD) {
        int iSTREAMREAD = bufferedinputstream.read(bINMESSAGE);
        String sSTREAMREAD  = (new String (bINMESSAGE)).trim();
        if (sSTREAMREAD.length() == FIRMWARE_ACK_MESSAGE.length())
        {
            blnISACKRCVD = true;
        }
    }
}

Upvotes: 0

Views: 36

Answers (0)

Related Questions