Davynch0
Davynch0

Reputation: 103

How do I change the text in a JLabel for this app?

I'm trying to program a simple button clicks counter in java, and it's supposed to be finished, but when I run it, it appear to be something wrong with the JLabel change of test in line 33, when i click the button the application crashes. How do I fix it; what I am doing wrong? Here is the code:

package Main;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Eventos extends JFrame{

    JTextField clicksCounter;
    Eventos (){
        //Parámetros generales de la ventana.
        super("Eventos en Java");//Titulo de la ventana.
        setSize(320,200);//Tamaño de la ventana.
        setVisible(true);//Configurar como visible.
        setLocation(520,220);//Posicion inicial de la ventana en el medio y un poco arriba.
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//Salir al cerrrar la venta.
        this.setLayout(new GridLayout(2,1));
        //Componentes dentro de la ventana.
        JButton ClickMe = new JButton("Haz Click aqui!");//Boton que cuenta clicks.
        add(ClickMe);//Agrega el Boton a la venatana.
        ClickMe.setSize(140, 20);
        ClickMe.setLocation(90,40);
        JTextField clicksCounter = new JTextField("Número de Clicks");//Muestra el número de clicks al boton.
        add(clicksCounter);
        ButtonHandler handler = new ButtonHandler();
        ClickMe.addActionListener(handler);
    }
    private class ButtonHandler implements ActionListener{
        public void actionPerformed(ActionEvent handler){
            int times=0;
            times ++;
            clicksCounter.setText("Clicks:"+times);
        }
    }
}

Upvotes: 0

Views: 108

Answers (4)

Timr
Timr

Reputation: 1032

While all the other answers are partially correct, you also have one other large error. You define clicksCounter twice. Once in your global variables, and again within your Constructor.

See:

JTextField clicksCounter;

JTextField clicksCounter = new JTextField("Número de Clicks");//Muestra el número de clicks a l boton.

Change the second line (located within your constructor) to simply: clicksCounter = new JTextField("Número de Clicks");//Muestra el número de clicks a l boton.

If you don't do this, you'll get a NullPointerException.

The other problem you have is the local definition of times within your ActionListener. By defining times in your listener itself, you reset it with every click of the button, thus defeating the purpose of the counter.

Move times so it's outside the scope of the action listener, preferably with all other global definitions. Your action listener should then look like this:

private class ButtonHandler implements ActionListener{
    public void actionPerformed(ActionEvent handler){
        times++;
        clicksCounter.setText("Clicks:"+times);
    }
}

Upvotes: 3

MadProgrammer
MadProgrammer

Reputation: 347214

You create the times variable each time the button is clicked and the increment it...

int times=0;
times ++; // This will never be anything other then 1

Change the action listener class so that the times variable is maintained as part of the instance of the class

private class ButtonHandler implements ActionListener{
    private int times=0;
    public void actionPerformed(ActionEvent handler){
        times ++;
        clicksCounter.setText("Clicks:"+times);
    }
}

You define clicksCounter as a instance variable, but in your constructor, you create a local instance named the same (AKA name conflict). This means when you run the progam, the instance variable clicksCounter is null.

Change the constructor to remove the reference to the local variable...

public class Eventos extends JFrame{

    JTextField clicksCounter;
    Eventos (){
        // ... Your previous code ...
        JTextField clicksCounter = new JTextField("Número de Clicks");//Muestra el número de clicks al boton.
        // ... Your previous code ...
    }

Upvotes: 2

jpm
jpm

Reputation: 3155

times needs to be a member variable, either of your Eventos class, or of ButtonHandler. As it is, it will be instantiated as 0 every time your button is clicked, since it's a local variable.

Upvotes: 0

PC.
PC.

Reputation: 7024

public void actionPerformed(ActionEvent handler){
        int times=0;
        times ++;
        clicksCounter.setText("Clicks:"+times);
}

in this method, times is a local variable. Its value will be initialized to 0 whenever this method is called. Instead u must use:

private static int times=0;
public void actionPerformed(ActionEvent handler) {
    times ++;
    clicksCounter.setText("Clicks:"+times);
}

even a better solution would be to use

public class Eventos extends JFrame implements ActionListener

and use times as non static variable private int times=0;

Upvotes: 0

Related Questions