Selecting Date of Birth using JCalendar in Java

20.4k views Asked by At

I am developing a java desktop app. I wanna implement Date of Birth selecting using JCalendar. I have a JLabel and this jCalendar is added to JLabel .And set position using setBounds().But the calendar is overlapping with other components and i can't select the date so what should i do?enter image description here

Here is my code snippet

    JCalendar dob=new JCalendar();

    raillabel. add(dob);

    dob.setBounds(250, 186, 320, 330);
2

There are 2 answers

0
dic19 On BEST ANSWER

First off, why are you trying to add JCalendar to a JLabel? You can easily add this to a JPanel:

JPanel panel = new JPanel();
panel.add(new JCalendar());

If you still want to add this JCalendar to a JLabel then you need to provide a LayoutManager to this last one in order to properly add components to it:

JCalendar calendar = new JCalendar();        
JLabel label = new JLabel("Select date of birth:");
label.setLayout(new BorderLayout());
label.add(calendar, BorderLayout.EAST);

Take a look to A Visual Guide to Layout Managers.

As @mKorbel says in his comment you shouldn't use NullLayout and setBounds() method to set the component location/size. This is the task layout managers are intended for.

Finally:

I wanna implement Date of Birth selecting using JCalendar.

You may want to try JDateChooser instead, which allows selecting a date or type it by hand:

JDateChooser chooser = new JDateChooser();
chooser.setLocale(Locale.US);

JPanel panel = new JPanel();
panel.add(new JLabel("Date of Birth:"));
panel.add(chooser);

Picture

enter image description here

1
David On

Using swing, just place the jDateChooser control on your form and it will be just fine