Using JCalendar

JCalendar is a Java component, bean for graphically picking a date, you can download for their projects from here and see the API documentation.

Once JCalendar added to the classpath of the projec I can use it in an application, where the user must fill a text field within a format date, but the user should not directly edit this field if he doesn't select the date by pressing a button to load a pop pp window, which will see a calendar and a button to accept (which, when pressed, saving the date and subsequently chosen by the methods we can get that provides the class (the next one: CDatePicker which extends from a JDialog ), then close the window).


package datepicker;

public class CDatePicker extends javax.swing.JDialog
{
/* the JCalendar var */
private com.toedter.calendar.JCalendar jCal;

private javax.swing.JPanel pnlDatePicker;
private javax.swing.JButton btnAccept;

private String year;
private String month;
private String day;

public CDatePicker(java.awt.Frame parent)
{
super(parent, true);

initComponents();
initExtra();
}

public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
CDatePicker dialog = new CDatePicker(new javax.swing.JFrame());

dialog.addWindowListener(new java.awt.event.WindowAdapter()
{
public void windowClosing(java.awt.event.WindowEvent e)
{
System.exit(0);
}
});

dialog.setVisible(true);
}
});
}

/* default values */
private void initComponents()
{
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setSize(290,275);
setModal(true);
setTitle("Calendar");
setResizable(false);
setLayout(null);

setLocationRelativeTo(null);

pnlDatePicker = new javax.swing.JPanel();
pnlDatePicker.setLayout(null);
pnlDatePicker.setBorder(javax.swing.BorderFactory.createEtchedBorder());
pnlDatePicker.setBounds(2, 2, 278, 250);
add(pnlDatePicker);

/* set calendar properties */
jCal = new com.toedter.calendar.JCalendar();
jCal.setWeekOfYearVisible(false);
jCal.setBounds(3, 2, 271, 204);
pnlDatePicker.add(jCal);

btnAccept = new javax.swing.JButton();
btnAccept.setText("Aceptar");
btnAccept.setMnemonic('A');
btnAccept.setBounds(3, 206, 271, 32);
pnlDatePicker.add(btnAccept);

/* links event with the button */
btnAccept.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent ev)
{
btnAccept_Click();
}
});
}

private void initExtra()
{
year = new String("");
month = new String("");
day = new String("");
}

/* getters */
public String getYear()
{
return year;
}

public String getMonth()
{
return month;
}

public String getDay()
{
return day;
}

/* button event */
public void btnAccept_Click()
{
/* get selected values from the calendar */
year = Integer.toString(jCal.getCalendar().get(java.util.Calendar.YEAR));
month = Integer.toString(jCal.getCalendar().get(java.util.Calendar.MONTH) + 1);
day = Integer.toString(jCal.getCalendar().get(java.util.Calendar.DATE));

/* close window on click */
dispose();
}
}

Already in the form(who uses the class discussed above).

The button to load the window that contains the calendar bean and get the value selected by user.

When the user closes the windows, sets the form's text field if selected a day from the picker.


...
/* button event */
private void btnShowCalendar_ActionPerformed(java.awt.event.ActionEvent evt)
{
/* create an datepicker instance */
CDatePicker dlgDate = new CDatePicker(new javax.swing.JFrame());
dlgDate.setVisible(true);

if (dlgDate.getDay().compareTo("") != 0)
{
txtDateField.setText
(
dlgDate.getYear() + "/" + dlgDate.getMonth() + "/" + dlgDate.getDay()
);
}
}
...

Attachment Size
27.89 KB