Yurttas/PL/OOL/Java/F/05/01/05/03/DateFormat00.java
Revision as of 14:35, 7 November 2013 by MassBot1 (talk | contribs) (Created page with "<syntaxhighlight lang="java" line start="1" enclose="div">→* * @version 1.10 1999-10-14 * @author Cay Horstmann: import java.awt.*; import java.awt.event.*; import java...")
1/**
2 * @version 1.10 1999-10-14
3 * @author Cay Horstmann
4 */
5
6import java.awt.*;
7import java.awt.event.*;
8import java.text.*;
9import java.util.*;
10import javax.swing.*;
11
12public class DateFormat00 {
13 public static void main(String[] args) {
14 JFrame frame = new DateFormatFrame();
15 frame.show();
16 }
17}
18
19class DateFormatFrame extends JFrame {
20 public DateFormatFrame() {
21 setSize(400, 200);
22 setTitle("DateFormat00");
23
24 addWindowListener(new WindowAdapter() {
25 public void windowClosing(WindowEvent e) {
26 System.exit(0);
27 }
28 });
29
30 getContentPane().setLayout(new GridBagLayout());
31 GridBagConstraints gbc = new GridBagConstraints();
32
33 gbc.fill = GridBagConstraints.NONE;
34
35 gbc.anchor = GridBagConstraints.EAST;
36
37 add(new JLabel("Locale"), gbc, 0, 0, 1, 1);
38 add(new JLabel("Date style"), gbc, 0, 1, 1, 1);
39 add(new JLabel("Time style"), gbc, 2, 1, 1, 1);
40 add(new JLabel("Date"), gbc, 0, 2, 1, 1);
41 add(new JLabel("Time"), gbc, 0, 3, 1, 1);
42
43 gbc.anchor = GridBagConstraints.WEST;
44
45 add(localeCombo, gbc, 1, 0, 2, 1);
46 add(dateStyleCombo, gbc, 1, 1, 1, 1);
47 add(timeStyleCombo, gbc, 3, 1, 1, 1);
48 add(dateParseButton, gbc, 3, 2, 1, 1);
49 add(timeParseButton, gbc, 3, 3, 1, 1);
50 add(lenientCheckbox, gbc, 0, 4, 2, 1);
51
52 gbc.fill = GridBagConstraints.HORIZONTAL;
53
54 add(dateText, gbc, 1, 2, 2, 1);
55 add(timeText, gbc, 1, 3, 2, 1);
56
57 locales = DateFormat.getAvailableLocales();
58 for(int i=0; i<locales.length; i++)
59 localeCombo.addItem(locales[i].getDisplayName());
60 localeCombo.setSelectedItem(Locale.getDefault().getDisplayName());
61 currentDate = new Date();
62 currentTime = new Date();
63 updateDisplay();
64
65 ActionListener listener = new ActionListener() {
66 public void actionPerformed(ActionEvent event) {
67 updateDisplay();
68 }
69 };
70
71 localeCombo.addActionListener(listener);
72 dateStyleCombo.addActionListener(listener);
73 timeStyleCombo.addActionListener(listener);
74
75 dateParseButton.addActionListener(new ActionListener() {
76 public void actionPerformed(ActionEvent event) {
77 String d = dateText.getText();
78 try {
79 currentDateFormat.setLenient(lenientCheckbox.isSelected());
80 Date date = currentDateFormat.parse(d);
81 currentDate = date;
82 updateDisplay();
83 }
84 catch(ParseException e) {
85 dateText.setText("Parse error: " + d);
86 }
87 catch(IllegalArgumentException e) {
88 dateText.setText("Argument error: " + d);
89 }
90 }
91 });
92
93 timeParseButton.addActionListener(new ActionListener() {
94 public void actionPerformed(ActionEvent event) {
95 String t = timeText.getText();
96 try {
97 currentDateFormat.setLenient(lenientCheckbox.isSelected());
98 Date date = currentTimeFormat.parse(t);
99 currentTime = date;
100 updateDisplay();
101 }
102 catch(ParseException e) {
103 timeText.setText("Parse error: " + t);
104 }
105 catch(IllegalArgumentException e) {
106 timeText.setText("Argument error: " + t);
107 }
108 }
109 });
110 }
111
112 public void add(Component c,
113 GridBagConstraints gbc,
114 int x,
115 int y,
116 int w,
117 int h) {
118 gbc.gridx = x;
119 gbc.gridy = y;
120 gbc.gridwidth = w;
121 gbc.gridheight = h;
122 getContentPane().add(c, gbc);
123 }
124
125 public void updateDisplay() {
126 Locale currentLocale = locales[localeCombo.getSelectedIndex()];
127 int dateStyle = dateStyleCombo.getValue();
128 currentDateFormat = DateFormat.getDateInstance(dateStyle,
129 currentLocale);
130 String d = currentDateFormat.format(currentDate);
131 dateText.setText(d);
132 int timeStyle = timeStyleCombo.getValue();
133 currentTimeFormat = DateFormat.getTimeInstance(timeStyle,
134 currentLocale);
135 String t = currentTimeFormat.format(currentTime);
136 timeText.setText(t);
137 }
138
139 private Locale[] locales;
140
141 private Date currentDate;
142 private Date currentTime;
143 private DateFormat currentDateFormat;
144 private DateFormat currentTimeFormat;
145
146 private JComboBox localeCombo = new JComboBox();
147 private EnumCombo dateStyleCombo = new EnumCombo(DateFormat.class,
148 new String[]{"Default",
149 "Full",
150 "Long",
151 "Medium",
152 "Short"});
153 private EnumCombo timeStyleCombo = new EnumCombo(DateFormat.class,
154 new String[]{"Default",
155 "Full",
156 "Long",
157 "Medium",
158 "Short" });
159 private JButton dateParseButton = new JButton("Parse date");
160 private JButton timeParseButton = new JButton("Parse time");
161 private JTextField dateText = new JTextField(30);
162 private JTextField timeText = new JTextField(30);
163 private JTextField parseText = new JTextField(30);
164 private JCheckBox lenientCheckbox = new JCheckBox("Parse lenient",
165 true);
166}
167
168class EnumCombo extends JComboBox {
169 public EnumCombo(Class cl,
170 String[] labels) {
171 for(int i=0; i<labels.length; i++) {
172 String label = labels[i];
173 String name = label.toUpperCase().replace(' ', '_');
174 int value = 0;
175 try {
176 java.lang.reflect.Field f = cl.getField(name);
177 value = f.getInt(cl);
178 }
179 catch(Exception e) {
180 label = "(" + label + ")";
181 }
182 table.put(label, new Integer(value));
183 addItem(label);
184 }
185 setSelectedItem(labels[0]);
186 }
187
188 public int getValue() {
189 return ((Integer)table.get(getSelectedItem())).intValue();
190 }
191
192 private Hashtable table = new Hashtable();
193}