开发者

How to show loading

I want to know how to show the loading of an application. Normally (let's say Netbeans IDE,) the application will show how far it ha开发者_运维知识库s loaded and how far to load. Also, loading classes as well can be seen in welcome page. How is this is done and how we can show our classes loading and loaded status (in a progress bar) in our applications? Also let's say that we have used Hibernate, and there is a login on first page, it takes time to start but only for the starting (I think it is because that the Hibernate factory is getting started and load its classes). The answer is hoped in Java.


I want to know how to show the loading of an application.

See the java.awt.SplashScreen class. But..

How to show loading

..show how far it has loaded and how far to load.

..with a customized image. Call SplashScreen.createGraphics() to get a Graphics object (from the image defined as a splash in the manifest) which can be drawn on. Draw the progress bar at an appropriate location on the Graphics object. For the progress bar, either use a JProgressBar as already mentioned, or to keep it lean (using pure AWT), draw one big Rectangle to represent the bar, and fill a smaller Rectangle to represent the progress.

See also How to Create a Splash Screen in the Java Tutorial. (Where you can see that image above.)


That sounds for using JProgressBar, combined with Splash Screen or JDialog.

Be sure that JProgressBar must be updated on the Event Dispatch Thread, more about that in Concurrency in Swing,

Then you have two choices as to how to update a JProgressBar correctly - by wrapping code into:

  • SwingWorker
  • Runnable/Thread


There is some support built into swing for this. See the tutorial on How to Use Progress Bars to get started.


Here is a very basic code:

JProgressBar progressBar;
progressBar = new JProgressBar(0, task.getLengthOfTask());
progressBar.setValue(33); // put here the percentage you want to display...
progressBar.setStringPainted(true);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