Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / August 2006

Tip: Looking for answers? Try searching our database.

open source/free multilingual date picking calendar?

Thread view: 
timasmith@hotmail.com - 29 Jul 2006 16:12 GMT
Hi,

I have looked at Nachocalendar but are there any other open source
Swing controls for picking date(time) via a Calendar and/or typing in
the text field?

The Calendar must be able to display in all languages and obviously
support multiple formats.

thanks!

Tim
Roland de Ruiter - 29 Jul 2006 18:54 GMT
> Hi,
>
[quoted text clipped - 8 lines]
>
> Tim

<https://jcalendar.dev.java.net/> maybe?
Signature

Regards,

Roland

Knute Johnson - 29 Jul 2006 22:19 GMT
> Hi,
>
[quoted text clipped - 8 lines]
>
> Tim

Below is one I wrote.  It needs work to use any locale but if you want a
platform to start with it should work.  If you make any useful additions
to it, please send me a copy.

//
//
//
//  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.util.*;
import javax.swing.*;

public class JDateChooser extends JComponent {
    private static final String[] dayStr =
     {"SUN","MON","TUE","WED","THU","FRI","SAT"};
    private static final String[] monthStr =
     {"JANUARY ","FEBRUARY ","MARCH ","APRIL ","MAY ","JUNE ","JULY ",
      "AUGUST ","SEPTEMBER ","OCTOBER ","NOVEMBER ","DECEMBER "};
    private int[] daysInMonth = {31,28,31,30,31,30,31,31,30,31,30,31};

    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;

    public JDateChooser() {
        this(new GregorianCalendar());
    }

    public JDateChooser(GregorianCalendar calendar) {
        gc = calendar;
        thisYear = gc.get(Calendar.YEAR);
        thisMonth = gc.get(Calendar.MONTH);
        today = selectedDay = gc.get(Calendar.DAY_OF_MONTH);

        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 > daysInMonth[mon])
                    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 > daysInMonth[mon])
                    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[i] = new JLabel(dayStr[i],JLabel.CENTER);
            panel.add(dayOfWeekLabels[i]);
        }
        for (int i=0; i<dayOfMonthLabels.length; i++) {
            dayOfMonthLabels[i] = new JLabel("  ",JLabel.CENTER);
            dayOfMonthLabels[i].setOpaque(true);
            dayOfMonthLabels[i].addMouseListener(ml);
            panel.add(dayOfMonthLabels[i]);
        }

        drawCalendar();

        add(panel,c);
    }

    private void drawCalendar() {
        int month = gc.get(Calendar.MONTH);
        int year = gc.get(Calendar.YEAR);
        if (gc.isLeapYear(year))
            daysInMonth[1] = 29;
        else
            daysInMonth[1] = 28;

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

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

        int day = 1;
        for (int i=0; i<42; i++) {
            if (i >= (firstDayOfMonth - 1) &&
             i < (daysInMonth[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());
    }

    public static GregorianCalendar showDialog(Component comp,
     GregorianCalendar calendar) {
        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);
        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[i].setFont(font);
        for (int i=0; i<dayOfMonthLabels.length; i++)
            dayOfMonthLabels[i].setFont(font);
        monthYearLabel.setFont(font);
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            public void run() {
                JFrame.setDefaultLookAndFeelDecorated(true);
                JDialog.setDefaultLookAndFeelDecorated(true);
                System.setProperty("swing.aatext","true");
                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));
                    }
                });
                f.add(b);
                f.pack();
                f.setVisible(true);
            }
        };
        EventQueue.invokeLater(r);
    }
}

Signature

Knute Johnson
email s/nospam/knute/

TheDude - 30 Jul 2006 14:07 GMT
> Hi,
>
[quoted text clipped - 4 lines]
> The Calendar must be able to display in all languages and obviously
> support multiple formats.

You can also check out JXDatePicker from SwingLabs library.
Morten Alver - 31 Jul 2006 08:34 GMT
> Hi,
>
[quoted text clipped - 4 lines]
> The Calendar must be able to display in all languages and obviously
> support multiple formats.

I don't know if this one fulfills your requirements, but:

http://microba.sourceforge.net/

Signature

Morten

timasmith@hotmail.com - 01 Aug 2006 11:17 GMT
> > Hi,
> >
[quoted text clipped - 11 lines]
> --
> Morten

wow, that calendar rocks - I will add a time picker but the datepicking
is excellent thanks


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.