'looping a jframe window

I need help with every time a button is pressed and it goes through the validation process, It will increment the index by 1 and will show up on the UI. However, I'm having an issue where, when it get incremented, it won't go up by 1 in the UI and just won't update basically.

index = 1;

JLabel lblNewLabel = new JLabel("test " + index );
testTextField = new JTextField();
frame.getContentPane().add(testTextField, BorderLayout.CENTER);
testTextField.setColumns(10);
testTextField = new JTextField();

nextButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        String txtField = testTextField.getText();
         
        int s = 3; 
        int test = s - 1;
          
        if (txtInvalid(txtField) == false) {
            JOptionPane.showMessageDialog(null, "input valid text field");
        } 
        else {
            JOptionPane.showMessageDialog(null, "gratz, onto the next !!");
            index++;
        }
    }
});


Solution 1:[1]

You can declare the "index" variable globally or use static keyword. The static variable gets memory only once in the class area at the time of class loading.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Anonymous