unable to add JDateChooser component to JFrame

5.1k views Asked by At

i am trying to use JDateChooser in my JFrame.(code not Beans IDE).How do i make an istance of JDateChooser class and add it to JFrame.

import com.toedter.calendar.JDateChooser;
import com.toedter.calendar.JDateChooserBeanInfo;
import com.toedter.calendar.JDateChooserCellEditor;
import javax.swing.*;
import java.awt.*;
class main extends JFrame
{
JDateChooser cdate;

main()
{
cdate = new JDateChooser();
add(cdate);
setSize(400,200);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
1

There are 1 answers

2
Saj On
JDateChooser dateChooser = new JDateChooser();
dateChooser.setBounds(20, 20, 200, 30); // Modify depending on your preference
jFrame.getContentPane().add(dateChooser);

To get the date, you can do-

Date date = dateChooser.getDate();

Edit: Based from comment-

com.toedter.calendar.JDateChooser dateChooser = new com.toedter.calendar.JDateChooser();
dateChooser.setDateFormatString("MM/dd/yyyy");

Date date =  dateChooser.getDate();
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
someJLabel.setText(dateFormat.format(date));