/** * Java Date Chooser Class * Written by Jim Adams on 05/01/2005 for Capella University * */ import java.awt.BorderLayout; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.GridLayout; import java.awt.Insets; import java.awt.Point; import java.applet.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Calendar; import java.util.Date; import javax.swing.*; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingConstants; import javax.swing.UIManager; public class DateChooserApplet extends JApplet { int width, height; // test final int MAX=42; private JFrame frame = new JFrame(); private JPanel p0 = new JPanel(); // main panel private JPanel p1 = new JPanel(); // Date panel private JPanel p2 = new JPanel(); // Arrow panel private JPanel p3 = new JPanel(); // Cancel/Select Button Panel private GridLayout grid1 = new GridLayout(7,7,1,1); // day buttons private GridLayout grid2 = new GridLayout(1,2,2,2); // arrow buttons //private GridLayout grid3 = new GridLayout(1,4,2,2); // cancel/select buttons private BorderLayout border0 = new BorderLayout(4,4); // main panel private Point location = new Point(); private JButton cancelbutton = new JButton("Cancel"); private JButton selectbutton = new JButton("Select"); private JDialog d = new JDialog(frame, null, true); private JButton[] daybutton = new JButton[MAX]; private JButton[] headbutt = new JButton[7]; private Font verdana8bold = new Font("Verdana", Font.BOLD, 8); private Font verdana9plain = new Font("Verdana", Font.PLAIN, 9); private Font verdana9bold = new Font("Verdana", Font.BOLD, 9); private Font verdana10plain = new Font("Verdana", Font.PLAIN, 10); private Font verdana10bold = new Font("Verdana", Font.BOLD, 10); private Font verdana11bold = new Font("Verdana", Font.BOLD, 11); private Font courier10bold = new Font("Courier New", Font.BOLD, 10); private Font courier10plain = new Font("Courier New", Font.PLAIN, 10); private Font courier11plain = new Font("Courier New", Font.PLAIN, 11); private Color red = new Color(255,0,0); private Color blue = new Color(0,0,255); private Color lightblue = new Color(144,144,255); private Color lightgrey = new Color(240,248,240); private Color white = new Color(255,255,255); private Color black = new Color(0,0,0); private String selected_month =""; private String selected_day =""; private String selected_year =""; private Date global_date = new Date(); public void init() { set_look_and_feel(); this.location.x = 400; this.location.y = 400; display(); } public void paint( Graphics g ) { set_look_and_feel(); //display(); } /** Display the Calendar @date 04/28/2005 @author Jim Adams @param None @return Nothing */ public void display() { String[] headers = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; // Get current date Calendar today = Calendar.getInstance(); int curmonth = today.get(Calendar.MONTH); int curday = today.get(Calendar.DAY_OF_MONTH); int curyear = today.get(Calendar.YEAR); this.selected_month = Integer.toString(curmonth+1); this.selected_day = Integer.toString(curday); this.selected_year = Integer.toString(curyear); set_title(); p0.setLayout(border0); p1.setLayout(grid1); // add header row Insets insets = new Insets(1,0,2,0); // top, left, bot, right int j=0; for (j=0; j<7; j++) { headbutt[j] = new JButton(); headbutt[j].setText(headers[j]); headbutt[j].setFont(verdana9bold); headbutt[j].setMargin(insets); p1.add(headbutt[j]); } // aded data rows for (j=0; j>"); leftArrow.addActionListener(new ButtonListener()); rightArrow.addActionListener(new ButtonListener()); rightArrow.setBackground(white); leftArrow.setBackground(white); rightArrow.setFont(verdana9plain); leftArrow.setFont(verdana9plain); //rightArrow.setIcon(rightIcon); //leftArrow.setIcon(leftIcon); rightArrow.setHorizontalTextPosition(SwingConstants.LEFT); p2.setLayout(grid2); p2.add(leftArrow); p2.add(rightArrow); cancelbutton.setFont(verdana9plain); selectbutton.setFont(verdana9plain); cancelbutton.addActionListener(new CancelListener()); selectbutton.addActionListener(new SelectListener()); selectbutton.setFocusable(true); selectbutton.setEnabled(true); selectbutton.grabFocus(); p3.add(cancelbutton); p3.add(selectbutton); p0.add(p2, BorderLayout.NORTH); p0.add(p1, BorderLayout.CENTER); p0.add(p3, BorderLayout.SOUTH); set_title(); d.setLocation(location); // x, y start location d.setSize(300,250); d.getContentPane().add( p0 ); d.setResizable(false); d.setVisible(true); d.pack(); } // end of method /** Set all the calendar buttons to 1 through the highest day of the month @date 04/28/2005 @author Jim Adams @param Calendar Object @return Nothong. Sets Global JButton Array */ private void set_calendar_buttons(Calendar cal) { Calendar today = Calendar.getInstance(); int todays_date = today.get(Calendar.DAY_OF_MONTH); int todays_month = today.get(Calendar.MONTH); cal.set(Calendar.DAY_OF_MONTH, 1); int startday = cal.get(Calendar.DAY_OF_WEEK); int days_in_month = cal.getActualMaximum(Calendar.DAY_OF_MONTH); // day in month // clear the buttons, first for (int j=0; j 12) { y++; m=1; } } selected_month = String.valueOf(m); selected_day = String.valueOf(d); selected_year = String.valueOf(y); Calendar newday = Calendar.getInstance(); newday.set(Calendar.DAY_OF_MONTH, d); newday.set(Calendar.MONTH, m-1); newday.set(Calendar.YEAR, y); set_calendar_buttons(newday); set_title(); } } // end of method /** Action Listener for The Cancel Button @date 04/28/2005 @param None @author Jim Adams */ private class CancelListener implements ActionListener { public void actionPerformed( ActionEvent e ) { frame.setVisible(false); } } // end of method /** Action Listener for The Calendar Select Button Sets some global date variables @date 04/28/2005 @param None @author Jim Adams */ private class SelectListener implements ActionListener { public void actionPerformed( ActionEvent e ) { global_date.setYear(Integer.parseInt(selected_year)-1900); global_date.setMonth(Integer.parseInt(selected_month)-1); global_date.setDate(Integer.parseInt(selected_day)); frame.setVisible(false); } } // end of method }