Java Reference
In-Depth Information
65
// construct a new PrimeCalculator object
calculator = new PrimeCalculator(number,
displayPrimesJTextArea, statusJLabel, getPrimesJButton,
cancelJButton);
66
67
68
69
70
// listen for progress bar property changes
calculator.addPropertyChangeListener(
new PropertyChangeListener()
{
public void propertyChange(PropertyChangeEvent e)
{
// if the changed property is progress,
// update the progress bar
if (e.getPropertyName().equals( "progress" ))
{
int newValue = (Integer) e.getNewValue();
progressJProgressBar.setValue(newValue);
}
}
} // end anonymous inner class
); // end call to addPropertyChangeListener
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// disable Get Primes button and enable Cancel button
88
getPrimesJButton.setEnabled( false );
89
cancelJButton.setEnabled( true );
90
91
calculator.execute(); // execute the PrimeCalculator object
92
}
93
} // end anonymous inner class
94
); // end call to addActionListener
95
northJPanel.add(getPrimesJButton);
96
97 // add a scrollable JList to display results of calculation
98 displayPrimesJTextArea.setEditable( false );
99 add( new JScrollPane(displayPrimesJTextArea,
100 ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS ,
101 ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER ));
102
103 // initialize a panel to display cancelJButton,
104 // progressJProgressBar, and statusJLabel
105 JPanel southJPanel = new JPanel( new GridLayout( 1 , 3 , 10 , 10 ));
106 cancelJButton.setEnabled( false );
107 cancelJButton.addActionListener(
108 new ActionListener()
109 {
110 public void actionPerformed(ActionEvent e)
111 {
112
113 }
114 } // end anonymous inner class
115 ); // end call to addActionListener
calculator.cancel( true ); // cancel the calculation
Fig. 23.27 | Using a SwingWorker to display prime numbers and update a JProgressBar
while the prime numbers are being calculated. (Part 3 of 4.)
Search WWH ::




Custom Search