Code To Pick A Date From A Calendar

Discussion in 'Java' started by clusardi2k@aol.com, Aug 2, 2012.

  1. Guest

    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,
     
    , Aug 2, 2012
    #1
    1. Advertising

  2. Eric Sosman Guest

    On 8/2/2012 2:30 PM, 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.


    http://migcalendar.com/

    > Thank you,


    You're most welcome.

    --
    Eric Sosman
    d
     
    Eric Sosman, Aug 2, 2012
    #2
    1. Advertising

  3. Guest

    On Thursday, August 2, 2012 2:59:28 PM UTC-4, Eric Sosman wrote:
    > On 8/2/2012 2:30 PM, 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. http://migcalendar.com/ > Thank you, You're most welcome. -- Eric Sosman d


    That's too expensive for me! What about something free without any license?

    Thank you,
     
    , Aug 2, 2012
    #3
  4. On 8/2/2012 11:30 AM, 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<dayStr.length; i++) {
    dayOfWeekLabels = new JLabel(dayStr,JLabel.CENTER);
    panel.add(dayOfWeekLabels);
    }
    */

    dayStr = dfs.getShortWeekdays();
    int firstDay = gc.getFirstDayOfWeek();
    for (int i=0; i<7; i++) {
    int x = firstDay + i;
    if (i == 6)
    x = firstDay == Calendar.MONDAY ? Calendar.SUNDAY :
    Calendar.SATURDAY;
    dayOfWeekLabels =
    new JLabel(dayStr[x].toUpperCase(),JLabel.CENTER);
    panel.add(dayOfWeekLabels);
    }

    for (int i=0; i<dayOfMonthLabels.length; i++) {
    dayOfMonthLabels = new JLabel(" ",JLabel.CENTER);
    dayOfMonthLabels.setOpaque(true);
    dayOfMonthLabels.addMouseListener(ml);
    panel.add(dayOfMonthLabels);
    }

    drawCalendar();

    add(panel,c);
    }

    private void drawCalendar() {
    int month = gc.get(Calendar.MONTH);
    int year = gc.get(Calendar.YEAR);

    monthYearLabel.setText(monthStr[month].toUpperCase() + " " +
    Integer.toString(year));

    gc.set(Calendar.DAY_OF_MONTH,1);
    int firstDayOfMonth = gc.get(Calendar.DAY_OF_WEEK);
    if (gc.getFirstDayOfWeek() == Calendar.MONDAY)
    --firstDayOfMonth;
    gc.set(Calendar.DAY_OF_MONTH,selectedDay);

    int day = 1;
    for (int i=0; i<42; i++) {
    if (i >= (firstDayOfMonth - 1) &&

    i<(gc.getActualMaximum(Calendar.DAY_OF_MONTH)+firstDayOfMonth-1)) {
    dayOfMonthLabels.setText(Integer.toString(day));
    if (day == today && month == thisMonth && year == thisYear)
    dayOfMonthLabels.setForeground(Color.RED);
    else
    dayOfMonthLabels.setForeground(Color.BLACK);
    if (day == selectedDay)
    dayOfMonthLabels.setBackground(new Color(0xa0a0a0));
    else
    dayOfMonthLabels.setBackground(Color.WHITE);
    ++day;
    } else {
    dayOfMonthLabels.setText(" ");
    dayOfMonthLabels.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<dayOfWeekLabels.length; i++)
    dayOfWeekLabels.setFont(font);
    for (int i=0; i<dayOfMonthLabels.length; i++)
    dayOfMonthLabels.setFont(font);
    monthYearLabel.setFont(font);
    }

    public static void main(String[] args) {
    Runnable r = new Runnable() {
    public void run() {
    final JDateChooser dc = new JDateChooser();
    final JFrame f = new JFrame();
    f.setLayout(new FlowLayout());
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(dc);
    final JButton b = new JButton("DateChooser");
    b.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
    System.out.println(showDialog(b,new
    GregorianCalendar(Locale.FRANCE),Locale.FRANCE));
    }
    });
    f.add(b);
    f.pack();
    f.setVisible(true);
    }
    };
    EventQueue.invokeLater(r);
    }
    }




    --

    Knute Johnson
     
    Knute Johnson, Aug 3, 2012
    #4
  5. Roedy Green Guest

    On Thu, 2 Aug 2012 11:30:43 -0700 (PDT), wrote,
    quoted or indirectly quoted someone who said :

    >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.


    see http://mindprod.com/jgloss/date.html
    there are links to two of them at the bottom.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    The greatest shortcoming of the human race is our inability to understand the exponential function.
    ~ Dr. Albert A. Bartlett (born: 1923-03-21 age: 89)
    http://www.youtube.com/watch?v=F-QA2rkpBSY
     
    Roedy Green, Aug 3, 2012
    #5
  6. Guest

    > On Thursday, August 2, 2012 11:09:13 PM UTC-4, Knute Johnson wrote:

    > 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.


    Does the code you posted have a copyright.

    Thanks,
     
    , Aug 3, 2012
    #6
  7. On 8/3/2012 5:58 AM, wrote:
    >> On Thursday, August 2, 2012 11:09:13 PM UTC-4, Knute Johnson wrote:

    >
    >> 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.

    >
    > Does the code you posted have a copyright.
    >
    > Thanks,
    >


    Feel free to steal this code. If you can make any money with it, more
    power to you.

    --

    Knute Johnson
     
    Knute Johnson, Aug 4, 2012
    #7
  8. Arne Vajhøj Guest

    On 8/3/2012 8:58 AM, wrote:
    > Does the code you posted have a copyright.


    It is expected that code posted to usenet is free for
    everybody to use for whatever. Otherwise there were
    not really any point in posting it.

    A lawyer would probably fell down from the chair
    hearing about such "expectations", but ...

    Arne
     
    Arne Vajhøj, Aug 4, 2012
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric Sosman
    Replies:
    2
    Views:
    136
    Steve W. Jackson
    Aug 3, 2012
  2. clusardi2k

    Code To Pick A Date From A Calendar

    clusardi2k, Aug 3, 2012, in forum: Java
    Replies:
    19
    Views:
    487
    Arne Vajhøj
    Aug 13, 2012
  3. clusardi2k

    Code To Pick A Date From A Calendar

    clusardi2k, Aug 4, 2012, in forum: Java
    Replies:
    3
    Views:
    122
    Steve W. Jackson
    Aug 4, 2012
  4. Eric Sosman
    Replies:
    2
    Views:
    159
    Steve W. Jackson
    Aug 4, 2012
  5. clusardi2k

    Code To Pick A Date From A Calendar

    clusardi2k, Aug 4, 2012, in forum: Java
    Replies:
    0
    Views:
    155
    clusardi2k
    Aug 4, 2012
Loading...

Share This Page