Java Reference
In-Depth Information
The second view is ContactEditView , which allows a user to update the contact defined by the model.
Example 4.4 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;
22. private JLabel firstNameLabel, lastNameLabel, titleLabel, organizationLabel;
23. private JTextField firstName, lastName, title, organization;
24. private JButton update, exit;
25.
26. public ContactEditView(ContactModel model){
27. controller = new ContactEditController(model, this);
28. createGui();
29. }
30. public ContactEditView(ContactModel model, ContactEditController newController){
31. controller = newController;
32. createGui();
33. }
34.
35. public void createGui(){
36. update = new JButton(UPDATE_BUTTON);
37. exit = new JButton(EXIT_BUTTON);
38.
39. firstNameLabel = new JLabel(CONTACT_FIRST_NAME);
40. lastNameLabel = new JLabel(CONTACT_LAST_NAME);
41. titleLabel = new JLabel(CONTACT_TITLE);
42. organizationLabel = new JLabel(CONTACT_ORG);
43.
44. firstName = new JTextField(FNAME_COL_WIDTH);
45. lastName = new JTextField(LNAME_COL_WIDTH);
46. title = new JTextField(TITLE_COL_WIDTH);
47. organization = new JTextField(ORG_COL_WIDTH);
48.
49. JPanel editPanel = new JPanel();
50. editPanel.setLayout(new BoxLayout(editPanel, BoxLayout.X_AXIS));
51.
52. JPanel labelPanel = new JPanel();
53. labelPanel.setLayout(new GridLayout(0, 1));
54.
55. labelPanel.add(firstNameLabel);
56. labelPanel.add(lastNameLabel);
57. labelPanel.add(titleLabel);
58. labelPanel.add(organizationLabel);
59.
60. editPanel.add(labelPanel);
61.
62. JPanel fieldPanel = new JPanel();
63. fieldPanel.setLayout(new GridLayout(0, 1));
64.
65. fieldPanel.add(firstName);
66. fieldPanel.add(lastName);
67. fieldPanel.add(title);
68. fieldPanel.add(organization);
69.
70. editPanel.add(fieldPanel);
71.
72. JPanel controlPanel = new JPanel();
73. controlPanel.add(update);
74. controlPanel.add(exit);
Search WWH ::




Custom Search