You're here: Snippet Directory » Java (241)
Language:

Simple Pick date Calendar

Language: English
Programming Language: Java
Published by: tommyg [not registered]
Last Update: 5/15/2006
Views: 257

License: BSD License

Description

This is a simple pop up calandar that allows the user to pick a date. A takes advantage of the Java calendaring features.

Code

1 import java.awt.BorderLayout; 2 import java.awt.Color; 3 import java.awt.Dimension; 4 import java.awt.FontMetrics; 5 import java.awt.Graphics; 6 import java.awt.Graphics2D; 7 import java.awt.GridLayout; 8 import java.awt.event.ActionListener; 9 import java.awt.event.ActionEvent; 10 import java.awt.event.MouseAdapter; 11 import java.awt.event.MouseEvent; 12 import java.text.DateFormatSymbols; 13 import java.util.Calendar; 14 15 import javax.swing.BorderFactory; 16 import javax.swing.JButton; 17 import javax.swing.JComboBox; 18 import javax.swing.JComponent; 19 import javax.swing.JLabel; 20 import javax.swing.JPanel; 21 import javax.swing.JSpinner; 22 import javax.swing.SpinnerNumberModel; 23 import javax.swing.UIManager; 24 import javax.swing.event.ChangeEvent; 25 import javax.swing.event.ChangeListener; 26 27 public class DatePanel extends JPanel { 28 private static final int MAX_DAYS = 42; 29 private static final DateFormatSymbols SYMBOLS = new DateFormatSymbols(); 30 31 private final DayField[] dayCmp = new DayField[MAX_DAYS]; 32 private DayField current; 33 private JComboBox monthCmp; 34 private SpinnerNumberModel yearModel; 35 private JSpinner yearCmp; 36 private JButton today; 37 private ActionListener[] listeners = new ActionListener[0]; 38 private String command = "DateChanged"; 39 private ActionEvent event; 40 41 private Calendar cal; 42 43 public DatePanel() { 44 super(new BorderLayout()); 45 cal = Calendar.getInstance(); 46 add(createNorthPanel(), BorderLayout.NORTH); 47 add(createCenterPanel(), BorderLayout.CENTER); 48 add(createSouthPanel(), BorderLayout.SOUTH); 49 updateDays(); 50 } 51 52 public void addActionListener(ActionListener l) { 53 ActionListener[] tmp = listeners; 54 int len = listeners.length; 55 ActionListener[] tmp1 = new ActionListener[len + 1]; 56 if (len > 0) { 57 System.arraycopy(tmp, 0, tmp1, 0, len); 58 } 59 tmp1[len] = l; 60 listeners = tmp1; 61 } 62 63 public void removeActionListener(ActionListener l) { 64 ActionListener[] tmp = listeners; 65 int len = tmp.length; 66 for (int i = 0; i < len; ++i) { 67 if (tmp[i] == l) { 68 --len; 69 ActionListener[] tmp1 = new ActionListener[len]; 70 if (i > 0) { 71 System.arraycopy(tmp, 0, tmp1, 0, i); 72 } 73 if (i < len) { 74 System.arraycopy(tmp, i + 1, tmp1, i, len - i); 75 } 76 listeners = tmp1; 77 break; 78 } 79 } 80 } 81 82 public void setActionCommand(String command) { 83 if (!command.equals(this.command)) { 84 this.command = command; 85 event = null; 86 } 87 } 88 89 public String getActionCommand() { 90 return command; 91 } 92 93 public Calendar getCalendar() { 94 return cal; 95 } 96 97 private void fireActionPerformed() { 98 ActionListener[] tmp = listeners; 99 int len = tmp.length; 100 if (event == null) { 101 event = new ActionEvent(DatePanel.this, ActionEvent.ACTION_PERFORMED, command); 102 } 103 for (int i = 0; i < len; ++i) { 104 tmp[i].actionPerformed(event); 105 } 106 } 107 108 private JPanel createNorthPanel() { 109 JPanel p = new JPanel(); 110 String[] dfsm = SYMBOLS.getMonths(); 111 String[] months = new String[12]; 112 // Has to be done this way to copy just 12 months (out of 13!) 113 for (int i = 0; i < 12; ++i) { 114 months[i] = dfsm[i]; 115 } 116 monthCmp = new JComboBox(months); 117 monthCmp.setSelectedIndex(cal.get(Calendar.MONTH)); 118 monthCmp.addActionListener(new ActionListener() { 119 public void actionPerformed(ActionEvent e) { 120 cal.set(Calendar.MONTH, monthCmp.getSelectedIndex()); 121 updateDays(); 122 } 123 }); 124 p.add(monthCmp); 125 126 yearModel = new SpinnerNumberModel(cal.get(Calendar.YEAR), 127 cal.getMinimum(Calendar.YEAR), cal.getMaximum(Calendar.YEAR), 1); 128 yearCmp = new JSpinner(yearModel); 129 yearCmp.setEditor(new JSpinner.NumberEditor(yearCmp, "####")); 130 yearCmp.addChangeListener(new ChangeListener() { 131 public void stateChanged(ChangeEvent e) { 132 cal.set(Calendar.YEAR, yearModel.getNumber().intValue()); 133 updateDays(); 134 } 135 }); 136 p.add(yearCmp); 137 138 return p; 139 } 140 141 private JPanel createCenterPanel() { 142 JPanel p = new JPanel(new GridLayout(7, 7, 1, 1)); 143 p.setBackground(Color.WHITE); 144 p.setBorder(BorderFactory.createLoweredBevelBorder()); 145 146 String[] dfsd = SYMBOLS.getShortWeekdays(); 147 for (int i = 0; i < 7; i++) { 148 p.add(new JLabel(dfsd[i + 1].substring(0, 1), JLabel.CENTER)); 149 } 150 MouseAdapter a = new MouseAdapter() { 151 public void mouseClicked(MouseEvent e) { 152 DayField f = (DayField) e.getSource(); 153 if (f.day != 0) { 154 cal.set(cal.DAY_OF_MONTH, f.day); 155 fireActionPerformed(); 156 updateDays(); 157 } 158 } 159 }; 160 for (int i = 0; i < MAX_DAYS; ++i) { 161 dayCmp[i] = new DayField(); 162 dayCmp[i].addMouseListener(a); 163 p.add(dayCmp[i]); 164 } 165 return p; 166 } 167 168 private JPanel createSouthPanel() { 169 JPanel p = new JPanel(); 170 today = new JButton("Today"); 171 today.addActionListener(new ActionListener() { 172 public void actionPerformed(ActionEvent ae) { 173 cal.setTimeInMillis(System.currentTimeMillis()); 174 updateDays(); 175 fireActionPerformed(); 176 } 177 }); 178 p.add(today); 179 return p; 180 } 181 182 private void updateDays() { 183 int day = cal.get(cal.DAY_OF_MONTH); 184 cal.set(cal.DAY_OF_MONTH, 1); 185 int dow = cal.get(Calendar.DAY_OF_WEEK) - 1; 186 int max = cal.getActualMaximum(Calendar.DAY_OF_MONTH); 187 int i = 0; 188 if (current != null) { 189 current.selected = false; 190 } 191 while (i < dow) { 192 dayCmp[i++].day = 0; 193 } 194 int d = 1; 195 while (d <= max) { 196 if (d == day) { 197 current = dayCmp[i]; 198 current.selected = true; 199 } 200 dayCmp[i++].day = d++; 201 } 202 while (i < MAX_DAYS) { 203 dayCmp[i++].day = 0; 204 } 205 cal.set(cal.DAY_OF_MONTH, day); 206 repaint(); 207 } 208 209 private static class DayField extends JComponent { 210 static Color hf = UIManager.getColor("textHighlightText"); 211 static Color hb = UIManager.getColor("textHighlight"); 212 int day; 213 boolean selected; 214 215 public void paint(Graphics g) { 216 if (day != 0) { 217 Graphics2D g2 = (Graphics2D) g; 218 Dimension d = getSize(); 219 FontMetrics fm = g2.getFontMetrics(); 220 String s = String.valueOf(day); 221 int w = fm.stringWidth(s); 222 int x = Math.round((d.width - w) / 2f); 223 Color fg = null, bg = null; 224 if (selected) { 225 fg = g2.getColor(); 226 bg = g2.getBackground(); 227 g2.setColor(hf); 228 g2.setBackground(hb); 229 g2.clearRect(x, 0, w, fm.getHeight()); 230 } 231 g2.drawString(s, x, fm.getAscent()); 232 if (selected) { 233 g2.setColor(fg); 234 g2.setBackground(bg); 235 } 236 } 237 } 238 } 239 } 240 241 import java.awt.BorderLayout; 242 import java.awt.event.ActionEvent; 243 import java.awt.event.ActionListener; 244 import java.util.Calendar; 245 246 import javax.swing.JDialog; 247 import javax.swing.JFrame; 248 249 public class DateDialog extends JDialog { 250 private DatePanel datePanel = new DatePanel(); 251 252 public DateDialog() { 253 this(null); 254 } 255 256 public DateDialog(JFrame f) { 257 super(f, "Select a Date", true); 258 getContentPane().add(datePanel, BorderLayout.CENTER); 259 datePanel.addActionListener(new ActionListener() { 260 public void actionPerformed(ActionEvent e) { 261 hide(); 262 } 263 }); 264 } 265 266 public Calendar getCalendar() { 267 return datePanel.getCalendar(); 268 } 269 } 270 271 import javax.swing.JDialog; 272 import javax.swing.JTextField; 273 import javax.swing.JButton; 274 import javax.swing.JPanel; 275 276 import java.awt.event.ActionListener; 277 import java.awt.event.ActionEvent; 278 import java.awt.event.WindowAdapter; 279 import java.awt.event.WindowEvent; 280 281 import java.util.Calendar; 282 import java.util.Date; 283 import java.text.DateFormat; 284 285 //test routine to call DateDialog 286 public class CallDateDialogExample extends JDialog { 287 Calendar calendar; 288 DateDialog dateDialog; 289 JTextField sText; 290 JPanel jp; 291 JButton btn; 292 Calendar retCal = null; 293 DateFormat dft; 294 295 CallDateDialogExample() { 296 297 dateDialog = new DateDialog(); 298 299 calendar = Calendar.getInstance(); 300 dft = DateFormat.getDateInstance(DateFormat.LONG); 301 Date date = calendar.getTime(); 302 String sDate = dft.format(date); 303 304 sText = new JTextField(14); 305 sText.setEditable(false); 306 sText.setText(sDate); 307 308 btn = new JButton("click me"); 309 310 jp = new JPanel(); 311 jp.add(sText); 312 jp.add(btn); 313 314 getContentPane().add(jp); 315 316 btn.addActionListener(new ActionListener() { 317 public void actionPerformed(ActionEvent e) { 318 dateDialog.pack(); 319 dateDialog.show(); 320 321 retCal = dateDialog.getCalendar(); 322 Date aDate = retCal.getTime(); 323 String tempString = dft.format(aDate); 324 sText.setText(tempString); 325 } 326 }); 327 } 328 329 public static void main(String[] args) { 330 CallDateDialogExample cdd = new CallDateDialogExample(); 331 cdd.pack(); 332 cdd.setVisible(true); 333 334 cdd.addWindowListener(new WindowAdapter() { 335 public void windowClosing(WindowEvent we) { 336 System.exit(0); 337 } 338 }); 339 } 340 } 341

No comments avaiable

Add a comment

Name *  

Email (won't be displayed) *    

Website  

Comment *  

Sicherheitscode Security Code *    

RSS