Java Reference
In-Depth Information
32. displayPanel = new JPanel();
33. display = new JTextArea(10, 40);
34. display.setEditable(false);
35. displayPanel.add(display);
36. content.add(displayPanel);
37.
38. controlPanel = new JPanel();
39. update = new JButton("Update Item");
40. exit = new JButton("Exit");
41. controlPanel.add(update);
42. controlPanel.add(exit);
43. content.add(controlPanel);
44.
45. update.addActionListener(this);
46. exit.addActionListener(this);
47.
48. mainFrame.addWindowListener(new WindowCloseManager());
49. mainFrame.pack();
50. mainFrame.setVisible(true);
51. }
52.
53.
54. public void actionPerformed(ActionEvent evt){
55. Object originator = evt.getSource();
56. if (originator == update){
57. updateItem();
58. }
59. else if (originator == exit){
60. exitApplication();
61. }
62. }
63.
64. private class WindowCloseManager extends WindowAdapter{
65. public void windowClosing(WindowEvent evt){
66. exitApplication();
67. }
68. }
69.
70. private void updateItem(){
71. editor.commitChanges();
72. display.setText(editor.toString());
73. }
74.
75. private void exitApplication(){
76. System.exit(0);
77. }
78. }
79.
Note that the Update Item button makes a call to the ItemEditor 's commitChanges method.
The RunPattern class runs this pattern by creating a Contact and an EditorGui object. The EditorGui
constructor sets the ItemEditor for the example.
Example A.25 RunPattern.java
1. import javax.swing.JComponent;
2. import javax.swing.JFrame;
3. import java.awt.event.WindowAdapter;
4. import java.awt.event.WindowEvent;
5.
6.
7. public class RunPattern{
8. public static void main(String [] arguments){
9. System.out.println("Example for the FactoryMethod pattern");
10. System.out.println();
11.
12. System.out.println("Creating a Contact object");
13. System.out.println();
14. Contact someone = new Contact();
15.
16. System.out.println("Creating a GUI editor for the Contact");
17. System.out.println();
18. System.out.println("The GUI defined in the EditorGui class is a truly generic
editor.");
19. System.out.println("It accepts an argument of type ItemEditor, and delegates");
20. System.out.println(" all editing tasks to its ItemEditor and the associated GUI.");
Search WWH ::




Custom Search