/* * TemperaturePanel.java */ import java.awt.*; import java.awt.event.*; import java.util.Observer; /** * This class models both View and Controller functionality. * * Note that the actual implementation of the Observer (View) interface is * left to concrete subclasses. This is because subclasses will render a * different view, depending whether the a Fahrenheit or Celsius representation * of the data model is to be rendered. */ public abstract class TemperaturePanel extends java.awt.Panel implements java.util.Observer { private String label; private TemperatureModel model; // data model that is both viewed and controlled from this object private TextField display; // textbox to display the current temperature private Button raiseButton; // button to increase the temp by 1 degree (F or C) private Button lowerButton; // button to decrease the temp by 1 degree (F or C) /** * Construct a new TemperaturePanel object. Need a String that labels * the panel with a name, and a TemperatureModel that will be linked * to this object (which is both a View and a Controller). * * @param label String to name this Panel * @param model data model to be linked w/ the View/Controller embedded in this object */ public TemperaturePanel(String label,TemperatureModel model) { this.label = label; this.model = model; this.setLayout(new BorderLayout()); display = new TextField(); raiseButton = new Button("Raise"); lowerButton = new Button("Lower"); this.add("North",new Label(label)); this.add("Center",display); Panel buttons = new Panel(); buttons.add(raiseButton); buttons.add(lowerButton); this.add("South",buttons); model.addObserver(this); // <-- MVC method that links the data Model with the View } /** * Hook through which program users can update the data model. Code * that implements updates to data model is found in the concrete * subclasses of this class. In the subclasses, the "display" TextField * object is linked to an ActionListener that interfaces with the data model. * This method merely sets the text in the TextField object. * * @param s new temperature value, entered by user at the screen. */ public void setDisplay(String s) { display.setText(s); } /** * Hook through which program users can view the current state of the * data (as rendered through this particular View object). Code that * actually updates the View (based on changes made to the data Model by * Controller objects) is found in the concrete subclasses of this class. * Concrete subclasses implement the Observer method by implementing the * update() method, which is called from the Model whenever its state has * changed. This method merely parses a double value out of the text * value found in the TextField object. * * @return double value in the TextField object, displayed to user. */ public double getDisplay() { double result = 0.0; try { result = Double.valueOf(display.getText()).doubleValue(); } catch ( NumberFormatException ex ) { } return result; } /** * Allow child objects access to the data model. * * @return TemperatureModel return model object to client */ protected TemperatureModel model() { return model; } // next three methods allow child objects to ActionListener // objects the GUI controls available to the end user. This // implements Controller functionality for this object. protected void addRaiseListener(ActionListener a) { raiseButton.addActionListener(a); } protected void addLowerListener(ActionListener a) { lowerButton.addActionListener(a); } protected void addDisplayListener(ActionListener a) { display.addActionListener(a); } }
![RSS](https://i.cmpnet.com/ddj/v2/images/rss.gif)
Instant Update
Use MVC to make your data and spreadsheets viewable online. Users can make charts and graphs that will be udpated with any changes you make.Related Reading
More Insights
INFO-LINK
![]() |
To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy. |