Java Reference
In-Depth Information
66. while (notifyViews.hasNext()){
67. ((ContactView)notifyViews.next()).refreshContactView(firstName, lastName, title,
organization);
68. }
69. }
70. }
The ContactModel maintains an ArrayList of ContactView objects, updating them whenever the model data
changes. The standard behavior for all views is defined by the ContactView interface method
refreshContactView .
Example A.200 ContactView.java
1. public interface ContactView{
2. public void refreshContactView(String firstName,
3. String lastName, String title, String organization);
4. }
Two views are used in this example. The first, ContactDisplayView , displays the updated model information but
does not support a controller, an example of “view-only” behavior.
Example A.201 ContactDisplayView.java
1. import javax.swing.JPanel;
2. import javax.swing.JScrollPane;
3. import javax.swing.JTextArea;
4. import java.awt.BorderLayout;
5. public class ContactDisplayView extends JPanel implements ContactView{
6. private JTextArea display;
7.
8. public ContactDisplayView(){
9. createGui();
10. }
11.
12. public void createGui(){
13. setLayout(new BorderLayout());
14. display = new JTextArea(10, 40);
15. display.setEditable(false);
16. JScrollPane scrollDisplay = new JScrollPane(display);
17. this.add(scrollDisplay, BorderLayout.CENTER);
18. }
19.
20. public void refreshContactView(String newFirstName,
21. String newLastName, String newTitle, String newOrganization){
22. display.setText("UPDATED CONTACT:\nNEW VALUES:\n" +
23. "\tName: " + newFirstName + " " + newLastName +
24. "\n" + "\tTitle: " + newTitle + "\n" +
25. "\tOrganization: " + newOrganization);
26. }
27. }
The second view is ContactEditView , which allows a user to update the contact defined by the model.
Example A.202 ContactEditView.java
1. import javax.swing.BoxLayout;
2. import javax.swing.JButton;
3. import javax.swing.JLabel;
4. import javax.swing.JTextField;
5. import javax.swing.JPanel;
6. import java.awt.GridLayout;
7. import java.awt.BorderLayout;
8. import java.awt.event.ActionListener;
9. import java.awt.event.ActionEvent;
10. public class ContactEditView extends JPanel implements ContactView{
11. private static final String UPDATE_BUTTON = "Update";
12. private static final String EXIT_BUTTON = "Exit";
13. private static final String CONTACT_FIRST_NAME = "First Name ";
14. private static final String CONTACT_LAST_NAME = "Last Name ";
15. private static final String CONTACT_TITLE = "Title ";
16. private static final String CONTACT_ORG = "Organization ";
17. private static final int FNAME_COL_WIDTH = 25;
18. private static final int LNAME_COL_WIDTH = 40;
19. private static final int TITLE_COL_WIDTH = 25;
20. private static final int ORG_COL_WIDTH = 40;
21. private ContactEditController controller;
Search WWH ::




Custom Search