Java Reference
In-Depth Information
20. }
21. public void createGui(){
22. setLayout(new BorderLayout());
23.
24. JPanel editor = new JPanel();
25. editor.setLayout(new GridLayout(4, 2));
26. editor.add(new JLabel("First Name:"));
27. firstName = new JTextField(20);
28. editor.add(firstName);
29. editor.add(new JLabel("Last Name:"));
30. lastName = new JTextField(20);
31. editor.add(lastName);
32. editor.add(new JLabel("Title:"));
33. title = new JTextField(20);
34. editor.add(title);
35. editor.add(new JLabel("Organization:"));
36. organization = new JTextField(20);
37. editor.add(organization);
38. add(editor, BorderLayout.CENTER);
39.
40. JPanel control = new JPanel();
41. create = new JButton("Create Contact");
42. update = new JButton("Update Contact");
43. create.addActionListener(this);
44. update.addActionListener(this);
45. control.add(create);
46. control.add(update);
47. add(control, BorderLayout.SOUTH);
48. }
49. public void actionPerformed(ActionEvent evt){
50. Object source = evt.getSource();
51. if (source == create){
52. createContact();
53. }
54. else if (source == update){
55. updateContact();
56. }
57. }
58.
59. public void createContact(){
60. mediator.createContact(firstName.getText(), lastName.getText(),
61. title.getText(), organization.getText());
62. }
63. public void updateContact(){
64. mediator.updateContact(firstName.getText(), lastName.getText(),
65. title.getText(), organization.getText());
66. }
67.
68. public void setContactFields(Contact contact){
69. firstName.setText(contact.getFirstName());
70. lastName.setText(contact.getLastName());
71. title.setText(contact.getTitle());
72. organization.setText(contact.getOrganization());
73. }
74. public void setContactMediator(ContactMediator newMediator){
75. mediator = newMediator;
76. }
77. }
The ContactMediator interface defines set methods for each of the GUI components, and for the business
methods createContact , updateContact , selectContact and getAllContacts .
Example A.78 ContactMediator.java
1. public interface ContactMediator{
2. public void setContactDisplayPanel(ContactDisplayPanel displayPanel);
3. public void setContactEditorPanel(ContactEditorPanel editorPanel);
4. public void setContactSelectorPanel(ContactSelectorPanel selectorPanel);
5. public void createContact(String firstName, String lastName, String title, String
organization);
6. public void updateContact(String firstName, String lastName, String title, String
organization);
7. public Contact [] getAllContacts();
8. public void selectContact(Contact contact);
9. }
Search WWH ::




Custom Search