I am creating a replica of a program written in VB.NET in JAVA as a practice. I'm having an annoying problem and I haven't been able to exactly identify why it is caused... you see, I'm using some libraries downloaded from the following source:
In VB.NET there is a component called DateTimePicker that does not exist in JAVA, and that is why I had to download libraries from the internet to obtain it. I'm using the JDateChooser component which is very similar to the one in VB.NET... the only difference is that when choosing a date from it, the component prints that date in the textfield with a short date format (10/24/2016) while that in the other original program, it printed it in a long date (Monday, October 24, 2016).
To get around this problem, I wrote a little function in my class so that we can say, "convert" or "translate" (whatever you want to call it) that format from a short date to a long date:
public String getFechaLarga(String fecha) throws ParseException{
Calendar c = Calendar.getInstance();
Date date = new SimpleDateFormat("MM/dd/yyyy").parse(fecha);
c.setTime(date);
String hoy=null;
switch(c.get(Calendar.DAY_OF_WEEK)){
case 1: hoy="Domingo,";break;
case 2: hoy="Lunes, ";break;
case 3: hoy="Martes, ";break;
case 4: hoy="Miércoles, ";break;
case 5: hoy="Jueves, ";break;
case 6: hoy="Viernes, ";break;
case 7: hoy="Sábado, ";break;
}
hoy = hoy + c.get(Calendar.DAY_OF_MONTH);
switch(c.get(Calendar.MONTH)){
case 0: hoy=hoy + " de enero";break;
case 1: hoy=hoy + " de febrero";break;
case 2: hoy=hoy + " de marzo";break;
case 3: hoy=hoy + " de abril";break;
case 4: hoy=hoy + " de mayo";break;
case 5: hoy=hoy + " de junio";break;
case 6: hoy=hoy + " de julio";break;
case 7: hoy=hoy + " de agosto";break;
case 8: hoy=hoy + " de septiembre";break;
case 9: hoy=hoy + " de octubre";break;
case 10: hoy=hoy + " de noviembre";break;
case 11: hoy=hoy + " de diciembre";break;
}
hoy = hoy + " de " + c.get(Calendar.YEAR);
return hoy;
}
The function works correctly (well, "almost" I think) as you can see:
Now come the problems... and why do I put the function too? Well, I don't know exactly if this is what is giving me the exception or not. At the beginning, it gave me the error "Unparseable Date", and it was because the program tried to convert the string already converted from the TextField of the date to Date (as if it took the short date, converted it to a long date and took the long date again to convert... but of course, it would give an error because it was already converted). What I did was put a condition on it to regulate it (if the TextField string is less than or equal to 10 characters, then it proceeds).
((JTextFieldDateEditor)calendario.getDateEditor()).addPropertyChangeListener(
new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent pce) {
try {
if(((JTextFieldDateEditor)calendario.getDateEditor()).getText().length() <= 10){
((JTextFieldDateEditor)calendario.getDateEditor()).setText(admin.getFechaLarga(((JTextFieldDateEditor)calendario.getDateEditor()).getText()));
}
} catch (ParseException ex) {
Logger.getLogger(NuevaEntrada.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
Everything seemed to work correctly, but when I changed to another date, another error was thrown at me... and this time it complained that the JTextField of the date was "empty". I inserted another condition to the if... (If the number of characters in the TextField string is not equal to 0, then it proceeds) This is how the propertyChange event is left at the end:
((JTextFieldDateEditor)calendario.getDateEditor()).addPropertyChangeListener(
new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent pce) {
try {
if(((JTextFieldDateEditor)calendario.getDateEditor()).getText().length() <= 10 &&
((JTextFieldDateEditor)calendario.getDateEditor()).getText().length() != 0){
((JTextFieldDateEditor)calendario.getDateEditor()).setText(admin.getFechaLarga(((JTextFieldDateEditor)calendario.getDateEditor()).getText()));
}
} catch (ParseException ex) {
Logger.getLogger(NuevaEntrada.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
Now yes, it's not supposed to throw any more exceptions I think, but it throws one last exception... which is the following:
The line of code where it marks the error, is what is inside the if (when I use that function I wrote, that's why I'm thinking that the error may be there):
((JTextFieldDateEditor)calendario.getDateEditor()).setText(admin.getFechaLarga(((JTextFieldDateEditor)calendario.getDateEditor()).getText()));
It could also be that I'm not using the correct event to use my function... maybe there are alternatives to the PropertyChangeListener that I don't know about. I saw there also in the English stackoverflow that this bug disappears using SwingUtilities.invokeLater() but the funniest thing of all is that I'm already using it, and that error doesn't disappear...
public static void main(String[]args){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
NuevaEntrada nv = new NuevaEntrada("M001");
} catch (ClassNotFoundException | SQLException | IOException | ParseException ex) {
Logger.getLogger(NuevaEntrada.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
I would really appreciate your help...
El
JTextFieldDateEditor
ends up using aSimpleDateFormat
1 to format the date, and alSimpleDateFormat
can be parameterized by passing the format String specified by the API .For example, "Monday, November 24" would be something like "EEEE, dd' of 'MMMM'."
Simply specify the format for the
SimpleDateFormat
, make sure itLocale
is Spain or another Spanish-speaking country, and forget about listeners and such.1 Which is usually by far the most rational way to convert text to dates and vice versa in Java.