JButton doesnt recognize does JRadioButton is select
Asked Answered
I

1

0

This JButton examines does is some of JRadioButtons is selected. So if JRadionButtons from list below is selected message dialog box will print message that's correct answer else it should print that isn't correct answer.

I have 40 JRadioButtons, 10 Button Groups, 4 JRadioButtons is showing on screen at the time.

I noted that this JButton (btn2) remember first examines so if I click on wrong JRadioButton and then I click on this btn2 it's will show me message dialog box with "this isn't correct answer"- that's correct, next if I continue selecting wrong JRadioButton it will continue to show that's isn't correct answer - that's correct, but if I select correct JRadioButton it will show me message dialog box with "this is correct answer" - that's correct but it will continue that if I now select wrong JRadioButton

        btn2.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            tim.stop();
            btn1.setEnabled(true);

            //Ne radi kako treba ne pravi razliku da li su   selektovani ili nisu
            //ne znam u čemu je problem
            if(nizRButtona[1].isSelected()
                    ||nizRButtona[5].isSelected()
                    ||nizRButtona[11].isSelected()
                    ||nizRButtona[13].isSelected()
                    ||nizRButtona[16].isSelected()
                    ||nizRButtona[23].isSelected()
                    ||nizRButtona[27].isSelected()
                    ||nizRButtona[29].isSelected()
                    ||nizRButtona[32].isSelected()){
                s+=10;
                JOptionPane.showMessageDialog(null, "To je tačan odgovor"); //This is correct answer
            }else if(nizRButtona[36].isSelected()){
                s+=10;
                JOptionPane.showMessageDialog(null, "To je tačan odgovor osvoji ste ukupno "+s+" poena"); // This is correct answer you won s points.
                btn1.setText("Započni opet igru");
            }else if(nizRButtona[33].isSelected()
                    ||nizRButtona[34].isSelected()
                    ||nizRButtona[35].isSelected()){
        JOptionPane.showMessageDialog(null, "To nije tačan odgovor osvoji ste ukupno "+s+" poena"); //This isn't correct answer you won s points
                btn1.setText("Započni opet igru");
            }else{                  
                JOptionPane.showMessageDialog(null, "To nije tačan odgovor"); //This isn't correct answer
            }

        }

    });

