Trigger two different events with Jbutton

Asked

Viewed 223 times

0

I would like to know how to trigger different events using the same button. What I wanted is that, with the next click, he gave a jButton1.setText("Créditos"); and return to the btnGerar.setVisible(true);

The idea is that when I click on "Credits", everything would disappear and only my name would appear, and after clicking back(which would be the same button), it would appear the other menus back.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {       
    pnBusca.setVisible(false);
    pnOpcao.setVisible(false);
    btnGerar.setVisible(false);
    jButton1.setText("  Voltar  ");
}

1 answer

1


You can do it like this:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

     boolean isVisible;

    if(jButton1.getText().equals.("Créditos")) {

        isVisible = false;
        jButton1.setText("  Voltar  ");
    } else {

        isVisible = true;
        jButton1.setText("Créditos")
    }

    pnBusca.setVisible(isVisible);
    pnOpcao.setVisible(isVisible);
    btnGerar.setVisible(isVisible);
}

Remembering that the ideal is always redeem the button from the ActionEvent. Simplifying the code a little bit, it could look like this:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

    JButton btn = (JButton)evt.getSource();
    boolean isVisible = btn.getText().equals.("Créditos");

    btn.setText(isVisible ? "  Voltar  " : "Créditos");

    pnBusca.setVisible(!isVisible);
    pnOpcao.setVisible(!isVisible);
    btnGerar.setVisible(!isVisible);

}

Browser other questions tagged

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