Java Eclipse WindowsBuilder: it tells me to change my textfield and checkbox to final but, the error does not disappear

-1

The program says to change all my textfield and checkbox to "final" but, when I do, in another part of the code it tells me to remove "final", I'm confused.

the error at the beginning marks it here:

JButton btnCalcular = new JButton("Calcular");
        btnCalcular.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                // Si es Llamada Local
                if (checkboxLocal.isSelected())
                {
                }

                // Si es Llamada Provincial y Franja 1
                else if (checkboxProvincial.isSelected() && checkboxFranja1.isSelected())
                {
                }

                // Si es Llamada Provincial y Franja 2
                else if (checkboxProvincial.isSelected() && checkboxFranja2.isSelected())
                {
                }

                // Si es Llamada Provincial y Franja 3
                else if (checkboxProvincial.isSelected() && checkboxFranja3.isSelected())
                {
                }
            }
        });

The error is:

  

Can not refer to the non-final local variable txtOrderNumber defined   in an enclosing scope

This error appears in the following things:

                txtNumeroOrigen.setText("");
                txtNumeroDestino.setText("");
                txtDuracion.setText("");

                // Check Box
                checkboxProvincial.setSelected(false);
                checkboxProvincial.setEnabled(true);
                checkboxLocal.setSelected(false);
                checkboxLocal.setEnabled(true);
                checkboxFranja1.setSelected(false);
                checkboxFranja2.setSelected(false);
                checkboxFranja3.setSelected(false);
                checkboxFranja1.setEnabled(false);
                checkboxFranja2.setEnabled(false);
                checkboxFranja3.setEnabled(false);

What am I doing wrong ?, I am declaring things where it is not ?. He says to end it and after I remove it, I do not understand anything.

    
asked by Robert Gomez 15.11.2016 в 02:28
source

1 answer

1

There are two issues here:

  • To access a local variable from an anonymous class, the variable must be final . It is a problem caused because Java does not implement closures , and makes a "fix" allowing access to local variables final 1

  • If a variable is final , values can not be assigned after the declaration.

You add the final to solve the first problem, and you find the second one.

The solution is obvious:

final JCheckBox checkboxProvincial = new JCheckBox("Provincial");

and never assign a new object to checkboxProvincial . Repeat for the rest of the variables.

1 What Java does is take advantage of the fact that the value of the variable will not change (because it is final ) to make a "copy" of the reference in the anonymous class.     
answered by 15.11.2016 / 02:41
source