All code (will be deleted)

   package Kviz;
   import javax.swing.*;
   import java.awt.*;
   import java.awt.event.*;

   public class Kviz {

private JFrame okvir;
private JPanel p1,p2,p3;
private JLabel [] nizLabela;
private JRadioButton [] nizRButtona;
private ButtonGroup[] nizGrupa;
private JPanel [] nizPanela;
private CardLayout cl;
private JLabel lb1,lb2;
private JButton btn1,btn2;
private Timer tim;
private int t,s;

public Kviz(){
    pripremiGUI();
}

private void pripremiGUI(){

    okvir=new JFrame();
    okvir.setSize(400, 400);
    okvir.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    okvir.setLocationRelativeTo(null);
    okvir.setLayout(new BorderLayout());

    p1=new JPanel();
    p1.setLayout(new FlowLayout());

    lb1=new JLabel("Dobrodošli u kviz");

    cl=new CardLayout();

    p2=new JPanel();
    p2.setLayout(cl);

    nizPanela = new JPanel[10];

    //Niz panela na kome će se smenjivati pitanja i odgovori
    for(int i=0;i<nizPanela.length;i++){

        nizPanela[i] = new JPanel();
        p2.add(nizPanela[i], String.valueOf(i));
        nizPanela[i].setLayout(new GridLayout(0,1));

    }

    //Niz odgovora

    nizRButtona = new JRadioButton[40];

    for(int i=0;i<nizRButtona.length;i++){
        nizRButtona[i] = new JRadioButton();

        switch(i){
            case 0:nizRButtona[i].setText("Mesopotamiji");
                break;
            case 1:nizRButtona[i].setText("Grčkoj");
                break;
            case 2:nizRButtona[i].setText("Kini");
                break;
            case 3:nizRButtona[i].setText("Egiptu");
                break;
            case 4:nizRButtona[i].setText("Kinšasa");
                break;
            case 5:nizRButtona[i].setText("Lagos");
                break;
            case 6:nizRButtona[i].setText("Ulan Bator");
                break;
            case 7:nizRButtona[i].setText("Abu Dabi");
                break;
            case 8:nizRButtona[i].setText("Velikoj Britaniji");
                break;
            case 9:nizRButtona[i].setText("Norveškoj");
                break;
            case 10:nizRButtona[i].setText("SAD");
                break;
            case 11:nizRButtona[i].setText("Danskoj");
                break;
            case 12:nizRButtona[i].setText("Step mom");
                break;
            case 13:nizRButtona[i].setText("Runaway Bride");
                break;
            case 14:nizRButtona[i].setText("Grease");
                break;
            case 15:nizRButtona[i].setText("Nothing Hill");
                break;
            case 16:nizRButtona[i].setText("Neil Armstrong");
                break;
            case 17:nizRButtona[i].setText("John Lenon");
                break;
            case 18:nizRButtona[i].setText("Martin Luter King");
                break;
            case 19:nizRButtona[i].setText("Winston Churchill");
                break;
            case 20:nizRButtona[i].setText("Meka");
                break;
            case 21:nizRButtona[i].setText("Medina");
                break;
            case 22:nizRButtona[i].setText("Dubai");
                break;
            case 23:nizRButtona[i].setText("Rijad");
                break;
            case 24:nizRButtona[i].setText("Španije");
                break;
            case 25:nizRButtona[i].setText("Venecije");
                break;
            case 26:nizRButtona[i].setText("Portugala");
                break;
            case 27:nizRButtona[i].setText("Đenove");
                break;
            case 28:nizRButtona[i].setText("Holokaust");
                break;
            case 29:nizRButtona[i].setText("Perl Harbour");
                break;
            case 30:nizRButtona[i].setText("Napad na Francusku");
                break;
            case 31:nizRButtona[i].setText("Blitzkrieg na Poljsku");
                break;
            case 32:nizRButtona[i].setText("Bon");
                break;
            case 33:nizRButtona[i].setText("Minhen");
                break;
            case 34:nizRButtona[i].setText("Lajpcig");
                break;
            case 35:nizRButtona[i].setText("Hamburg");
                break;
            case 36:nizRButtona[i].setText("Čile");
                break;
            case 37:nizRButtona[i].setText("Istočni Timor");
                break;
            case 38:nizRButtona[i].setText("Portugal");
                break;
            case 39:nizRButtona[i].setText("Brazil");
                break;

        }
    }

    //Niz button grupa i dodavanje komponenti u njega
    nizGrupa=new ButtonGroup[10];

    int j=0;
    for(int i=0;i<nizGrupa.length;i++){

    nizGrupa[i] = new ButtonGroup();

    nizGrupa[i].add(nizRButtona[j]);
    nizGrupa[i].add(nizRButtona[j+1]);
    nizGrupa[i].add(nizRButtona[j+2]);
    nizGrupa[i].add(nizRButtona[j+3]);

    j+=4;
    }

    //Niz pitanja
    nizLabela = new JLabel[10];

    for(int i=0;i<nizLabela.length;i++){
        nizLabela[i] = new JLabel();
        switch(i){
            case 0:nizLabela[i].setText("U kojoj zemlji su rođene Olimpijske igre");
                break;
            case 1:nizLabela[i].setText("Koji je glavni grad Nigerije");
                break;
            case 2:nizLabela[i].setText("Kojoj državi pripada Grenland");
                break;
            case 3:nizLabela[i].setText("Osim filma \"Pretty Women\" u kojem filmu zajedno glume Julia Roberts i Richard Gere");
                break;
            case 4:nizLabela[i].setText("Kako se zove prvi čovek koji je sleteo na mesec");
                break;
            case 5:nizLabela[i].setText("Glavni grad Saudijske Arabije");
                break;
            case 6:nizLabela[i].setText("Kristofer Kolumob je iz");
                break;
            case 7:nizLabela[i].setText("Koji događaj je naveo SAD da se uključe u rat");
                break;
            case 8:nizLabela[i].setText("Koji je grad bio glavni pri ujedinjenju Nemačke");
                break;
            case 9:nizLabela[i].setText("U kojoj zemlji je bio na vlasti diktator Salazar");
                break;
        }
    }

    p3=new JPanel();
    p3.setLayout(new FlowLayout());

    //Menja panele sa pitanjima i odgovorima
    btn1=new JButton("Sledeće pitanje");
    btn1.setEnabled(false);
    btn1.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            cl.next(p2);
            btn1.setEnabled(false);
            btn2.setEnabled(true);
            t=10;
            lb2.setText(String.valueOf(t));
            tim.start();
            btn1.setText("Sledeće pitanje");
        }
    });

    //Proverava da li su odgovori tačni
    btn2=new JButton("Odgovori");
    btn2.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            tim.stop();
            btn1.setEnabled(true);

            //
            //
            if(nizRButtona[1].isSelected()
                    ||nizRButtona[5].isSelected()
                    ||nizRButtona[11].isSelected()
                    ||nizRButtona[13].isSelected()
                    ||nizRButtona[16].isSelected()
                    ||nizRButtona[23].isSelected()
                    ||nizRButtona[27].isSelected()
                    ||nizRButtona[29].isSelected()
                    ||nizRButtona[32].isSelected()){
                s+=10;
                JOptionPane.showMessageDialog(null, "To je tačan odgovor");
            }else if(nizRButtona[36].isSelected()){
                s+=10;
                JOptionPane.showMessageDialog(null, "To je tačan odgovor osvoji ste ukupno "+s+" poena");
                btn1.setText("Započni opet igru");
            }else if(nizRButtona[33].isSelected()
                    ||nizRButtona[34].isSelected()
                    ||nizRButtona[35].isSelected()){
                JOptionPane.showMessageDialog(null, "To nije tačan odgovor osvoji ste ukupno "+s+" poena");
                btn1.setText("Započni opet igru");
            }else{
                JOptionPane.showMessageDialog(null, "To nije tačan odgovor");
            }

        }

    });

    t=10;

    //Tajmer
    lb2=new JLabel("10");
    tim=new Timer(1000,new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            if(t>0){
            t--;
            lb2.setText(String.valueOf(t));
            }else{
                tim.stop();
                JOptionPane.showMessageDialog(null, "Vreme je isteklo");
                btn2.setEnabled(false); 
                btn1.setEnabled(true);
            }
        }
    });
}

