This is my infernal problem. nowadays i'm trying to create my project according to my Degree. I already have added jcalender to my project in netbeans, and i already added jDateChooser to my jFrame. my problem is, when i chooseing a date from jDateChooser how will display this date on a jLabel. i tried to using jLabel1.setText(jDateChooser1);
but in this case error will occur.
https://i.stack.imgur.com/FXeYO.jpg
Asked
Active
Viewed 4.5k times
3

mKorbel
- 109,525
- 20
- 134
- 319

majesticProgrammer
- 33
- 1
- 1
- 4
-
What is `jDateChooser1`? Please, post the code related to the problem. – Christian Tapia Jan 09 '14 at 06:07
-
@Christian `JDateChooser` is a date picker from the [`JCalendar`](http://www.toedter.com/en/jcalendar/) library – MadProgrammer Jan 09 '14 at 06:16
5 Answers
9
First, you need to get the date from the component, something like...
Date date = jDateChooser1.getDate();
Next you need to format that Date
value to a String
String strDate = DateFormat.getDateInstance().format(date);
Finally, you need to set that value as the text for the lable...
jLabel1.setText(strDate);
If you have particular formatting requirements, you may need to look at SimpleDateFormat

MadProgrammer
- 343,457
- 22
- 230
- 366
-
Date date = jDateChooser1.getDate(); line says "illegal forward reference". what to do? – majesticProgrammer Jan 09 '14 at 07:39
-
It's possible that `jDateChooser1` has not yet being defined, as described [here](http://stackoverflow.com/questions/1746758/illegal-forward-reference-in-java) – MadProgrammer Jan 09 '14 at 07:40
-
5
String date = ((JTextField)jDateChooser1.getDateEditor().getUiComponent()).getText();
jLabel1.setText(date);

Indunil Girihagama
- 415
- 1
- 7
- 11
4
import this file>>
import java.text.SimpleDateFormat;
Try this code >>
SimpleDateFormat dcn = new SimpleDateFormat("yyyy-MM-dd");
String date = dcn.format(jDateChooser1.getDate() );
jLabel1.setText(date.toString());

DASUN CHINTHAKA
- 41
- 4
0
Try this,
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date = sdf.format(jDateChooser1.getSelectedDate().getTime());
jLabel1.setText(date);

Anuruddha Lanka Liyanarachchi
- 2,056
- 2
- 25
- 34
0
Try this:
String date1 = ((JTextField) jDateChooser4.getDateEditor().getUiComponent()).getText() + "";
jLabel1.setText(date1);

Blauharley
- 4,186
- 6
- 28
- 47

Shinwar ismail
- 299
- 2
- 9
-
Please edit to add a small explanation of what this code does. – Thomas Smyth - Treliant Dec 21 '17 at 15:35