Failed to compare the values of a JSpinner. JFrame

1

I have 2 spinners, each one with its corresponding values taken from a string:

        SpinnerListModel pal = new SpinnerListModel(palabras );
        spinner = new javax.swing.JSpinner(pal);
        SpinnerListModel word = new SpinnerListModel(words);
        spinner1 = new javax.swing.JSpinner(word);

and these are the arrays that are previously declared:

static String palabras[] = {"hola", "manzana", "leon", "castillo", "videojuego"};
    static String words[] = {"hello", "apple", "lion", "castle", "videogame"};

My problem arises when I try to compare the values that they have in the spinner, when I press a button I get the values in the following way:

private void btnComprobarActionPerformed(java.awt.event.ActionEvent evt) {                                             
        //prueba.setText(String.valueOf(spinner.getValue()));
        if(String.valueOf(spinner).equals("hola")&& String.valueOf(spinner1).equals("hello")){
            prueba.setText("Correcto");
        }
    }

If I get the value and I show it in the text it shows the word it contains at the moment, but if I compare it it does not show anything, which may be failing?

    
asked by Alberto Martínez 03.05.2018 в 13:10
source

1 answer

1

It's just that you're not comparing it in the same way you get it, look at your code:

private void btnComprobarActionPerformed(java.awt.event.ActionEvent evt) {                                             
    //prueba.setText(String.valueOf(spinner.getValue())); GETVALUE()
    if(String.valueOf(spinner).equals("hola") && 
       String.valueOf(spinner1).equals("hello")){ //aquí no usas el getValue()
        prueba.setText("Correcto");
    }
}

What you are doing within the if is to compare the following:
if(JSpinner.valueOf() == "cadena de texto) , you are not comparing the value, but the object in the form of String.valueOf()

Try this, it should work:

private void btnComprobarActionPerformed(java.awt.event.ActionEvent evt) {
    if(String.valueOf(spinner.getValue()).equals("hola")) && 
       String.valueOf(spinner1.getValue()).equals("hello")){ 
        prueba.setText("Correcto");
    }
}
    
answered by 03.05.2018 / 13:21
source