Trigger two different events with JButton

0

I'd like to know how to trigger different events using the same button. What I wanted was for the next click to give jButton1.setText("Créditos"); and return to btnGerar.setVisible(true);

The idea is that when I click on "Credits", it would drop everything and only my name would appear, and after I clicked back (it 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  ");
}
    
asked by anonymous 04.06.2017 / 06:14

1 answer

1

You can do 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 it is always a good idea to retrieve the button from ActionEvent . To simplify the code a little, 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);

}
    
04.06.2017 / 11:40