Issue
I wanted to have CMD character-based one-line-updating progressbar that would work not only in CMD once project is compiled but also in NetBeans OutputWindow which basically everybody here was saying simply does not work (well at least those many post I read here before I made my own progressbar below).
The real problem why it does not work normally in NB output window is with System.out.print()
together with using \r
and by pure coincidence I noticed that when Thread.sleep()
is set lower than some 250/300 it stops responding when testing in NB OutputWindow (it outputs as whole only once the code is stopped) but if I increase the value let's say to those 300 it starts work nicely. And as I need quite simple endlessly running DOS one-line progressbar just to let user know something is going on and that the app is not frozen while it is doing its stuff, it suits my needs pretty well.
So I made it like this:
package jthndemo;
import java.io.PrintStream;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.SwingUtilities;
public class JthnDEMO {
public static void main(final String[] args) throws InterruptedException {
SwingUtilities.invokeLater(() -> {
PrintStream out = System.out;
int pause = 300;
int progressbarLength = 15;
boolean cond = true;
String txt = "Extracting...please, wait ";
String character = "█";
String str = null;
while (cond) {
int i = 0;
str = txt;
out.print("\r" + str + character);
try {
Thread.sleep(pause);
} catch (InterruptedException ex) {
Logger.getLogger(JthnDEMO.class.getName()).log(Level.SEVERE, null, ex);
}
while (i < progressbarLength) {
str = txt;
for (int j = 0; j < i + 1; j++) {
str += character;
}
out.print("\r" + str);
try {
Thread.sleep(pause);
} catch (InterruptedException ex) {
Logger.getLogger(JthnDEMO.class.getName()).log(Level.SEVERE, null, ex);
}
i++;
}
}
});
}
}
But to my big surprise when I compiled it and run it from command line via .bat file the output in CMD window makes just one full loop and then just kind-of flicking the whole line without updating and I do not know why.
Can anyone tell me why my progressbar code runs in NetBeans 8 output window but not in Win7 x64 system CMD window once compiled?
P.S.: the value of variable cond would be changed later (I will rewrite that single line of code so that the variable is defined somewhere else outside) in my code once I want my endless progressbar to end, so no worries about that (just saying I know).
Solution
So, I found solution myself (solution was to add last output in a loop as completely blank line in the length of the last "normal" output string) - this updated code now works both in NetBeans 8 output window (so one can test it right from the IDE without need of perpetual compiling with every change to code) and also as expected in CMD window too, I hope this may help anyone who might run into same "problems" as I did:
package jthndemo;
import java.io.PrintStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.SwingUtilities;
public class JthnDEMO {
public static void main(final String[] args) throws InterruptedException {
SwingUtilities.invokeLater(() -> {
PrintStream out = System.out;
int pause = 300;
int progressbarLength = 15;
boolean cond = true;
String txt = "Extracting...please, wait ";
String character = "█";
String str = null;
while (cond) {
int i = 0;
str = txt;
out.print("\r" + str + character);
try {
Thread.sleep(pause);
} catch (InterruptedException ex) {
Logger.getLogger(JthnDEMO.class.getName()).log(Level.SEVERE, null, ex);
}
while (i < progressbarLength) {
str = txt;
for (int j = 0; j < i + 1; j++) {
str += character;
}
out.print("\r" + str);
try {
Thread.sleep(pause);
} catch (InterruptedException ex) {
Logger.getLogger(JthnDEMO.class.getName()).log(Level.SEVERE, null, ex);
}
i++;
}
String blank = "";
for (int k = 0; k < str.length(); k++) {
blank += " ";
}
out.print("\r" + blank);
try {
Thread.sleep(10);
} catch (InterruptedException ex) {
Logger.getLogger(JthnDEMO.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
}
Answered By - qraqatit