Another (very quick) question
Just wondering, and I'd like a quick answer for this :p, how would i make a progress bar for the opening of a file.
Basically, when the file is being opened (if its large) the window appears to an untrianed eye to freeze up, and I'd just like a simple progress bar to show them it hasnt.
I looked at this: http://java.sun.com/docs/books/tutorial/uiswing/components/progress.html#bars
[404 byte] By [
Glynndera] at [2007-11-26 13:54:16]

# 1
How are you openning the file? What method are you using?
# 2
Well, I'm using a method I hate, I am going to try another, this one checks how many lines there are in the file and then adds each on into the text box, like so:
try {
BufferedReader in = new BufferedReader(new FileReader(file));
String str;
while ((str = in.readLine()) != null) {
System.out.println(str);
texttoadd = texttoadd + "\n" + str;
luacode.setText(texttoadd);
}
in.close();
} catch (IOException str) {
}
# 3
> while ((str = in.readLine()) != null) {
>System.out.println(str);
>texttoadd = texttoadd + "\n" + str; //inefficient cause of many temporary String objects
>luacode.setText(texttoadd);
>}
With large files, this is very inefficient. If you use a StringBuilder (or for Thread
safety, a StringBuffer), it might be
so fast that a Progressbar isn't necessary: (a quick snipset)
StringBuilder builder = new StringBuilder();
while(in.ready())
{
builder.append((char) in.read())
}
luacode.setText(builder.toString());
If you need to count the lines, have a look at the class LineNumberReader.
# 4
Is this a text file?
Why would you want to add a progress bar, how big is this file suppose to be?
If is takes less than a few seconds you should just run it in a separate thread.
Anyway if you're reading a text file you can read it all at once:
FileInputStream in = new FileInputStream(fileName);
byte[] buffer = new byte[(int) new File(fileName).length()];
int length = 0;
String str = "";
while ((length = in.read(buffer)) != -1)
str += new String(buffer, 0, length);
in.close();
# 5
it can be any type of file,, however limiting it to text files would be fine...The only times its taken ages is when i tried to open something such as a .exe
# 6
I used your method chweyer and it works great thanks.I opened a text file, 630000 chars, and it took only five seconds, thanks!I dont really need a progress bar anymore, and do you think it'd be pointless to add on to look good?Message was edited by: Glynnder
# 7
I think 5 seconds is not enogh time for a progress bar, just set the cursor to busy.
# 8
oh ok, how would i do that then, just while the file's openeing?
# 9
Before openning the file set the cursor on your panel to busy:setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));After you finish loading the file do:setCursor(Cursor.getDefaultCursor());
# 10
Thanks, you get 7 dukes the three go to the other guy :p
# 11
Did you try using my code to load the file?It should work much faster.
# 12
No not yet, I used the other guys- ill try your then