Class 2 Intermediate Java 30-IT-397  

What Are Threads?

/*
 * RandomNumber.java
 *
 * Created on April 23, 2002, 11:53 PM
 */

package Threads;

import javax.swing.DefaultListModel;

/**
 *
 * @author  default
 */
public class RandomNumber extends javax.swing.JFrame {

    DefaultListModel model;
 
    /** Creates new form RandomNumber */
    public RandomNumber() {
        initComponents();
 
        // Instantiate the model, add it to the list.
        model = new DefaultListModel();
        lstNumbers.setModel(model);
 
    }

    public void addRandoms() {
        try {
            for (int j = 0; j < 500; j++) {
                // Generate a random number between 0 and 1, multiply it by 1000 to make it intersting.
                int intRandom = (int) (Math.random() * 1000);

                // add the random number to the model.
                model.addElement(j + ": " + intRandom);

                // Sleep for 20 ms.
                Thread.sleep(20);
            }

        } catch (InterruptedException e) {
            System.out.println("Interrupted");
        }
    }
 
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {
        jScrollPane1 = new javax.swing.JScrollPane();
        lstNumbers = new javax.swing.JList();
        jPanel1 = new javax.swing.JPanel();
        btnStart = new javax.swing.JButton();
        btnStop = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        jScrollPane1.setViewportView(lstNumbers);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        btnStart.setText("Start");
        btnStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStartActionPerformed(evt);
            }
        });

        jPanel1.add(btnStart);

        btnStop.setText("Stop");
        btnStop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStopActionPerformed(evt);
            }
        });

        jPanel1.add(btnStop);

        getContentPane().add(jPanel1, java.awt.BorderLayout.SOUTH);

        jLabel1.setText("Random Numbers");
        getContentPane().add(jLabel1, java.awt.BorderLayout.NORTH);

        pack();
    }

    private void btnStartActionPerformed(java.awt.event.ActionEvent evt) {
        // Start the random number generation.
        addRandoms();
 
    }

    private void btnStopActionPerformed(java.awt.event.ActionEvent evt) {
        // Just close the program.
        System.exit(0);
 
    }

    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
 
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        new RandomNumber().show();
    }
 

    // Variables declaration - do not modify
    private javax.swing.JButton btnStop;
    private javax.swing.JList lstNumbers;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton btnStart;
    // End of variables declaration

}
 

Created by:  Brandan Jones December 17, 2001