How to change the value of a jLabel with a jButton (Netbeans)

Asked

Viewed 242 times

-1

I’m a beginner in Java, and I want to create a supposedly very simple program. Inside a panel there is only a jlabel and a jbutton. jLabel has the value "0". When Clicking on jbutton, the idea is that it increases a number with each click on jButton.

2 answers

0

botao.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jlabel.setText(Integer.parseInt(jlabel.getText()) + 1); } } );

I’m not sure if this function is correct as I haven’t opened the IDE for Ester.

However, it is not difficult to do what you want, pass the function to it to execute when you press a similar button in the example above. within the function is only to give a setText on that jlabel of yours, passing the text of it not forgetting to convert to integer and incrementing 1. ready :)

  • I haven’t been able to yet. I actually know that the concept is very simple, but the practice is being very complicated. I already managed to make a list with Abstracttabelmodel and fields for registration, all complete, but change an element through a button ta being very difficult

0

I managed to fix it. I leave it here for those who need it too:

private int senha = 0;

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        if(senha == 50){
            JOptionPane.showMessageDialog(null,"Limite de Senhas já entregue.");
        } else{
            senha++;
            this.jLabel1.setText(senha+"");
        }
    }          
  • Mark your answer later (see how) not to leave the question open, ok?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.