1 package sellwin.gui; 2 3 import javax.swing.*; 4 import java.awt.*; 5 import java.util.*; 6 import java.text.*; 7 8 12 17 public class DatePanel extends JPanel { 18 private String [] 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 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 (MONTHS)); 35 add(monthCombo); 36 dayCombo.setModel(new javax.swing.DefaultComboBoxModel ( 37 new String [] { "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 ( 41 new String [] { "2001", "2002", "2003" })); 42 add(yearCombo); 43 44 setColors(); 45 setFonts(); 46 } 47 48 52 public final void setDate(java.util.Date d) { 53 54 if (d == null) 55 d = new java.util.Date (); 56 57 Calendar cal = new GregorianCalendar(); 58 if (d != null) cal.setTime(d); 60 else 61 cal.setTime(new java.util.Date ()); 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 month = formatter.format(d); 70 yearCombo.setSelectedItem(Integer.toString(yyyy)); 71 dayCombo.setSelectedItem(Integer.toString(dd)); 72 monthCombo.setSelectedItem(month); 73 } catch (NumberFormatException e) { 74 ErrorHandler.show(this, e); 75 } 76 } 77 78 82 public final java.util.Date getDate() { 83 String yyyy = (String )(yearCombo.getSelectedItem()); 84 String dd = (String )(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 e) { 93 ErrorHandler.show(this, e); 94 } 95 return null; 96 } 97 98 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 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 |