Class 2 | Intermediate Java 30-IT-397 |
Try it with Runnable
package Threads;
import javax.swing.DefaultListModel;
/**
*
* @author default
*/
public class RunnableRandomNumber extends javax.swing.JFrame {
DefaultListModel model;
/** Creates new form RandomNumber */
public RunnableRandomNumber() {
initComponents();
// Instantiate the model,
add it to the list.
model = new DefaultListModel();
lstNumbers.setModel(model);
}
// An inner class which
will run as a thread.
class AddRandoms implements
Runnable {
// The thread object we will use to do the dirty work.
Thread thread;
/**
* We don't have to make our own start method.
* We could do the same logic in the calling method.
* But this approach is better for transparency, in case we
* decide to switch back to extending Thread again.
*
*/
// This method is called exclusively from the
// JFrame object RunnableRandomNumber.
public void start() {
// Instantiate the Thread object,
// and pass in a reference of the current class,
// which implements runnable.
thread = new Thread(this);
// Let's light this candle.
thread.start();
}
// This method is called exclusively from the thread in the above method
run().
public void run() {
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 AddRandoms
thread.
AddRandoms addRandoms
= new AddRandoms();
addRandoms.start();
}
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 RunnableRandomNumber().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