显示自定义的加载进度指示器

Java Web Start 应用程序可以显示自定义的加载进度指示器,该指示器显示应用程序资源下载的进度。

考虑 Weather 应用程序和CustomProgress类,以了解如何为 Java Web Start 应用程序实现自定义的加载进度指示器。为了演示 Long 时间的大量下载,此 Java Web Start 应用程序的 JAR 文件已被人为夸大,而customprogress_webstart.jnlp文件将其他 JAR 文件指定为资源。

开发自定义的加载进度指示器

要为 Java Web Start 应用程序开发自定义的加载进度指示器,请创建一个实现DownloadServiceListenerinterface的类。

加载进度指示器类的构造函数不应具有任何参数。

import javax.jnlp.DownloadServiceListener;
import java.awt.Container;
import java.applet.AppletStub;
import netscape.javascript.*;
// ...
public class CustomProgress
        implements DownloadServiceListener {   
    JFrame frame = null;
    JProgressBar progressBar = null;
    boolean uiCreated = false;

    public CustomProgress() {
    }
...
}

以下代码段显示了如何为加载进度指示器构建 UI:

private void create() {
    JPanel top = createComponents();
    frame = new JFrame(); // top level custom progress
                          // indicator UI
    frame.getContentPane().add(top,
                               BorderLayout.CENTER);
    frame.setBounds(300,300,400,300);
    frame.pack();
    updateProgressUI(0);
}

private JPanel createComponents() {
    JPanel top = new JPanel();
    top.setBackground(Color.WHITE);
    top.setLayout(new BorderLayout(20, 20));
 
    String lblText =
        "<html><font color=green size=+2" +
        ">JDK Documentation</font>
" + "The one-stop shop for Java enlightenment!
</html>"; JLabel lbl = new JLabel(lblText); top.add(lbl, BorderLayout.NORTH); ... progressBar = new JProgressBar(0, 100); progressBar.setValue(0); progressBar.setStringPainted(true); top.add(progressBar, BorderLayout.SOUTH); return top; }

基于overallPercent参数,通过以下方法创建和更新加载进度指示器。 Java Web Start 软件会定期调用这些方法,以传达应用程序下载的进度。当资源的下载和验证完成 100%时,Java Web Start 软件将始终发送消息。

public void progress(URL url, String version, long readSoFar,
                     long total, int overallPercent) {        
    updateProgressUI(overallPercent);

}

public void upgradingArchive(java.net.URL url,
                  java.lang.String version,
                  int patchPercent,
                  int overallPercent) {
    updateProgressUI(overallPercent);
}

public void validating(java.net.URL url,
            java.lang.String version,
            long entry,
            long total,
            int overallPercent) {
    updateProgressUI(overallPercent);
}

private void updateProgressUI(int overallPercent) {
    if (overallPercent > 0 && overallPercent < 99) {
        if (!uiCreated) {
            uiCreated = true;
            // create custom progress indicator's
            // UI only if there is more work to do,
            // meaning overallPercent > 0 and
            // < 100 this prevents flashing when
            // RIA is loaded from cache
            create();
        }
        progressBar.setValue(overallPercent);
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                frame.setVisible(true);
            }
        });
    } else {
        // hide frame when overallPercent is
        // above 99
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                frame.setVisible(false);
                frame.dispose();
            }
        });
    }
}

编译加载进度指示器类,并使用显示加载进度指示器所需的所有资源构建一个 JAR 文件。在您的classpath中包括<your JRE directory>/lib/javaws.jar文件以启用编译。

现在可以使用加载进度指示器类。下一步是将此加载进度指示器 JAR 文件指定为 Java Web Start 应用程序的进度指示器。

为 Java Web Start 应用程序指定自定义的加载进度指示器

要为 Java Web Start 应用程序指定定制的加载进度指示器,请在应用程序的 JNLP 文件中包括以下信息:

  • 具有download="progress"属性的jar标签

  • progress-class属性,具有定制的加载进度类的完全限定名称。

customprogress_webstart.jnlp文件中的以下代码片段显示了download="progress"progress-class属性的用法。

<jnlp spec="1.0+" codebase=
  "https://docs.oracle.com/javase/tutorialJWS/samples/deployment" 
   href="customprogress_webstartJWSProject/customprogress_webstart.jnlp">
  <!-- ... -->
  <resources>
    <j2se version="1.7+"/>
    <jar href=
      "webstart_AppWithCustomProgressIndicator/webstart_AppWithCustomProgressIndicator.jar" />
    <jar href=
      "webstart_CustomProgressIndicator/webstart_CustomProgressIndicator.jar"
         download="progress" />
    <jar href=
      "webstart_AppWithCustomProgressIndicator/lib/IconDemo.jar" />
    <jar href=
      "webstart_AppWithCustomProgressIndicator/lib/SplitPaneDemo.jar" />
    <jar href=
      "webstart_AppWithCustomProgressIndicator/lib/SplitPaneDemo2.jar" />
    <jar href=
      "webstart_AppWithCustomProgressIndicator/lib/TextBatchPrintingDemo.jar" />
    <jar href=
      "webstart_AppWithCustomProgressIndicator/lib/ToolBarDemo.jar" />
    <jar href=
      "webstart_AppWithCustomProgressIndicator/lib/ToolBarDemo2.jar" />
    <jar href=
      "webstart_AppWithCustomProgressIndicator/lib/SwingSet2.jar" />
  </resources>
  <application-desc 
      main-class="customprogressindicatordemo.Main"
      progress-class="customprogressindicator.CustomProgress"
  />
  <!-- ... -->
</jnlp>

在网页中部署 Java Web Start 应用程序。在网络浏览器中打开JavaWebStartAppPage.html,以查看“天气”应用程序的自定义加载进度指示器。

Note:

要正确查看示例,您至少需要安装Java SE 开发套件(JDK)6 更新 18版本。

Note:

如果您以前查看过此 Java Web Start 应用程序,请在再次查看该应用程序之前使用 Java 控制面板清除缓存。您将看不到先前缓存的应用程序的进度指示器。

Note:

如果看不到示例正在运行,则可能需要在浏览器中启用 JavaScript 解释器,以便 Deployment Toolkit 脚本能够正常运行。

下载源代码用于“带有自定义加载进度指示器的 Java Web Start 应用程序”示例,以进一步进行实验。

有关自定义富 Internet 应用程序(RIA)加载体验的详细信息,请参见自定义加载体验主题。