home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / J A V A / Java Development Kit V1.2 / jdk12-win32(1).exe / data1.cab / demos / demo / jfc / SwingSet / ProgressPanel.java < prev    next >
Encoding:
Java Source  |  1998-12-01  |  4.5 KB  |  150 lines

  1. /*
  2.  * @(#)ProgressPanel.java    1.9 98/08/26
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. import javax.swing.*;
  16. import javax.swing.text.*;
  17. import javax.accessibility.*;
  18.  
  19. import java.awt.*;
  20. import java.awt.event.*;
  21. import java.util.*;
  22.  
  23.  
  24. /**
  25.  * Demo the Progress Bar
  26.  *
  27.  * @version 1.9 08/26/98
  28.  * @author Jeff Dinkins
  29.  # @author Peter Korn (accessibility support)
  30.  */
  31. public class ProgressPanel extends JPanel implements ActionListener {
  32.     SwingSet swing;
  33.     JProgressBar progressBar;
  34.     JTextArea progressTextArea;
  35.     JButton loadButton;
  36.     JButton stopButton;
  37.     Timer timer;
  38.     Object  lock = new Object();
  39.  
  40.     public ProgressPanel(SwingSet swing) {
  41.     this.swing = swing;
  42.  
  43.     setLayout(new BorderLayout());
  44.  
  45.     JPanel textWrapper = new JPanel(new BorderLayout());
  46.     textWrapper.setBorder(swing.loweredBorder);
  47.     textWrapper.setAlignmentX(LEFT_ALIGNMENT);
  48.     progressTextArea = new MyTextArea();
  49.     progressTextArea.getAccessibleContext().setAccessibleName("Text progressively being loaded in");
  50.     progressTextArea.getAccessibleContext().setAccessibleDescription("This JTextArea is being filled with text from a buffer progressively a character at a time while the progress bar a the bottom of the window shows the loading progress");
  51.     textWrapper.add(progressTextArea, BorderLayout.CENTER);
  52.  
  53.     add(textWrapper, BorderLayout.CENTER);
  54.  
  55.     JPanel progressPanel = new JPanel();
  56.     add(progressPanel, BorderLayout.SOUTH);
  57.  
  58.     progressBar = new JProgressBar(JProgressBar.HORIZONTAL,
  59.                        0, text.length()) {
  60.         public Dimension getPreferredSize() {
  61.         return new Dimension(300, super.getPreferredSize().height);
  62.         }
  63.     };
  64.     progressBar.getAccessibleContext().setAccessibleName("Text loading progress");
  65.     progressPanel.add(progressBar);
  66.  
  67.     loadButton = new JButton("Start Loading Text");
  68.     loadButton.addActionListener(new ActionListener() {
  69.         public void actionPerformed(ActionEvent e) {
  70.         startLoading();
  71.         }
  72.     });
  73.     progressPanel.add(loadButton);
  74.  
  75.         stopButton = new JButton("Stop Loading Text");
  76.         stopButton.addActionListener(new ActionListener() {
  77.             public void actionPerformed(ActionEvent e) {
  78.                 stopLoading();
  79.             }
  80.         });
  81.         stopButton.setEnabled(false);
  82.     progressPanel.add(stopButton);
  83.     }
  84.  
  85.     public Insets getInsets() {
  86.     return new Insets(10,10,10,10);
  87.     }
  88.  
  89.     public void startLoading() {
  90.         if(timer == null) {
  91.             loadButton.setEnabled(false);
  92.             stopButton.setEnabled(true);
  93.         timer = new Timer(25, this);
  94.         timer.start();
  95.         }
  96.     }
  97.  
  98.     public void stopLoading() {
  99.     if(timer != null) {
  100.        timer.stop();
  101.        timer = null;
  102.     }
  103.         loadButton.setEnabled(true);
  104.         stopButton.setEnabled(false);
  105.     }
  106.  
  107.     int textLocation = 0;
  108.  
  109.     String text =
  110.       "      The saying goes: if an infinite number of monkeys\n" +
  111.          "   typed on an infinite number of typewriters, eventually\n" +
  112.          "   all the great works of mankind would emerge. \n\n " +
  113.       "      Now, with today's high speed computers, we can\n " +
  114.          "   finally test the theory...\n\n" +
  115.       "      Lzskd jfy 92y;ho4 th;qlh sd 6yty;q2 hnlj 8sdf. Djfy\n " +
  116.          "   92y;ho4, th;qxhz d7yty; Q0hnlj 23&^ (# ljask djf y92y;h\n " +
  117.          "   fy92y; Sd6y ty;q2h nl jk la gfa harvin garvil lasdfsd\n " +
  118.          "   a83sl la8z 2 be or not to be... that is the question. Hath\n" +
  119.      "   forth not to want a banana, or to be a banana. Banana, I \n" +
  120.      "   knew him banana. Banana banana banana.\n\n" +
  121.          "          Well... it seemed like a good idea...\n\n\n\n\n\n\n\n\n\n" +
  122.       "             Hi Ewan and Montana!";
  123.  
  124.     public void actionPerformed (ActionEvent e) {
  125.         if(progressBar.getValue() < progressBar.getMaximum()) {
  126.         progressBar.setValue(progressBar.getValue() + 1);
  127.         progressTextArea.append(text.substring(textLocation, textLocation+1));
  128.         textLocation++;
  129.         } else {
  130.             stopLoading();
  131.             }
  132.     }
  133.  
  134.     class MyTextArea extends JTextArea {
  135.         public MyTextArea() {
  136.             super(null, 0, 0);
  137.         setEditable(false);
  138.         setText("");
  139.         }
  140.  
  141.         public float getAlignmentX () {
  142.             return LEFT_ALIGNMENT;
  143.         }
  144.  
  145.         public float getAlignmentY () {
  146.             return TOP_ALIGNMENT;
  147.         }
  148.     }
  149. }
  150.