-2

how can i check is 2 jRadio button are selected and then show a new frame showing the new operation in the new frame ,the problem in my code is that is show the question 1 even if non of radio button is selected the program should check id addition and easy are selected and when click start test should show new frame

this is my main code

     package javaapplication78;
import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class javaapplication78{
JFrame f1;
JPanel panel1,panel2,panel3,panel4;
JButton b1,b2;
JLabel label_1,label_2;
JRadioButton radio_1,radio_2,radio_3,radio_4,radio_5,radio_6,radio_7;

javaapplication78(){

JFrame f1 = new JFrame ("MathTest - Main Menu");
f1.setVisible(true);
f1.setSize(300,400);
f1.setLayout(new GridLayout(0,1));
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

panel1 = new JPanel(new GridLayout(0, 2));
panel1.setBorder(BorderFactory.createLineBorder(Color.black, 1));
panel1.add(new JLabel("Select a test type"));
panel1.add(radio_1=new JRadioButton("Addition"));
panel1.add(new JLabel(""));
panel1.add(radio_2=new JRadioButton("Substraction"));
panel1.add(new JLabel(""));
panel1.add(radio_3=new JRadioButton("Multiplication"));
panel1.add(new JLabel(""));
panel1.add(radio_4=new JRadioButton("Division"));

panel2 = new JPanel(new GridLayout(0, 2));
panel2.setBorder(BorderFactory.createLineBorder(Color.black, 1));
panel2.add(new JLabel("select a diffculty level"));
panel2.add(radio_5=new JRadioButton("easy "));
panel2.add(new JLabel(""));
panel2.add(radio_6=new JRadioButton("moderate"));
panel2.add(new JLabel(""));
panel2.add(radio_7=new JRadioButton("hard"));

panel3 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
panel3.add(new JLabel(""));

JButton b1 = new JButton("Start test");      
panel3.add(b1);
b1.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {

new javaapplication79();

    }
});


  panel4 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
  panel4.add(b2=new JButton("    exit    "));

  f1.add(panel1);
  f1.add(panel2);
  f1.add(panel3);
  f1.add(panel4);

  f1.add(label_1); 
  f1.add(radio_1);
  f1.add(radio_2);
  f1.add(radio_3); 
  f1.add(radio_4); 

  f1.add(label_2);
  f1.add(radio_5);
  f1.add(radio_6); 
  f1.add(radio_7);
  b1 = new JButton();
  b2 = new JButton();
  f1.add(b1); 
  f1.add(b2);   
    }

  public static void main(String[] args)
  {
    javaapplication78 xyz =new javaapplication78();

  }
}

and this is frame code package javaapplication78;

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

public class javaapplication79{
JFrame f1;
JPanel panel1,panel4;
JLabel label_1;
JTextField t1;

 javaapplication79(){
f1 = new JFrame ("MathTest - Test Page");
f1.setVisible(true);
f1.setSize(400,150);
f1.setLayout(new GridLayout(0,1));

panel1 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
panel1.add(new JLabel("Question 1     14  -  5  = "));
panel1.add(new JTextField(10));
panel1.add(new JButton("Submit Answer"));



panel4 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
panel4.add(new JButton("   Cancel Test   "));
f1.add(panel1);
f1.add(panel4);
f1.add(label_1); 

}

}
  • radio buttons are designed to select only one. use check boxes for multiple section. – Braj May 09 '14 at 13:59
  • set the default value for each radio group initially then there is no case when none of radio buttons are selected – Braj May 09 '14 at 14:00

1 Answers1

1

Put some precoditions like

radio_1.isSelected(); returns true when selected.

radio_1.getText() if it is already selected, check for the selected value like this (value ='Y' or 'N')

Sireesh Yarlagadda
  • 12,978
  • 3
  • 74
  • 76
  • why i have to use radio_1.getText() i don't have any value ? – user3615990 May 09 '14 at 14:01
  • @user3615990 Just in case you need the value of the name of the radio button. It's an OOP design with the JRadioButton Object. I think Sireesh nailed it! I would add a button or action Listener. – Josef E. May 09 '14 at 14:06