Topic: 请教大侠们一个问题--进度条

  Print this page

1.请教大侠们一个问题--进度条 Copy to clipboard
Posted by: jlynnc
Posted on: 2002-12-07 13:22

我谢了一个ftpclient
下载文件的时候需要弹出一个进度条窗口指示进度
我把数据流的读写放到了另一个线程里,可是还是不行
一按下载按钮就像死极了一样,进度条窗口显示不完全
直到下载完成,才完全显示,不知如何解决

2.解决办法 [Re: jlynnc] Copy to clipboard
Posted by: levels
Posted on: 2002-12-08 00:52

使用SwingUtilities.invokeLater()

3.try this out [Re: jlynnc] Copy to clipboard
Posted by: snowbug
Posted on: 2002-12-08 11:50

I've been using a small library called foxtrot for a while in my swing related programming. It made my life so much easier. Basically, it allows you to invoke a time-consuming task but not block the swing UI (so the button, progress bar, .. , will still be responsive), with an added benefit of being able to catch the exception from the time-consuming task the same way you do in regular java code. Plase read the short documentation from the site to get a full idea.

http://foxtrot.sourceforge.net

4.Re:请教大侠们一个问题--进度条 [Re: jlynnc] Copy to clipboard
Posted by: floater
Posted on: 2002-12-08 14:20

The reason is that you are using the same thread for swing event dispatching *and* ftp. So you have to fire off ftp from a different thread.
There are several ways:
1. SwingUtilities in swing
2. Sun also has a Thread worker class(do a search at sun.com)
3. A simple embedded as follows(My own woking code, but use it freely)

if ((e.getClickCount() == 2) && SwingUtilities.isLeftMouseButtonEnvelope)
{
debugLine("Left mouse double selected. Go get data!");
//////////////////////////////////////////////////
//This will start a new thread so it won't block the
//Event Dispatch Thread.

Thread worker = new Thread()
{
public void run()
{
//This sets the progress bar
StockChart.getFrame().setProgressBar(true);
//do whatever you want

//This reset the progress bar
StockChart.getFrame().setProgressBar(false);
}
};
worker.start();
}

5.Re:请教大侠们一个问题--进度条 [Re: floater] Copy to clipboard
Posted by: snowbug
Posted on: 2002-12-09 02:00

floater wrote:
The reason is that you are using the same thread for swing event dispatching *and* ftp. So you have to fire off ftp from a different thread.
There are several ways:
1. SwingUtilities in swing
2. Sun also has a Thread worker class(do a search at sun.com)
3. A simple embedded as follows(My own woking code, but use it freely)

if ((e.getClickCount() == 2) && SwingUtilities.isLeftMouseButtonEnvelope)
{
debugLine("Left mouse double selected. Go get data!");
//////////////////////////////////////////////////
//This will start a new thread so it won't block the
//Event Dispatch Thread.

Thread worker = new Thread()
{
public void run()
{
//This sets the progress bar
StockChart.getFrame().setProgressBar(true);
//do whatever you want

//This reset the progress bar
StockChart.getFrame().setProgressBar(false);
}
};
worker.start();
}


This is a traditional way to solve the problem but it has some problems:
1. The exception throwed from the worker thread can not be easily catched and dealt with.
2. when worker.start() is called, the current thread keeps its execution, and you don't know when the worker thread will start. In other words, it is not blocked, which brings in another typical problem in thread programming: synchronization.

By using the foxtrot framework, you program the time-consuming (or cpu-consuming) code as if you are in a single thread environment, and the GUI is not freezing. I am thinking of writing a small tutorial of how to use it, but before that, here is an example code:


...
public void actionPerformed(ActionEvent ae){
//start fetching ftp data
try{
Worker.post(new Task(){
public Object run() throws Exception{
// Here write the time-consuming ftp code to get the data
}
});
}catch (Exception x){
// Handle the exception thrown by the Task
}
}


it is really that easy, and you can see the code is much shorter and cleaner (In the case of using the ProgressBar, you can simply update its progress value from the ftp downloading code and the GUI will not freezing). The documentation it provides is short but sufficient (although it is in English).