private void pokreniGUI(){
    p1.add(lb1);

    p3.add(btn1);
    p3.add(btn2);
    p3.add(lb2);

    int q=0;
    for(int w=0;w<10;w++){

    nizGrupa[w] = new ButtonGroup();

    nizPanela[w].add(nizLabela[w]);
    nizPanela[w].add(nizRButtona[q]);
    nizPanela[w].add(nizRButtona[q+1]);
    nizPanela[w].add(nizRButtona[q+2]);
    nizPanela[w].add(nizRButtona[q+3]);

    q+=4;
    }

    okvir.add(p1,BorderLayout.NORTH);
    okvir.add(p2,BorderLayout.CENTER);
    okvir.add(p3,BorderLayout.SOUTH);

    cl.show(p2, "0");

    tim.start();
    okvir.setVisible(true);
}

public static void main(String[] args) {
    Kviz pokreni=new Kviz();
    pokreni.pokreniGUI();

}

   }
Infringe answered 31/3, 2016 at 17:24 Comment(9)
Can you translate what does the messages say? Or specify which options are the correct and incorrect ones?Doubleedged
And you might want to make your code smaller and provide a Runnable Example instead of code snippetsDoubleedged
You don't need all code i say what's the problem. First time when i select correct answer all other answer will be correct (Message dialog box will say that's correct answer). So if i on first question select correct answer all other will be correct. If i select wrong answer in first many questions it will show wrong answer but when i select correct again it will show for all other answer as correctInfringe
does is this my logical ok? when i write this i think like: if(some of JRadioButton is selected) show message box "Correct answer" else if(the last correct JRadioButton is selected) show message box "Correct answer + how many points i was won" else if(the last 3 wrong JRadioButton is selected) show message box "Wrong answer + how many points i was won" else(all other JRadioButton is selected) show messge box "Wrong answer"Infringe
Alright after that I can understand your logic a little more, let me see what can be wrong...Doubleedged
i add all code you can see itInfringe
please see the answer below, and don't delete your code, StackOverflow is a site where people should benefit from your question and our answers, in case they have a similar problem, they can know why and how to solve it. Not only you are the one who should benefit from the answer(s). Please take the tourDoubleedged
ok i will not delete but i has problem on the my last question some guy say that my code is too long it has only 100 linesInfringe
Yes, that's why you should provide a Runnable Example also called a Minimal Complete and Verifiable Example (MCVE), it's not your WHOLE working code, but a smaller one, one which contains everything we need to test it and compiles, so we can copy-paste it and see the same output as you. Also consider that your MCVE should be in english as a recommendation so we can understand what it says. Please read the link I gave and How to ask a good questionDoubleedged
D
2

As I thought before you posted the code, this error comes from you having all your JRadioButtons as a single array and the selection is still there, you need to clear this selection each time you finish to evaluate it. Next time try to use a debugger too.

On your code you need to modify 2 things:

  1. Clear each of your group selections as follows:

This is because you have a global array of JRadioButtons, instead you should have a smaller array which would get it's text from a global String array, and updates each time you continue to next question, so you only have to manage a single ButtonGroup instead of 10. This is the way you can "easily" make your program to work, but the "best" way to fix it is re-do all your program so you have a JRadioButton array of size 4, a single ButtonGroup and a String array with the text for each JRadioButton

//Your if-else codes are above here
else {
    JOptionPane.showMessageDialog(null, "To nije tačan odgovor");
}
for (int i = 0; i < 10; i++) {
    nizGrupa[i].clearSelection();
}
  1. Remove this line nizGrupa[w] = new ButtonGroup(); from your pokreniGUI method

Because you initialized it before, so you don't need to do it again, if you don't, then the above selection clear won't work because it will make reference to a ButtonGroup that no longer exists.

Update with full code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Kviz {

private JFrame okvir;
private JPanel p1,p2,p3;
private JLabel [] nizLabela;
private JRadioButton [] nizRButtona;
private ButtonGroup[] nizGrupa;
private JPanel [] nizPanela;
private CardLayout cl;
private JLabel lb1,lb2;
private JButton btn1,btn2;
private Timer tim;
private int t,s;

public Kviz(){
    pripremiGUI();
}

private void pripremiGUI(){

    okvir=new JFrame();
    okvir.setSize(400, 400);
    okvir.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    okvir.setLocationRelativeTo(null);
    okvir.setLayout(new BorderLayout());

    p1=new JPanel();
    p1.setLayout(new FlowLayout());

    lb1=new JLabel("Dobrodošli u kviz");

    cl=new CardLayout();

    p2=new JPanel();
    p2.setLayout(cl);

    nizPanela = new JPanel[10];

    //Niz panela na kome će se smenjivati pitanja i odgovori
    for(int i=0;i<nizPanela.length;i++){

        nizPanela[i] = new JPanel();
        p2.add(nizPanela[i], String.valueOf(i));
        nizPanela[i].setLayout(new GridLayout(0,1));

    }

    //Niz odgovora

    nizRButtona = new JRadioButton[40];

    for(int i=0;i<nizRButtona.length;i++){
        nizRButtona[i] = new JRadioButton();

        switch(i){
            case 0:nizRButtona[i].setText("Mesopotamiji");
                break;
            case 1:nizRButtona[i].setText("Grčkoj");
                break;
            case 2:nizRButtona[i].setText("Kini");
                break;
            case 3:nizRButtona[i].setText("Egiptu");
                break;
            case 4:nizRButtona[i].setText("Kinšasa");
                break;
            case 5:nizRButtona[i].setText("Lagos");
                break;
            case 6:nizRButtona[i].setText("Ulan Bator");
                break;
            case 7:nizRButtona[i].setText("Abu Dabi");
                break;
            case 8:nizRButtona[i].setText("Velikoj Britaniji");
                break;
            case 9:nizRButtona[i].setText("Norveškoj");
                break;
            case 10:nizRButtona[i].setText("SAD");
                break;
            case 11:nizRButtona[i].setText("Danskoj");
                break;
            case 12:nizRButtona[i].setText("Step mom");
                break;
            case 13:nizRButtona[i].setText("Runaway Bride");
                break;
            case 14:nizRButtona[i].setText("Grease");
                break;
            case 15:nizRButtona[i].setText("Nothing Hill");
                break;
            case 16:nizRButtona[i].setText("Neil Armstrong");
                break;
            case 17:nizRButtona[i].setText("John Lenon");
                break;
            case 18:nizRButtona[i].setText("Martin Luter King");
                break;
            case 19:nizRButtona[i].setText("Winston Churchill");
                break;
            case 20:nizRButtona[i].setText("Meka");
                break;
            case 21:nizRButtona[i].setText("Medina");
                break;
            case 22:nizRButtona[i].setText("Dubai");
                break;
            case 23:nizRButtona[i].setText("Rijad");
                break;
            case 24:nizRButtona[i].setText("Španije");
                break;
            case 25:nizRButtona[i].setText("Venecije");
                break;
            case 26:nizRButtona[i].setText("Portugala");
                break;
            case 27:nizRButtona[i].setText("Đenove");
                break;
            case 28:nizRButtona[i].setText("Holokaust");
                break;
            case 29:nizRButtona[i].setText("Perl Harbour");
                break;
            case 30:nizRButtona[i].setText("Napad na Francusku");
                break;
            case 31:nizRButtona[i].setText("Blitzkrieg na Poljsku");
                break;
            case 32:nizRButtona[i].setText("Bon");
                break;
            case 33:nizRButtona[i].setText("Minhen");
                break;
            case 34:nizRButtona[i].setText("Lajpcig");
                break;
            case 35:nizRButtona[i].setText("Hamburg");
                break;
            case 36:nizRButtona[i].setText("Čile");
                break;
            case 37:nizRButtona[i].setText("Istočni Timor");
                break;
            case 38:nizRButtona[i].setText("Portugal");
                break;
            case 39:nizRButtona[i].setText("Brazil");
                break;

        }
    }

    //Niz button grupa i dodavanje komponenti u njega
    nizGrupa=new ButtonGroup[10];

    int j=0;
    for(int i=0;i<nizGrupa.length;i++){

    nizGrupa[i] = new ButtonGroup();

    nizGrupa[i].add(nizRButtona[j]);
    nizGrupa[i].add(nizRButtona[j+1]);
    nizGrupa[i].add(nizRButtona[j+2]);
    nizGrupa[i].add(nizRButtona[j+3]);

    j+=4;
    }

    //Niz pitanja
    nizLabela = new JLabel[10];

    for(int i=0;i<nizLabela.length;i++){
        nizLabela[i] = new JLabel();
        switch(i){
            case 0:nizLabela[i].setText("U kojoj zemlji su rođene Olimpijske igre");
                break;
            case 1:nizLabela[i].setText("Koji je glavni grad Nigerije");
                break;
            case 2:nizLabela[i].setText("Kojoj državi pripada Grenland");
                break;
            case 3:nizLabela[i].setText("Osim filma \"Pretty Women\" u kojem filmu zajedno glume Julia Roberts i Richard Gere");
                break;
            case 4:nizLabela[i].setText("Kako se zove prvi čovek koji je sleteo na mesec");
                break;
            case 5:nizLabela[i].setText("Glavni grad Saudijske Arabije");
                break;
            case 6:nizLabela[i].setText("Kristofer Kolumob je iz");
                break;
            case 7:nizLabela[i].setText("Koji događaj je naveo SAD da se uključe u rat");
                break;
            case 8:nizLabela[i].setText("Koji je grad bio glavni pri ujedinjenju Nemačke");
                break;
            case 9:nizLabela[i].setText("U kojoj zemlji je bio na vlasti diktator Salazar");
                break;
        }
    }

    p3=new JPanel();
    p3.setLayout(new FlowLayout());

    //Menja panele sa pitanjima i odgovorima
    btn1=new JButton("Sledeće pitanje");
    btn1.setEnabled(false);
    btn1.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            cl.next(p2);
            btn1.setEnabled(false);
            btn2.setEnabled(true);
            t=10;
            lb2.setText(String.valueOf(t));
            tim.start();
            btn1.setText("Sledeće pitanje");
        }
    });

    //Proverava da li su odgovori tačni
    btn2=new JButton("Odgovori");
    btn2.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            tim.stop();
            btn1.setEnabled(true);

            if(nizRButtona[1].isSelected()
                ||nizRButtona[5].isSelected()
                ||nizRButtona[11].isSelected()
                ||nizRButtona[13].isSelected()
                ||nizRButtona[16].isSelected()
                ||nizRButtona[23].isSelected()
                ||nizRButtona[27].isSelected()
                ||nizRButtona[29].isSelected()
                ||nizRButtona[32].isSelected()){
                s+=10;
                JOptionPane.showMessageDialog(null, "Correct");
            } else if(nizRButtona[36].isSelected()){
                s+=10;
                JOptionPane.showMessageDialog(null, "Correct, points earned: " + s);
                btn1.setText("Započni opet igru");
            }else if(nizRButtona[33].isSelected()
                    ||nizRButtona[34].isSelected()
                    ||nizRButtona[35].isSelected()){
                JOptionPane.showMessageDialog(null, "Incorrect, points earned " + s);
                btn1.setText("Započni opet igru");
            }else{
                JOptionPane.showMessageDialog(null, "Incorrect");
            }
            for (int i = 0; i < 10; i++) {
                nizGrupa[i].clearSelection();
            }
        }

    });

    t=10;

    //Tajmer
    lb2=new JLabel("10");
    tim=new Timer(1000,new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            if(t>0){
            t--;
            lb2.setText(String.valueOf(t));
            }else{
                tim.stop();
                JOptionPane.showMessageDialog(null, "Vreme je isteklo");
                btn2.setEnabled(false); 
                btn1.setEnabled(true);
            }
        }
    });
}

