Java Reference
In-Depth Information
Observer (Chapter 9)
SOLUTION 9.1
One solution is:
public JSlider slider()
{
if (slider == null)
{
slider = new JSlider();
sliderMax = slider.getMaximum();
sliderMin = slider.getMinimum();
slider.addChangeListener(this);
slider.setValue(slider.getMinimum());
}
return slider;
}
public void stateChanged(ChangeEvent e)
{
double val = slider.getValue();
double tp = (val - sliderMin) / (sliderMax - sliderMin);
burnPanel().setTPeak(tp);
thrustPanel().setTPeak(tp);
valueLabel().setText("" + tp);
}
SOLUTION 9.2
One solution is shown in Figure B.9. To allow a label to register for slider events, the design
in Figure B.9 creates a subclass of JLabel that implements ChangeListener . The new
design lets the components that depend on the slider register their interest and update
themselves. This is arguably an improvement, but we will refactor the design again, to a
model/view/control architecture.
Search WWH ::




Custom Search