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?
Rodney_McKaya at 2007-7-8 1:32:45 > top of Java-index,Desktop,Core GUI APIs...
# 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) {

}

Glynndera at 2007-7-8 1:32:45 > top of Java-index,Desktop,Core GUI APIs...
# 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.

chweyera at 2007-7-8 1:32:45 > top of Java-index,Desktop,Core GUI APIs...
# 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();

Rodney_McKaya at 2007-7-8 1:32:45 > top of Java-index,Desktop,Core GUI APIs...
# 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
Glynndera at 2007-7-8 1:32:45 > top of Java-index,Desktop,Core GUI APIs...
# 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
Glynndera at 2007-7-8 1:32:45 > top of Java-index,Desktop,Core GUI APIs...
# 7
I think 5 seconds is not enogh time for a progress bar, just set the cursor to busy.
Rodney_McKaya at 2007-7-8 1:32:45 > top of Java-index,Desktop,Core GUI APIs...
# 8
oh ok, how would i do that then, just while the file's openeing?
Glynndera at 2007-7-8 1:32:45 > top of Java-index,Desktop,Core GUI APIs...
# 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());
Rodney_McKaya at 2007-7-8 1:32:45 > top of Java-index,Desktop,Core GUI APIs...
# 10
Thanks, you get 7 dukes the three go to the other guy :p
Glynndera at 2007-7-8 1:32:45 > top of Java-index,Desktop,Core GUI APIs...
# 11
Did you try using my code to load the file?It should work much faster.
Rodney_McKaya at 2007-7-8 1:32:45 > top of Java-index,Desktop,Core GUI APIs...
# 12
No not yet, I used the other guys- ill try your then
Glynndera at 2007-7-8 1:32:45 > top of Java-index,Desktop,Core GUI APIs...