java.lang.IllegalStateException: Attempt to mutate in notification (JDateChooser, JAVA)

2

Good morning!

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 could not exactly identify why it's caused ... you see, I'm using some libraries downloaded from the following source:

link

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 in order to obtain it. I'm using the JDateChooser component which is very similar to VB.NET ... the only difference is that when choosing a date of it, the component prints that date in the textfield with a short date format ( 10/24/2016) while in the other original program, I printed it on a long date (Monday, October 24, 2016).

To solve this problem, I wrote a small function in my class so that we can say, "convert" or "translate" (as you want to call it) that short date format to 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 the problems come ... and why do I set the function as well? Well, I do not know exactly if this is what is giving me the exception or not. At the beginning, I gave the error "Unparseable Date", and it was because the program was trying to convert to Date the already converted string of the TextField of the date (as if it took the short date, converts it to a long date and retakes the long date to convert ... but of course, I would give an error because I was already converted). What I did was put a condition to regulate it (if the TextField string is less than or equal to 10 characters, then proceed).

((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);
            }
        }
    });

It seemed to work correctly, but at the moment I changed to another date, I launched another error more ... and this time complained that the JTextField of the date was "empty". Insert another condition to the if ... (If the number of characters in the TextField string is not equal to 0, then proceed) So I leave the propertyChange event to 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 is supposed that I should not throw 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 the function that 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 may also be that I'm not using the correct event to use my function ... maybe there are alternatives to the PropertyChangeListener that you do not know. I saw also in stackoverflow in English that this bug disappears using SwingUtilities.invokeLater () but the funniest thing of all, is that I am already using it, and that error does not 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 greatly appreciate your help ...

    
asked by TwoDent 24.10.2016 в 20:37
source

1 answer

2

The JTextFieldDateEditor ends up using a SimpleDateFormat 1 to format the date, and the SimpleDateFormat can be parameterized 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 SimpleDateFormat , make sure that the Locale is Spain or another Castilian-speaking country, and forget about listeners and others.

1 That is usually by far the most rational way to convert text to dates and vice versa in Java.

    
answered by 24.10.2016 / 21:21
source