Java by API/javax.swing/ProgressMonitor

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

new ProgressMonitor(Component p, Object m, String n, int min, int max)

   <source lang="java">

import java.awt.ruponent; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.ProgressMonitor; import javax.swing.Timer; public class MainClass {

 static ProgressMonitor monitor;
 static int progress;
 static Timer timer;
 static class ProgressMonitorHandler implements ActionListener {
   public void actionPerformed(ActionEvent actionEvent) {
     if (monitor == null)
       return;
     if (monitor.isCanceled()) {
       System.out.println("Monitor canceled");
       timer.stop();
     } else {
       progress += 3;
       monitor.setProgress(progress);
       monitor.setNote("Loaded " + progress + " files");
     }
   }
 }
 public static void main(String args[]) {
   JFrame frame = new JFrame("ProgressMonitor Sample");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   JButton autoIncreaseButton = new JButton("Automatic Increase");
   ActionListener autoIncreaseActionListener = new ActionListener() {
     public void actionPerformed(ActionEvent actionEvent) {
       Component parent = (Component) actionEvent.getSource();
       monitor = new ProgressMonitor(parent, "Loading Progress", "Getting Started...", 0, 200);
       progress = 0;
       
       if (monitor != null) {
         if (timer == null) {
           timer = new Timer(250, new ProgressMonitorHandler());
         }
         timer.start();
       }
     }
   };
   autoIncreaseButton.addActionListener(autoIncreaseActionListener);
   frame.add(autoIncreaseButton);
   frame.setSize(300, 200);
   frame.setVisible(true);
 }

}


 </source>
   
  
 
  



ProgressMonitor: getMillisToDecideToPopup()

   <source lang="java">
 

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);
 }

}


 </source>
   
  
 
  



ProgressMonitor: getMillisToPopup()

   <source lang="java">
 

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);
 }

}


 </source>
   
  
 
  



ProgressMonitor: isCanceled()

   <source lang="java">

import java.awt.ruponent; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.ProgressMonitor; import javax.swing.Timer; public class MainClass {

 static ProgressMonitor monitor;
 static int progress;
 static Timer timer;
 static class ProgressMonitorHandler implements ActionListener {
   public void actionPerformed(ActionEvent actionEvent) {
     if (monitor == null)
       return;
     if (monitor.isCanceled()) {
       System.out.println("Monitor canceled");
       timer.stop();
     } else {
       progress += 3;
       monitor.setProgress(progress);
       monitor.setNote("Loaded " + progress + " files");
     }
   }
 }
 public static void main(String args[]) {
   JFrame frame = new JFrame("ProgressMonitor Sample");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   JButton autoIncreaseButton = new JButton("Automatic Increase");
   ActionListener autoIncreaseActionListener = new ActionListener() {
     public void actionPerformed(ActionEvent actionEvent) {
       Component parent = (Component) actionEvent.getSource();
       monitor = new ProgressMonitor(parent, "Loading Progress", "Getting Started...", 0, 200);
       progress = 0;
       
       if (monitor != null) {
         if (timer == null) {
           timer = new Timer(250, new ProgressMonitorHandler());
         }
         timer.start();
       }
     }
   };
   autoIncreaseButton.addActionListener(autoIncreaseActionListener);
   frame.add(autoIncreaseButton);
   frame.setSize(300, 200);
   frame.setVisible(true);
 }

}


 </source>
   
  
 
  



ProgressMonitor.progressText

   <source lang="java">

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 MainClass extends JFrame implements ActionListener {

 static ProgressMonitor pbar = new ProgressMonitor(null, "Monitoring Progress", "Initializing . . .", 0, 100);
 static int counter = 0;
 public MainClass() {
   super("Progress Monitor Demo");
   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?");
   new MainClass();
 }
 public void actionPerformed(ActionEvent e) {
   SwingUtilities.invokeLater(new Update());
 }
 class Update implements Runnable {
   public void run() {
     if (pbar.isCanceled()) {
       pbar.close();
       System.exit(1);
     }
     pbar.setProgress(counter);
     pbar.setNote("Operation is " + counter + "% complete");
     counter += 2;
   }
 }

}


 </source>
   
  
 
  



ProgressMonitor: setMillisToDecideToPopup(int millisToDecideToPopup)

   <source lang="java">
 

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);
 }

}


 </source>
   
  
 
  



ProgressMonitor: setMillisToPopup(int millisToPopup)

   <source lang="java">
 

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);
 }

}


 </source>
   
  
 
  



ProgressMonitor: setNote(String note)

   <source lang="java">

import java.awt.ruponent; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.ProgressMonitor; import javax.swing.Timer; public class MainClass {

 static ProgressMonitor monitor;
 static int progress;
 static Timer timer;
 static class ProgressMonitorHandler implements ActionListener {
   public void actionPerformed(ActionEvent actionEvent) {
     if (monitor == null)
       return;
     if (monitor.isCanceled()) {
       System.out.println("Monitor canceled");
       timer.stop();
     } else {
       progress += 3;
       monitor.setProgress(progress);
       monitor.setNote("Loaded " + progress + " files");
     }
   }
 }
 public static void main(String args[]) {
   JFrame frame = new JFrame("ProgressMonitor Sample");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   JButton autoIncreaseButton = new JButton("Automatic Increase");
   ActionListener autoIncreaseActionListener = new ActionListener() {
     public void actionPerformed(ActionEvent actionEvent) {
       Component parent = (Component) actionEvent.getSource();
       monitor = new ProgressMonitor(parent, "Loading Progress", "Getting Started...", 0, 200);
       progress = 0;
       
       if (monitor != null) {
         if (timer == null) {
           timer = new Timer(250, new ProgressMonitorHandler());
         }
         timer.start();
       }
     }
   };
   autoIncreaseButton.addActionListener(autoIncreaseActionListener);
   frame.add(autoIncreaseButton);
   frame.setSize(300, 200);
   frame.setVisible(true);
 }

}



 </source>
   
  
 
  



ProgressMonitor: setProgress(int nv)

   <source lang="java">

import java.awt.ruponent; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.ProgressMonitor; import javax.swing.Timer; public class MainClass {

 static ProgressMonitor monitor;
 static int progress;
 static Timer timer;
 static class ProgressMonitorHandler implements ActionListener {
   public void actionPerformed(ActionEvent actionEvent) {
     if (monitor == null)
       return;
     if (monitor.isCanceled()) {
       System.out.println("Monitor canceled");
       timer.stop();
     } else {
       progress += 3;
       monitor.setProgress(progress);
       monitor.setNote("Loaded " + progress + " files");
     }
   }
 }
 public static void main(String args[]) {
   JFrame frame = new JFrame("ProgressMonitor Sample");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   JButton autoIncreaseButton = new JButton("Automatic Increase");
   ActionListener autoIncreaseActionListener = new ActionListener() {
     public void actionPerformed(ActionEvent actionEvent) {
       Component parent = (Component) actionEvent.getSource();
       monitor = new ProgressMonitor(parent, "Loading Progress", "Getting Started...", 0, 200);
       progress = 0;
       
       if (monitor != null) {
         if (timer == null) {
           timer = new Timer(250, new ProgressMonitorHandler());
         }
         timer.start();
       }
     }
   };
   autoIncreaseButton.addActionListener(autoIncreaseActionListener);
   frame.add(autoIncreaseButton);
   frame.setSize(300, 200);
   frame.setVisible(true);
 }

}



 </source>