Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!news-out.readnews.com!transit4.readnews.com!news-out.news.tds.net!newsreading01.news.tds.net!53ab2750!not-for-mail From: "Knute Johnson" Subject: Re: Code To Pick A Date From A Calendar Message-ID: <501C1567.56040.calajapr@time.synchro.net> X-Comment-To: clusardi2k Newsgroups: comp.lang.java.programmer In-Reply-To: <501C1560.56001.calajapr@time.synchro.net> References: <501C1560.56001.calajapr@time.synchro.net> X-FTN-AREA: COMP.LANG.JAVA.PROGRAMMER X-FTN-MSGID: 1:261/38 6d3a424b X-FTN-REPLY: 1:261/38 f07968d1 Content-Type: text/plain; charset=IBM437 Content-Transfer-Encoding: 8bit X-Gateway: time.synchro.net [Synchronet 3.16a-Win32 NewsLink 1.98] Lines: 326 Date: Fri, 03 Aug 2012 18:54:19 GMT NNTP-Posting-Host: 69.21.70.65 X-Complaints-To: news@tds.net X-Trace: newsreading01.news.tds.net 1344020059 69.21.70.65 (Fri, 03 Aug 2012 13:54:19 CDT) NNTP-Posting-Date: Fri, 03 Aug 2012 13:54:19 CDT Organization: tds.net Xref: csiph.com comp.lang.java.programmer:17081 To: clusardi2k From: Knute Johnson On 8/2/2012 11:30 AM, clusardi2k@aol.com wrote: > Does anyone have a link on the Internet to a project that shows how > to build and display a calendar, and lets the user pick a date. > > Thank you, > Here is one I started some years ago. It is not complete but it would give you an idea how I was thinking of doing one at the time. I don't really know the state of the code as I haven't played with it in some time. // // // // JDateChooser // // Written by: Knute Johnson // // Date Version Modification // --------- ------- --------------------------------------------------- // 04 jun 05 01.00 incept // // // Constructor Summary // JDateChooser() // Creates a new JDateChooser with today's date displayed // JDateChooser(GregorianCalendar gc) // Creates a new JDateChooser with the specified date displayed // // Method Summary // GregorianCalendar getCalendar() // Gets the selected date // void setCalendar(GregorianCalendar gc) // Sets and display's the specified date // static GregorianCalendar showDialog(Component comp) // Displays a JDateChooser in a modal JDialog and returns the // selected date or null if dismissed. // static GregorianCalendar showDialog(Component comp,GregorianCalendar gc) // Displays a JDateChooser in a modal JDialog with the specified date // and returns the selected date or null if dismissed. // // // package com.knutejohnson.components; import java.awt.*; import java.awt.event.*; import java.text.*; import java.util.*; import javax.swing.*; public class JDateChooser extends JComponent { /* private static final String[] dayStr = {"SUN","MON","TUE","WED","THU","FRI","SAT"}; */ private final String[] dayStr; private final String[] monthStr; /* private static final String[] monthStr = {"JANUARY ","FEBRUARY ","MARCH ","APRIL ","MAY ","JUNE ","JULY ", "AUGUST ","SEPTEMBER ","OCTOBER ","NOVEMBER ","DECEMBER "}; */ private GregorianCalendar gc; private int thisYear,thisMonth,today; private int selectedDay; private JButton previousButton,nextButton; private JLabel[] dayOfWeekLabels = new JLabel[7]; private JLabel[] dayOfMonthLabels = new JLabel[42]; private JLabel monthYearLabel; private static JDialog dialog; private static GregorianCalendar retcod; private final Locale locale; public JDateChooser() { this(new GregorianCalendar(),Locale.getDefault()); } public JDateChooser(GregorianCalendar calendar, Locale locale) { gc = calendar; thisYear = gc.get(Calendar.YEAR); thisMonth = gc.get(Calendar.MONTH); today = selectedDay = gc.get(Calendar.DAY_OF_MONTH); this.locale = locale; DateFormatSymbols dfs = DateFormatSymbols.getInstance(locale); monthStr = dfs.getMonths(); setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); c.gridx = c.gridy = 0; c.insets = new Insets(2,2,2,2); c.weightx = 1.0; c.anchor = GridBagConstraints.WEST; previousButton = new JButton("<"); previousButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { gc.add(Calendar.MONTH,-1); int mon = gc.get(Calendar.MONTH); if (selectedDay > gc.getActualMaximum(Calendar.DAY_OF_MONTH)) selectedDay = 1; drawCalendar(); } }); add(previousButton,c); ++c.gridx; c.anchor = GridBagConstraints.CENTER; monthYearLabel = new JLabel(" ",JLabel.CENTER); add(monthYearLabel,c); ++c.gridx; c.anchor = GridBagConstraints.EAST; nextButton = new JButton(">"); nextButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { gc.add(Calendar.MONTH,1); int mon = gc.get(Calendar.MONTH); if (selectedDay > gc.getActualMaximum(Calendar.DAY_OF_MONTH)) selectedDay = 1; drawCalendar(); } }); add(nextButton,c); MouseListener ml = new MouseAdapter() { public void mouseClicked(MouseEvent me) { JLabel dayLabel = (JLabel)me.getSource(); String str = dayLabel.getText(); try { int num = Integer.parseInt(str); gc.set(Calendar.DAY_OF_MONTH,num); selectedDay = num; drawCalendar(); } catch (NumberFormatException nfe) { } } }; c.gridx = 0; ++c.gridy; c.gridwidth = 3; c.anchor = GridBagConstraints.CENTER; JPanel panel = new JPanel(new GridLayout(7,7,1,1)); /* for (int i=0; i= (firstDayOfMonth - 1) && i<(gc.getActualMaximum(Calendar.DAY_OF_MONTH)+firstDayOfMonth-1)) { dayOfMonthLabels[i].setText(Integer.toString(day)); if (day == today && month == thisMonth && year == thisYear) dayOfMonthLabels[i].setForeground(Color.RED); else dayOfMonthLabels[i].setForeground(Color.BLACK); if (day == selectedDay) dayOfMonthLabels[i].setBackground(new Color(0xa0a0a0)); else dayOfMonthLabels[i].setBackground(Color.WHITE); ++day; } else { dayOfMonthLabels[i].setText(" "); dayOfMonthLabels[i].setBackground(new Color(0xe0e0e0)); } } } public GregorianCalendar getCalendar() { return new GregorianCalendar(gc.get(Calendar.YEAR), gc.get(Calendar.MONTH),selectedDay); } public void setCalendar(GregorianCalendar calendar) { gc = calendar; drawCalendar(); } public static GregorianCalendar showDialog(Component comp) { return showDialog(comp, new GregorianCalendar(),Locale.getDefault()); } public static GregorianCalendar showDialog(Component comp, GregorianCalendar calendar,Locale locale) { GridBagConstraints c = new GridBagConstraints(); c.gridx = c.gridy = 0; c.insets = new Insets(2,2,2,2); c.gridwidth = 2; JFrame f = new JFrame(); dialog = new JDialog(f,"Select Date",true); dialog.setLayout(new GridBagLayout()); dialog.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { retcod = null; } }); final JDateChooser dc = new JDateChooser(calendar,locale); dialog.add(dc,c); ++c.gridy; c.gridwidth = 1; c.anchor = GridBagConstraints.WEST; JButton okButton = new JButton(" OK "); okButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { retcod = dc.getCalendar(); dialog.dispose(); } }); dialog.add(okButton,c); ++c.gridx; c.anchor = GridBagConstraints.EAST; JButton cancelButton = new JButton("Cancel"); cancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { retcod = null; dialog.dispose(); } }); dialog.add(cancelButton,c); dialog.pack(); dialog.setLocationRelativeTo(comp); dialog.setVisible(true); return retcod; } public void setFont(Font font) { previousButton.setFont(font); nextButton.setFont(font); for (int i=0; i