KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sellwin > gui > DatePanel


1 package sellwin.gui;
2
3 import javax.swing.*;
4 import java.awt.*;
5 import java.util.*;
6 import java.text.*;
7
8 // SellWin http://sourceforge.net/projects/sellwincrm
9
//Contact support@open-app.com for commercial help with SellWin
10
//This software is provided "AS IS", without a warranty of any kind.
11

12 /**
13  * This class implements the date panel that is shown in
14  * the date editor dialog. Day's and month's are
15  * represented by this widget.
16  */

17 public class DatePanel extends JPanel {
18     private String JavaDoc[] MONTHS = {
19         "Jan", "Feb", "Mar", "Apr", "May", "Jun",
20         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
21
22     private JComboBox yearCombo = new JComboBox();
23     private JComboBox monthCombo = new JComboBox();
24     private JComboBox dayCombo = new JComboBox();
25
26     /**
27      * construct the date panel
28      */

29     public DatePanel() {
30         super();
31         setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
32         setPreferredSize(new Dimension(170,22));
33         setMinimumSize(new Dimension(170,22));
34         monthCombo.setModel(new javax.swing.DefaultComboBoxModel JavaDoc(MONTHS));
35         add(monthCombo);
36         dayCombo.setModel(new javax.swing.DefaultComboBoxModel JavaDoc(
37             new String JavaDoc[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }));
38         add(dayCombo);
39
40         yearCombo.setModel(new javax.swing.DefaultComboBoxModel JavaDoc(
41             new String JavaDoc[] { "2001", "2002", "2003" }));
42         add(yearCombo);
43
44         setColors();
45         setFonts();
46     }
47
48     /**
49      * set the date shown by the date panel
50      * @param d the date we are to use
51      */

52     public final void setDate(java.util.Date JavaDoc d) {
53     
54         if (d == null)
55             d = new java.util.Date JavaDoc();
56     
57         Calendar cal = new GregorianCalendar();
58         if (d != null) //use the current date & time if input is null
59
cal.setTime(d);
60         else
61             cal.setTime(new java.util.Date JavaDoc());
62         int yyyy = cal.get(Calendar.YEAR);
63         int mm = cal.get(Calendar.MONTH);
64         int dd = cal.get(Calendar.DAY_OF_MONTH);
65     
66         SimpleDateFormat formatter = new SimpleDateFormat("MMM");
67
68         try {
69             String JavaDoc month = formatter.format(d);
70             yearCombo.setSelectedItem(Integer.toString(yyyy));
71             dayCombo.setSelectedItem(Integer.toString(dd));
72             monthCombo.setSelectedItem(month);
73         } catch (NumberFormatException JavaDoc e) {
74             ErrorHandler.show(this, e);
75         }
76     }
77
78     /**
79      * get the date this panel is using
80      * @return the date
81      */

82     public final java.util.Date JavaDoc getDate() {
83         String JavaDoc yyyy = (String JavaDoc)(yearCombo.getSelectedItem());
84         String JavaDoc dd = (String JavaDoc)(dayCombo.getSelectedItem());
85         int mm = monthCombo.getSelectedIndex();
86
87         try {
88             int iyyyy = Integer.parseInt(yyyy);
89             int idd = Integer.parseInt(dd);
90             Calendar cal = new GregorianCalendar(iyyyy, mm, idd);
91             return cal.getTime();
92         } catch (NumberFormatException JavaDoc e) {
93             ErrorHandler.show(this, e);
94         }
95         return null;
96     }
97
98     /**
99      * set the panel's fonts
100      */

101     private final void setFonts() {
102         yearCombo.setFont(MainWindow.FIELD_FONT);
103         monthCombo.setFont(MainWindow.FIELD_FONT);
104         dayCombo.setFont(MainWindow.FIELD_FONT);
105     }
106
107     /**
108      * set this panel's colors
109      */

110     private final void setColors() {
111         yearCombo.setBackground(Color.white);
112         monthCombo.setBackground(Color.white);
113         dayCombo.setBackground(Color.white);
114     }
115 }
116
Popular Tags