private void pokreniGUI(){
    p1.add(lb1);

    p3.add(btn1);
    p3.add(btn2);
    p3.add(lb2);

    int q=0;
    for(int w=0;w<10;w++){

    //nizGrupa[w] = new ButtonGroup();

    nizPanela[w].add(nizLabela[w]);
    nizPanela[w].add(nizRButtona[q]);
    nizPanela[w].add(nizRButtona[q+1]);
    nizPanela[w].add(nizRButtona[q+2]);
    nizPanela[w].add(nizRButtona[q+3]);

    q+=4;
    }

    okvir.add(p1,BorderLayout.NORTH);
    okvir.add(p2,BorderLayout.CENTER);
    okvir.add(p3,BorderLayout.SOUTH);

    cl.show(p2, "0");

    tim.start();
    okvir.setVisible(true);
}

public static void main(String[] args) {
    Kviz pokreni=new Kviz();
    pokreni.pokreniGUI();

}
}
Doubleedged answered 31/3, 2016 at 18:51 Comment(5)
if i remove nizGrupa[w] = new ButtonGroup(); from pokreniGUI method i don't see it on panels but how to check does is answer correct if i have only 4 radiobutons i need 10x moreInfringe
@Infringe It works, see update, maybe you wrote the code on the wrong placesDoubleedged
Also you need only 4 radio buttons (the visible ones) and reuse them when the next question comes...Doubleedged
yeah it's good but i don't how to check does answer is correct if i have only 4 buttons, and how to add all on 10 panels? maybe my first question can be solved by getTextInfringe
if you still want to know how to do that, finish your code, make it work, translate it to english and paste it here (don't forget to read how that site works first). Also if the answer solved your question please accept itDoubleedged

© 2022 - 2024 — McMap. All rights reserved.