Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JDateChooser displaying invalid month and Date in the textBox

843805Jun 17 2006 — edited Jun 17 2006
let me paste the code below......
Problems....
1). whenever I choose the date, initially it will display the present date but on second click it displays the month as January .... and on select of any date, it shows a different date in the textBox I am using jcalendar-1.3.2.jar

A simple demo program which I tried is as below....
the JDateChooser Constructor...
txtDateChooser = new JDateChooser("dd/mm/yyyy","##/##/####", '_');

Java Demo Application.....

import com.toedter.calendar.JDateChooser;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
*
* @author pcasanova
*/
public class DateTest extends javax.swing.JFrame
{
private javax.swing.JButton btnGetDate;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField txtDate;
private com.toedter.calendar.JDateChooser txtDateChooser;

/**
* Creates new form DateTest
*/
public DateTest()
{
initComponents();
}

private void initComponents()
{
jPanel1 = new javax.swing.JPanel();
txtDateChooser = new JDateChooser("dd/mm/yyyy","##/##/####", '_');
btnGetDate = new javax.swing.JButton();
txtDate = new javax.swing.JTextField();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setLayout(new java.awt.BorderLayout());

jPanel1.setMaximumSize(new java.awt.Dimension(90, 63));
jPanel1.setMinimumSize(new java.awt.Dimension(90, 63));
jPanel1.setPreferredSize(new java.awt.Dimension(90, 63));
jPanel1.add(txtDateChooser, java.awt.BorderLayout.NORTH);

btnGetDate.setText("Get Date");
btnGetDate.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
btnGetDateActionPerformed(evt);
}
});

jPanel1.add(btnGetDate, java.awt.BorderLayout.CENTER);

jPanel1.add(txtDate, java.awt.BorderLayout.SOUTH);

getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

pack();
}

private void btnGetDateActionPerformed(java.awt.event.ActionEvent evt)
{
if (this.txtDateChooser.getDate() != null)
{
System.out.println(""+this.txtDateChooser.getDate());
SimpleDateFormat format = new SimpleDateFormat(this.txtDateChooser.getDateFormatString());

this.txtDate.setText(format.format(this.txtDateChooser.getDate()));
}
}

/**
* @param args the command line arguments
*/
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
new DateTest().setVisible(true);
}
});
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 15 2006
Added on Jun 17 2006
1 comment
904 views