Java Tutorial/Swing/ProgressMonitor — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 15:31, 31 мая 2010

14. Create a ProgressMonitor toolbar

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.ProgressMonitor;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIManager;
public class Main extends JFrame implements ActionListener {
  static ProgressMonitor pbar = new ProgressMonitor(null, "Monitoring Progress", "Init. . .", 0,
      100);
  static int counter = 0;
  public Main() {
    setSize(250, 100);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Timer timer = new Timer(500, this);
    timer.start();
    setVisible(true);
  }
  public static void main(String args[]) {
    UIManager.put("ProgressMonitor.progressText", "This is progress?");
    UIManager.put("OptionPane.cancelButtonText", "Go Away");
    new Main();
  }
  public void actionPerformed(ActionEvent e) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        if (pbar.isCanceled()) {
          pbar.close();
          System.exit(1);
        }
        pbar.setProgress(counter);
        pbar.setNote("Operation is " + counter + "% complete");
        counter += 2;
      }
    });
  }
}





14. Creating a Progress Monitor Dialog

import javax.swing.JFrame;
import javax.swing.ProgressMonitor;
import javax.swing.UIManager;
public class Main {
  public static void main(String[] argv) throws Exception {
    String message = "Description of Task";
    String note = "subtask";
    String title = "Task Title";
    UIManager.put("ProgressMonitor.progressText", title);
    int min = 0;
    int max = 100;
    JFrame component = new JFrame();
    ProgressMonitor pm = new ProgressMonitor(component, message, note, min, max);
    boolean cancelled = pm.isCanceled();
    if (cancelled) {
      System.out.println("Stop task");
    } else {
      pm.setProgress(100);
      pm.setNote("New Note");
    }
  }
}





14. Customizing a ProgressMonitor Look and Feel

ValueTypeProgressMonitor.progressTextString


14. Implement ProgressMonitor toolbar with a file to monitor

import java.io.FileInputStream;
import javax.swing.ProgressMonitorInputStream;
public class Main {
  public static void main(String args[]) throws Exception {
    ProgressMonitorInputStream monitor;
    monitor = new ProgressMonitorInputStream(null, "Loading ", new FileInputStream("fileName.txt"));
    while (monitor.available() > 0) {
      byte[] data = new byte[38];
      monitor.read(data);
      System.out.write(data);
    }
  }
}





14. JAR Archives: Pack Progress Monitor

import java.awt.ruponent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.jar.Pack200;
import javax.swing.ProgressMonitor;
public class PackProgressMonitor extends ProgressMonitor implements PropertyChangeListener {
  public PackProgressMonitor(Component parent) {
    super(parent, null, "Packing", -1, 100);
  }
  public void propertyChange(PropertyChangeEvent event) {
    if (event.getPropertyName().equals(Pack200.Packer.PROGRESS)) {
      String newValue = (String) event.getNewValue();
      int value = Integer.parseInt(newValue);
      this.setProgress(value);
    }
  }
}





14. ProgressMonitor Class

The ProgressMonitor class is used to report on the status of a time-consuming task.

Creating a ProgressMonitor



public ProgressMonitor(Component parentComponent, Object message, String note, int minimum, int maximum)





14. Setting the Popup Delay of a Progress Monitor Dialog

import javax.swing.JFrame;
import javax.swing.ProgressMonitor;
import javax.swing.UIManager;
public class Main {
  public static void main(String[] argv) throws Exception {
    String message = "Description of Task";
    String note = "subtask";
    String title = "Task Title";
    UIManager.put("ProgressMonitor.progressText", title);
    int min = 0;
    int max = 100;
    JFrame component = new JFrame();
    ProgressMonitor pm = new ProgressMonitor(component, message, note, min, max);
    int millisToPopup = pm.getMillisToPopup(); // 2000
    int millisToDecideToPopup = pm.getMillisToDecideToPopup(); // 500
    pm.setMillisToPopup(0);
    pm.setMillisToDecideToPopup(0);
  }
}