Java Reference
In-Depth Information
20. add(new JScrollPane(displayRegion));
21. }
22. public void contactChanged(Contact contact){
23. displayRegion.setText(
24. "Contact\n\tName: " + contact.getFirstName() +
25. " " + contact.getLastName() + "\n\tTitle: " +
26. contact.getTitle() + "\n\tOrganization: " +
27. contact.getOrganization());
28. }
29. public void setContactMediator(ContactMediator newMediator){
30. mediator = newMediator;
31. }
32. }
ContactSelectorPanel allows the user to choose a Contact for display and edit in the MediatorGui .
Example A.76 ContactSelectorPanel.java
1. import java.awt.event.ActionEvent;
2. import java.awt.event.ActionListener;
3. import javax.swing.JComboBox;
4. import javax.swing.JPanel;
5.
6. public class ContactSelectorPanel extends JPanel implements ActionListener{
7. private ContactMediator mediator;
8. private JComboBox selector;
9.
10. public ContactSelectorPanel(){
11. createGui();
12. }
13. public ContactSelectorPanel(ContactMediator newMediator){
14. setContactMediator(newMediator);
15. createGui();
16. }
17.
18. public void createGui(){
19. selector = new JComboBox(mediator.getAllContacts());
20. selector.addActionListener(this);
21. add(selector);
22. }
23.
24. public void actionPerformed(ActionEvent evt){
25. mediator.selectContact((Contact)selector.getSelectedItem());
26. }
27. public void addContact(Contact contact){
28. selector.addItem(contact);
29. selector.setSelectedItem(contact);
30. }
31. public void setContactMediator(ContactMediator newMediator){
32. mediator = newMediator;
33. }
34. }
The ContactEditorPanel provides an editing interface for the currently selected Contact . It has buttons that
allow a user to add or update a Contact .
Example A.77 ContactEditorPanel.java
1. import java.awt.BorderLayout;
2. import java.awt.GridLayout;
3. import java.awt.event.ActionEvent;
4. import java.awt.event.ActionListener;
5. import javax.swing.JButton;
6. import javax.swing.JLabel;
7. import javax.swing.JPanel;
8. import javax.swing.JTextField;
9. public class ContactEditorPanel extends JPanel implements ActionListener{
10. private ContactMediator mediator;
11. private JTextField firstName, lastName, title, organization;
12. private JButton create, update;
13.
14. public ContactEditorPanel(){
15. createGui();
16. }
17. public ContactEditorPanel(ContactMediator newMediator){
18. setContactMediator(newMediator);
19. createGui();
Search WWH ::




Custom Search