In my opinion, this small library should be integrated into the core JDK. It is JDK 1.3 and 1.4 tested. I strongly suggest everyone to take a look Wink

6.Re:请教大侠们一个问题--进度条 [Re: jlynnc] Copy to clipboard
Posted by: floater
Posted on: 2002-12-09 06:05

I think Timer could also be used for this purpose.

But like the above user mentioned, each way has its own goodies and baddies. Be aware!

Foxtrot is terrific, a very nice tool!

7.Re:try this out [Re: snowbug] Copy to clipboard
Posted by: Biubiu
Posted on: 2002-12-09 11:15

snowbug wrote:
I've been using a small library called foxtrot for a while in my swing related programming. It made my life so much easier. Basically, it allows you to invoke a time-consuming task but not block the swing UI (so the button, progress bar, .. , will still be responsive), with an added benefit of being able to catch the exception from the time-consuming task the same way you do in regular java code. Plase read the short documentation from the site to get a full idea.

http://foxtrot.sourceforge.net


It's great. Thank you for your information.

8.Re:请教大侠们一个问题--进度条 [Re: floater] Copy to clipboard
Posted by: snowbug
Posted on: 2002-12-09 14:00

floater wrote:
I think Timer could also be used for this purpose.


I dont think the Timer is appropriate for this purpose because the Timer is for scheduling tasks that will happen in a set time in the future either repeatedly or not, but in this case, the progressbar is driven by the amount of the data been transferred, not the time that passes.

Just my opinion Wink

9.Re:请教大侠们一个问题--进度条 [Re: jlynnc] Copy to clipboard
Posted by: floater
Posted on: 2002-12-09 22:28

No, it's not about the time it's using. Actually, the progressbar could be in a non determined state, like getting a web page(we really don't know how long it takes).

I run across something else yesterday and bumped into this Timer, so just posted a short one as a way to fire off a task. Sorry for the confusion, my intention was to get an extra way to do this.

Again, there is always a tradeoff no matter which way to go. For instance, if we don't care the synchronization, or we just want to sync a block of code, then we could use "loose" ways. However, there is a bottom line somewhere, namely, we have to know exactly what we are doing. I agree wholeheartedly with you on the safe side.

BTW, I generated a pdf file from foxtrot yesterday and will post here later for sharing. That's the tool so far I've seen as the safest tool.

One more question, foxtrot claims they came up this from swing dialog model, have you dived into their source to see how they did? I am just curious(because I wonder what price we pay for this feature).

10.About foxtrot [Re: jlynnc] Copy to clipboard
Posted by: snowbug
Posted on: 2002-12-09 22:57

I came to know this library when I attended the JavaOne 2001, where the author hosted a BOF session to promote it.

I didn't dive into the source code. But here is what I heard during the BOF:
Remember when you are using Swing Dialogs, your code is blocked for user selection, however, the UI is not freezing. That's because in the background Swing creates a new event dispatcher thread for the dialog. Foxtrot uses that idea and expanded it for generic uses.

The price you pay is minimal. In early versions, it requires you to include the foxtrot library in the VM bootstrap path. Now it has been rewritten to use the Proxy class to accomplish it (don't have too much experience with Proxy). It will not slow you code down, unless you create new tasks in a N loop Smile - expecially compare with all the ease and benefits you get.

Take a look at the forxtrot forum will help to provide more background info. It is a great library, or more precisely, a framework for swing GUI programming. Used it for many projects and never had a problem.

It will be nice if you can translate the foxtrot documents into chinese before post it here since the original documentation can be freely accessed from the web, and I know I am not good at translations.

11.Re:请教大侠们一个问题--进度条 [Re: jlynnc] Copy to clipboard
Posted by: floater
Posted on: 2002-12-10 01:09

Hehe, I can't translate either. I learned everything in computer in english, so it's hard to understand chinese computer terminologies without guessing.

But I like the docs in there, they explain the different threads very well, especially for beginners.

However, they assume the knowledge of synchronization and SwingWorker class, not good for beginners. Adding these, it would be a very good introduction on threads.


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923