Java Programming Lecture 18 Alice E. Fischer
April 3, 2012
Alice E. Fischer
()
Java Programming - L18. . .
1/20
April 3, 2012
1 / 20
Outline
1
Model-View-Controller
2
Christmas Tree
3
Inner Classes
4
Building Windows
5
Summary and Homework
Alice E. Fischer
()
Java Programming - L18. . .
2/20
April 3, 2012
2 / 20
Model-View-Controller
Model-View-Controller
Purpose and Principles The Event Loop In the Tree program
Alice E. Fischer
()
Java Programming - L18. . .
3/20
April 3, 2012
3 / 20
Model-View-Controller
Model-View-Controller MVC is probably the most important OO design pattern. It is a guideline for the architecture of any GUI-based application. The application has three parts: the Model, the Controller, and one or more Viewers. The Model consists of all the variables and data structures that support the application, plus the functions that work on those data members. A Viewer presents formatted data from the model. The Controller runs the handles user interaction and responds to events. It owns the Model and supervises all changes to the Model. Thus, the Controller is always positioned between the Model and the Viewers.
Alice E. Fischer
()
Java Programming - L18. . .
4/20
April 3, 2012
4 / 20
Model-View-Controller
How it Works
When a Viewer is created, it is given a pointer to the Model and it is put on a list of Viewers owned by the Controller. All changes to the Model are supervised by the Controller. The Controller “notifies” all of the viewers on its list whenever the Model changes. When a Viewer becomes active, it tests whether it has been notified that there was a change in the model. If so, it “pulls over” information from the relevant parts of the model to refresh its display. Flow of information from Model to Viewer is limited to the amount of info that is needed, at the time the Viewer needs to display it. (NOT every time the model changes.)
Alice E. Fischer
()
Java Programming - L18. . .
5/20
April 3, 2012
5 / 20
Model-View-Controller
Model-View-Controller: Purpose
Why divide up an application like this? Separating the components makes an application better organized. Instead of one very long class with three intermingled purposes, there are several shorter classes, each one focusing on one part of the job. Separating the controller from the model allows us to use the same controller code for different business models. The business logic would be in the Model class. This is economically important for a software business that sells customized products. Separating the viewer from the controller permits the app to support multiple independent viewers.
Alice E. Fischer
()
Java Programming - L18. . .
6/20
April 3, 2012
6 / 20
Model-View-Controller
An MVC Problem: Communication between classes. The Controller needs to change values inside the model If members of the model are private, all changes would need to be done using set functions. This badly clutters both the class definition and the logic of the program. If the model data is public, then it has no protection against change from anywhere in the program. This is not acceptable. If you omit the protection word, the default is that the data member is visible everywhere in the package. This is appropriate if one class cannot exist and does not make sense without the other. When you use the package to let the Controller access the Model, you have a tightly coupled pair of classes.
Alice E. Fischer
()
Java Programming - L18. . .
7/20
April 3, 2012
7 / 20
Model-View-Controller
A Solution: Use Package Visibility.
The Controller needs to change values inside the model In this coupled pair, the Controller is the “master” and the Model is the “helper”. The Controller can use the Model’s variables but not vice versa. This is useful to keep your classes organized neatly without forcing the use of many get and set functions. Since the Viewers are strongly tied to the Model, it makes sense to put them in the same package.
Alice E. Fischer
()
Java Programming - L18. . .
8/20
April 3, 2012
8 / 20
Christmas Tree
MVC Example
The ChristmasTree Program The Event Loop Inner Classes and the Timer CheckBoxes
Alice E. Fischer
()
Java Programming - L18. . .
9/20
April 3, 2012
9 / 20
Christmas Tree
In the Tree program
The model is the instance of TreeModel, owned by Tree. Information id downloaded from the GUI and stored in the model. The viewer is the Canvas. It displays a Christmas tree with colored lights. The controller is Tree, which is a JFrame class. Tree instantiates both the model and the viewer. When the viewer (Canvas) is instantiated, it is given a pointer to the model (TreeModel). It uses this pointer to find out whether the lights are on or off and which colors should be displayed.
Alice E. Fischer
()
Java Programming - L18. . .
10/20
April 3, 2012
10 / 20
Christmas Tree
The Event Queue
Each JButton in the GUI registers a listener-object (in this program it is baum, the instance of Tree). When the user clicks on one of the JButtons on the display, that mouse-click will be sensed by the hardware. The hardware will report the click to the OS, which will determine that it came from a window owned by the JVM. The event report will be sent to the JVM. The JVM will use the screen coordinates of the click and identify the button inside the currently-active-window that covers the click. Now it knows the source of the event. The click (with its source) is put on the event queue.
Alice E. Fischer
()
Java Programming - L18. . .
11/20
April 3, 2012
11 / 20
Christmas Tree
The Event Queue
TreeModel Swing
AWT Event thread
JFrame
Tree On button
JPanel
3. Click is detected in the Event thread and put in the event queue.
Alice E. Fischer
Canvas
1. Register this as a listener for the On button.
4. Event thread "calls back" the Tree object. Control goes to the ActionPerformed function of the registered listener.
Widgets 2. Click the On button
()
Java Programming - L18. . .
12/20
April 3, 2012
12 / 20
Christmas Tree
Reporting an Event An event loop processes the event queue, one event at a time. When an event comes to the head of the queue, an object of type Event will be sent to whatever listener is registered for the source of that event. This is a called a callback. In the Christmas Tree program, baum receives the callbacks for the Jbuttons. The callback goes to the ActionPerformed() method of the listener, which usually processes it by changing the model, then calls for the view to be redisplayed. When the display is refreshed, the Viewer will pull over new information from the model and the display will be changed.
Alice E. Fischer
()
Java Programming - L18. . .
13/20
April 3, 2012
13 / 20
Inner Classes
Inner Classes There are various kinds of action events. It is not desirable to have them all reported to the same ActionPerformed() method. Suppose a program has both button and timer events. We can separate these events by defining two objects as listeners. This is done by defining an inner class, within the JFrame class. This inner class contains only an ActionPerformed() method, nothing more. Then we register our JFrame as the listener for the button events. In the JFrame constructor, we instantiate the inner class and register the inner-object as the listener for the timer events. Now we have two ActionPerformed() methods listening for two different types of actions.
Alice E. Fischer
()
Java Programming - L18. . .
14/20
April 3, 2012
14 / 20
Inner Classes
Inner Classes
Here is the definition and instantiation of the inner class in the Christmas Tree program: class TimerListener implements ActionListener { public void actionPerformed( ActionEvent e) { blink(); repaint(); } } In the constructor of the JFrame class: private Timer tim = new Timer( 1000, new TimerListener());
Alice E. Fischer
()
Java Programming - L18. . .
15/20
April 3, 2012
15 / 20
Inner Classes
Anonymous Inner Classes
These strange things are automatically generated by window-builders to handle events. class TimerListener implements ActionListener { public void actionPerformed( ActionEvent e) { blink(); repaint(); } } In the constructor of the JFrame class: private Timer tim = new Timer( 1000, new TimerListener());
Alice E. Fischer
()
Java Programming - L18. . .
16/20
April 3, 2012
16 / 20
Inner Classes
CheckBoxes
import javax.Swing.*; import java.awt.event.*; JPanel colorPanel = new JPanel(); JCheckBox blueBox = new JCheckBox("Blue"); colorPanel.add(blueBox); Register a listener: blueBox.addActionListener(this); Process the action: boolean[] colors={false,false,false,false,false}; myModel.colors[4] = blueBox.isSelected();
Alice E. Fischer
()
Java Programming - L18. . .
17/20
April 3, 2012
17 / 20
Inner Classes
The CheckBoxes for the Christmas Tree
Two ways are shown to handle the check boxes and buttons. lights uses one ActionPerformed() method for all the widgets. Three if statements are used to detect action on each of the three buttons. If all the if’s fail, then the action came from a checkbox. In that case, the current status of all five boxes are downloaded. This is a shortcut that avoids writing five separate if statements. It is possible because the status of a checkbox has no effect on the other check box status values. Is this shortcut desirable? Probably not! lights2 uses a separate inner class, with its own ActionPerformed() method, is defined to handle each active widget.
Alice E. Fischer
()
Java Programming - L18. . .
18/20
April 3, 2012
18 / 20
Building Windows
Steps in Creating a JFrame Application
Open up your Eclipse (Indigo version) app. Make a new empty project and a new empty package within that project. Go to “File” on the main menu. Select “New”, then “Other” at the bottom of the drop-down menu. Open up the triangle for WindowBuilder, then open the triangle for “SwingDesigner”. Select JFrame. Next give it a name. It will create a GUI class file and a design file. Look for the two icons at the bottom of the main code window.
Alice E. Fischer
()
Java Programming - L18. . .
19/20
April 3, 2012
19 / 20
Summary and Homework
Java Summary
We have discussed the following packages, classes, methods, and concepts today: CheckBoxes Model – viewer – controller. What “package” really means: tightly coupled classes The Eclipse window builder.
Alice E. Fischer
()
Java Programming - L18. . .
20/20
April 3, 2012
20 / 20