How to show a message when no jradiobutton is selected?

Asked

Viewed 24 times

-1

I am doing a java swing exercise that contains the following screen with Jradiobuttons:

inserir a descrição da imagem aqui

When "java swing" is selected it should show a message by clicking on Jbutton and vice versa with the second option. But how to show a message when there is no option selected?

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

    if (jrbJavaSwing.isSelected()) {
        JOptionPane.showMessageDialog(null, "Java Swing");
    }
    if (jrbOutraLP.isSelected()) {
        JOptionPane.showMessageDialog(null, "Outra linguagem... Tem certeza?");
    }
    JOptionPane.showMessageDialog(null, "Você não selecionou nada");

Because that way with this Joptionpane on the last line, it keeps returning the message always.

1 answer

-1


The method isSelected returns a Boolean, he returns false if not selected, then:

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

    if (jrbJavaSwing.isSelected()) {
        JOptionPane.showMessageDialog(null, "Java Swing");
    }
    if (jrbOutraLP.isSelected()) {
        JOptionPane.showMessageDialog(null, "Outra linguagem... Tem certeza?");
    }
    if (jrbJavaSwing.isSelected() == false && jrbOutraLP.isSelected() == false) {
        JOptionPane.showMessageDialog(null, "Você não selecionou nada");
    }
}

Browser other questions tagged

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