Java Reference
In-Depth Information
19. private CalendarEditor editor;
20. private JButton save, exit;
21.
22. public StateGui(CalendarEditor edit){
23. editor = edit;
24. }
25.
26. public void createGui(){
27. mainFrame = new JFrame("State Pattern Example");
28. Container content = mainFrame.getContentPane();
29. content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
30.
31. editPanel = new JPanel();
32. editPanel.setLayout(new BorderLayout());
33. JTable appointmentTable = new JTable(new StateTableModel((Appointment [])
editor.getAppointments().toArray(new Appointment[1])));
34. editPanel.add(new JScrollPane(appointmentTable));
35. content.add(editPanel);
36.
37. controlPanel = new JPanel();
38. save = new JButton("Save Appointments");
39. exit = new JButton("Exit");
40. controlPanel.add(save);
41. controlPanel.add(exit);
42. content.add(controlPanel);
43.
44. save.addActionListener(this);
45. exit.addActionListener(this);
46.
47. mainFrame.addWindowListener(new WindowCloseManager());
48. mainFrame.pack();
49. mainFrame.setVisible(true);
50. }
51.
52.
53. public void actionPerformed(ActionEvent evt){
54. Object originator = evt.getSource();
55. if (originator == save){
56. saveAppointments();
57. }
58. else if (originator == exit){
59. exitApplication();
60. }
61. }
62.
63. private class WindowCloseManager extends WindowAdapter{
64. public void windowClosing(WindowEvent evt){
65. exitApplication();
66. }
67. }
68.
69. private void saveAppointments(){
70. editor.save();
71. }
72.
73. private void exitApplication(){
74. System.exit(0);
75. }
76.
77. private class StateTableModel extends AbstractTableModel{
78. private final String [] columnNames = {
79. "Appointment", "Contacts", "Location", "Start Date", "End Date" };
80. private Appointment [] data;
81.
82. public StateTableModel(Appointment [] appointments){
83. data = appointments;
84. }
85.
86. public String getColumnName(int column){
87. return columnNames[column];
88. }
89. public int getRowCount(){ return data.length; }
90. public int getColumnCount(){ return columnNames.length; }
91. public Object getValueAt(int row, int column){
92. Object value = null;
93. switch(column){
94. case 0: value = data[row].getReason();
95. break;
Search WWH ::




Custom Search