I have problem with Radio Button

I am making small online examination application.

The problem is, suppose I select option 2 as the correct answer for first question,

Then for second question option 2 appears as selected ,

This may confuse the candidate for examination,,

Then if I make choice of 4th option,this appears selected for the next question,,,


I used:

radop1.setSelected(false)

But it is not helping,

Please help

Regards,

Harleen

package login;

import java.awt.*; 

import java.awt.event.*; 

import javax.swing.*;

import java.sql.*;




public class Student implements ActionListener
{

	private static String Previous = "Previous";

	private static String Next = "Next";

	private static String Close= "Close";

	private static String Radio= "Radio";

	
	JButton btnstart =new JButton("Start");

	JButton btnprevious =new JButton("Previous");

	JButton btnnext =new JButton("Next");

	JButton btnclose =new JButton("Close");
	
	

	
	JLabel lblroll=new JLabel("Enter your cet Roll no. :");

	JTextField txtroll=new JTextField("");
	
	JLabel lblrule=new JLabel("Instructions:");

	JLabel lblrule1=new JLabel("1) After 6 minutes the test would be automatically closed");

	JLabel lblrule2=new JLabel("2) You can close the test whenever you want by clicking  on close button");

	JLabel lblrule3=new JLabel("3) To start the test,click on start button ");


	
	JLabel lblno=new JLabel("");

	JLabel lblque=new JLabel("");
	
	JRadioButton radop1=new JRadioButton("");

	JRadioButton radop2=new JRadioButton("");

	JRadioButton radop3=new JRadioButton("");

	JRadioButton radop4=new JRadioButton("");

	

	JLabel lblmark1=new JLabel("");	

	JLabel lblmark2=new JLabel("");	

	JLabel lblmark3=new JLabel("");	

	JLabel lblmark4=new JLabel("");	


	
	ButtonGroup bg=new ButtonGroup();

	

	public JPanel p1 = new JPanel();

	public JPanel p2 = new JPanel();

	public JPanel p3 = new JPanel();
	

	CustomPanel panel = new CustomPanel();

	Color lemon=new Color(255,246,143);
	

	Student()
	{
		p1.setLayout(null);

		p1.setBackground(lemon);

		p1.setEnabled(false);

		p1.setVisible(false);



		p2.setLayout(null);

		p2.setBackground(lemon);

		p2.setEnabled(false);

		p2.setVisible(false);


		p3.setLayout(null);

		p3.setBackground(lemon);

		p3.setEnabled(false);

		p3.setVisible(false);


		
		radop1.setBackground(lemon);

		radop2.setBackground(lemon);

		radop3.setBackground(lemon);

		radop4.setBackground(lemon);

		
		p1.add(lblrule); 

		p1.add(lblrule1); 

		p1.add(lblrule2); 

		p1.add(lblrule3); 

		p1.add(btnstart);

		p1.add(lblroll);

		p1.add(txtroll);



		p2.add(lblno);
		
		p2.add(lblque);

		p2.add(btnnext);
		
		p2.add(btnprevious);

		p2.add(btnclose);

		p2.add(radop1);

		p2.add(radop2);

		p2.add(radop3);

		p2.add(radop4);

		bg.add(radop1);

		bg.add(radop2);

		bg.add(radop3);

		bg.add(radop4);





		p3.add(lblmark1);

		p3.add(lblmark2);

		p3.add(lblmark3);

		p3.add(lblmark4);



		
		 
			
		p1.add(panel);

		panel.setBounds(650,270,500,300);

		p1.setBounds(20,170,1000,500);

		p2.setBounds(20,170,700,600);

		p3.setBounds(20,170,700,600);


		
		lblmark1.setBounds(40,100,250,25);

		lblmark2.setBounds(40,150,410,25);

		lblmark3.setBounds(40,200,430,25);

		lblmark4.setBounds(40,250,410,25);


		
		lblroll.setBounds(40,80,200,25);

		txtroll.setBounds(190,80,100,25);

		lblrule.setBounds(40,120,250,25);

		lblrule1.setBounds(40,150,410,25);

		lblrule2.setBounds(40,200,430,25);

		lblrule3.setBounds(40,250,410,25);

		btnstart.setBounds(145,300,90,25);

		
		lblno.setBounds(100,100,50,25);		

		lblque.setBounds(150,100,450,25);

		radop1.setBounds(100,150,300,25);

		radop2.setBounds(100,200,300,25);

		radop3.setBounds(100,250,300,25);

		radop4.setBounds(100,300,300,25);

		btnprevious.setBounds(100,350,90,25);

		btnnext.setBounds(250,350,90,25);

		btnclose.setBounds(600,150,90,25);
		


		radop1.addActionListener(this);

		radop2.addActionListener(this);

		radop3.addActionListener(this);

		radop4.addActionListener(this);
		
		radop1.setActionCommand(Radio);

		radop2.setActionCommand(Radio);

		radop3.setActionCommand(Radio);

		radop4.setActionCommand(Radio);

		
		btnstart.addActionListener(this);

		btnclose.addActionListener(this);
		
		btnprevious.addActionListener(this);

		btnnext.addActionListener(this);

		btnprevious.setActionCommand(Previous);

		btnnext.setActionCommand(Next);

		btnclose.setActionCommand(Close);


	}


	class CustomPanel extends JPanel
	{   

		public void paintComponent (Graphics painter)   
		{    
			
			Image pic =Toolkit.getDefaultToolkit().getImage("best.png");

			if(pic != null)
			
			painter.drawImage(pic, 130,10, this); 

			   

		}

	}

	public void actionPerformed(ActionEvent e) 
	{	
		String cmd = e.getActionCommand();

		
		radop1.setSelected(false);

		radop2.setSelected(false);

		radop3.setSelected(false);

		radop4.setSelected(false);


		if (e.getSource()==btnstart) 
		{
			p1.setVisible(false);
		
			p2.setEnabled(true);

			p2.setVisible(true);
							
            					
				try
				{
					Class.forName("oracle.jdbc.driver.OracleDriver");

					String url="jdbc:oracle:thin:@localhost:1521:xe";

					String username="hr";

					String password="hr";

					Connection connection=DriverManager.getConnection(url,username,password);

					Statement statement=connection.createStatement();
        					
			 		String query1="Select qno,question,op1,op2,op3,op4 from test where qno = 1";

					String query2="update test set your = 0 where qno in(1,2,3,4,5)";
					
					ResultSet resultset2=statement.executeQuery(query2);

					ResultSet resultset1=statement.executeQuery(query1);

					while(resultset1.next())
					{
						lblno.setText(resultset1.getString(1));

						lblque.setText(resultset1.getString(2));
											
						radop1.setText(resultset1.getString(3));

						radop2.setText(resultset1.getString(4));

						radop3.setText(resultset1.getString(5));

						radop4.setText(resultset1.getString(6));

						
					
						
					}
					

					connection.close();			
 				}
				catch(ClassNotFoundException cnfe)
				{
	
					System.out.print("ERROR LOADING DRIVER: " + cnfe);

				}			
				catch(SQLException se)
				{ 
					System.out.print("SQL Exception  " + se);

				}




		}	
		


		if(Radio.equals(cmd))
		{
			int qno = Integer.parseInt(lblno.getText());

			int ur=0;				 		

			try
			{
				Class.forName("oracle.jdbc.driver.OracleDriver");

				String url="jdbc:oracle:thin:@localhost:1521:xe";

				String username="hr";
	
				String password="hr";

				Connection connection=DriverManager.getConnection(url,username,password);

				Statement statement=connection.createStatement();

				
				if(radop1.isSelected())
				{
					ur=1;	
				}
				else if(radop2.isSelected())
				{
					ur=2;	
				}
				else if(radop3.isSelected())
				{
					ur=3;
				}					
				else if(radop4.isSelected())
				{
					ur=4;	
				}

				
				

        					
			 	String query2="update test set your =" +ur + "where qno = " +qno;

				ResultSet resultset=statement.executeQuery(query2);
				
					
				connection.close();			
 				}
				catch(ClassNotFoundException cnfe)
				{
	
					System.out.print("ERROR LOADING DRIVER: " + cnfe);

				}			
				catch(SQLException se)
				{ 
					System.out.print("SQL Exception  " + se);

				}

						
				
			}



			
		if (Previous.equals(cmd)) 
		{
			
			int qno = Integer.parseInt(lblno.getText());

			String query1="";
	
				radop1.setSelected(false);

				radop2.setSelected(false);

				radop3.setSelected(false);

				radop4.setSelected(false);			
            					
				try
				{
					Class.forName("oracle.jdbc.driver.OracleDriver");

					String url="jdbc:oracle:thin:@localhost:1521:xe";

					String username="hr";

					String password="hr";

					Connection connection=DriverManager.getConnection(url,username,password);

					Statement statement=connection.createStatement();
				
					if(qno!=1)
        					
			 			query1="Select qno,question,op1,op2,op3,op4,your from test where qno ="+(qno-1);

					else

						query1="Select qno,question,op1,op2,op3,op4,your from test where qno = 5";



					ResultSet resultset=statement.executeQuery(query1);

					while(resultset.next())
					{
						lblno.setText(resultset.getString(1));

						lblque.setText(resultset.getString(2));
											
						radop1.setText(resultset.getString(3));

						radop2.setText(resultset.getString(4));

						radop3.setText(resultset.getString(5));

						radop4.setText(resultset.getString(6));

						int ur=Integer.parseInt(resultset.getString(7));

						if (ur==1)
						{
							radop1.setSelected(true);
						}
						else if (ur==2)
						{
							radop2.setSelected(true);
						}
						else if (ur==3)
						{
							radop3.setSelected(true);
						}
						else if (ur==4)
						{
							radop4.setSelected(true);
						}
						else if(ur==0)
						{
							
							radop1.setSelected(false);

							radop2.setSelected(false);

							radop3.setSelected(false);

							radop4.setSelected(false);
						}

					
						
					}
					

					connection.close();			
 				}
				catch(ClassNotFoundException cnfe)
				{
	
					System.out.print("ERROR LOADING DRIVER: " + cnfe);

				}			
				catch(SQLException se)
				{ 
					System.out.print("SQL Exception  " + se);

				}




		}	
		

		if (Next.equals(cmd)) 
		{
			
			int qno = Integer.parseInt(lblno.getText());

			String query1="";	
							
            			radop1.setSelected(false);

				radop2.setSelected(false);

				radop3.setSelected(false);

				radop4.setSelected(false);		
				try
				{
					Class.forName("oracle.jdbc.driver.OracleDriver");

					String url="jdbc:oracle:thin:@localhost:1521:xe";

					String username="hr";

					String password="hr";

					Connection connection=DriverManager.getConnection(url,username,password);

					Statement statement=connection.createStatement();
				
					if(qno!=5)
        					
			 			query1="Select qno,question,op1,op2,op3,op4,your from test where qno ="+(qno+1);

					else

						query1="Select qno,question,op1,op2,op3,op4,your from test where qno = 1";



					ResultSet resultset=statement.executeQuery(query1);

					while(resultset.next())
					{
						lblno.setText(resultset.getString(1));

						lblque.setText(resultset.getString(2));
											
						radop1.setText(resultset.getString(3));

						radop2.setText(resultset.getString(4));

						radop3.setText(resultset.getString(5));

						radop4.setText(resultset.getString(6));

						int ur=Integer.parseInt(resultset.getString(7));

						if (ur==1)
						{
							radop1.setSelected(true);
						}
						else if (ur==2)
						{
							radop2.setSelected(true);
						}
						else if (ur==3)
						{
							radop3.setSelected(true);
						}
						else if (ur==4)
						{
							radop4.setSelected(true);
						}
						else if(ur==0)
						{
							
							radop1.setSelected(false);

							radop2.setSelected(false);

							radop3.setSelected(false);

							radop4.setSelected(false);
						}

					
						
					}
					

					connection.close();			
 				}
				catch(ClassNotFoundException cnfe)
				{
	
					System.out.print("ERROR LOADING DRIVER: " + cnfe);

				}			
				catch(SQLException se)
				{ 
					System.out.print("SQL Exception  " + se);

				}




		}




			
		if (Close.equals(cmd)) 
		{
			
			int marks=0;	

			int roll = Integer.parseInt(txtroll.getText());	

			String eligible="";				
            					
				try
				{
					Class.forName("oracle.jdbc.driver.OracleDriver");

					String url="jdbc:oracle:thin:@localhost:1521:xe";

					String username="hr";

					String password="hr";

					Connection connection=DriverManager.getConnection(url,username,password);

					Statement statement=connection.createStatement();
				
					for(int i=0;i<5;i++)
					{
        					
			 			String query1="Select correct,your from test where qno ="+(i+1);

						ResultSet resultset=statement.executeQuery(query1);

						while(resultset.next())
						{
						
							if( (resultset.getString(1)).equals(resultset.getString(2)) )

								marks=marks+1;
						}

					}

					p3.setEnabled(true);

					p3.setVisible(true);

					p2.setVisible(false);

					lblmark1.setText("Your "+ marks +" answers were correct out of 5");

					lblmark2.setText("Your Percentage is: "+ (marks *20));

					if((marks*20)>=40)
					{
						lblmark3.setText("You are Eligible for Sikh Quota");
						
						eligible="yes";
					}
					else
					{
						lblmark3.setText("You are  not eligible for Sikh Quota");
							
						eligible ="no";

					}

					lblmark4.setText("Thank you!!");


					String query2="insert into result values ( " + roll + "," + marks +",' " + eligible + " ' ) ";

					ResultSet resultset=statement.executeQuery(query2);
				

					connection.close();			
 				}
				catch(ClassNotFoundException cnfe)
				{
	
					System.out.print("ERROR LOADING DRIVER: " + cnfe);

				}			
				catch(SQLException se)
				{ 
					System.out.print("SQL Exception  " + se);

				}




		}	
		
	
 	}



}

The problem is, suppose I select option 2 as the correct answer for first question, Then for second question option 2 appears as selected ,

What if you unselected all the answers before showing the next question?

Can you post a small program that compiles and executes that demonstrates your problem?
What you posted doesn't execute (has no main() method) and requires a database.

Yes,it has no main,because I m using packages.

When the application starts,for the first question all options are unselected,

I wanted same for every question,

@NormR1

if (Next.equals(cmd)) 
		{
			
			int qno = Integer.parseInt(lblno.getText());

			String query1="";	
							
            			radop1.setSelected(false);

				radop2.setSelected(false);

				radop3.setSelected(false);

				radop4.setSelected(false);

When the next command is clicked, I am unselecting all in the code,But it is not helping,

Can you post a small program that compiles and executes that demonstrates your problem?

Can you post a small program that compiles and executes that demonstrates your problem?

@NormR1 I have tried to make the program as small as possible, and I have made it executable too.

But It still requires database connectivity, because I have stored questions for online examination in the database.

When you will execute it,as you will click on start,the first question will appear,then you can continue with next and previous buttons, And it will show my problem,

Thanks

import java.awt.*; 

import java.awt.event.*; 

import javax.swing.*;

import java.sql.*;

public class Student extends JFrame implements ActionListener
{

	private static String Previous = "Previous";

	private static String Next = "Next";

	private static String Radio= "Radio";

	
	JButton btnprevious =new JButton("Previous");

	JButton btnnext =new JButton("Next");

	JButton btnstart =new JButton("Start");


	
	
	JLabel lblno=new JLabel("");

	JLabel lblque=new JLabel("");
	
	JRadioButton radop1=new JRadioButton("");

	JRadioButton radop2=new JRadioButton("");

	JRadioButton radop3=new JRadioButton("");

	JRadioButton radop4=new JRadioButton("");

	
	ButtonGroup bg=new ButtonGroup();

	Container cnt=getContentPane();
	
	
	Student()
	{
		cnt.setLayout(null);

		
		setSize(1000,600);

	 	setVisible(true);

		

		cnt.add(lblno);
		
		cnt.add(lblque);

		cnt.add(btnnext);
		
		cnt.add(btnprevious);

		cnt.add(btnstart);


		cnt.add(radop1);

		cnt.add(radop2);

		cnt.add(radop3);

		cnt.add(radop4);


		bg.add(radop1);

		bg.add(radop2);

		bg.add(radop3);

		bg.add(radop4);


		
		btnstart.setBounds(100,50,90,25);

		lblno.setBounds(100,100,50,25);		

		lblque.setBounds(150,100,450,25);

		radop1.setBounds(100,150,300,25);

		radop2.setBounds(100,200,300,25);

		radop3.setBounds(100,250,300,25);

		radop4.setBounds(100,300,300,25);

		btnprevious.setBounds(100,350,90,25);

		btnnext.setBounds(250,350,90,25);

		
		


		radop1.addActionListener(this);

		radop2.addActionListener(this);

		radop3.addActionListener(this);

		radop4.addActionListener(this);
		
		radop1.setActionCommand(Radio);

		radop2.setActionCommand(Radio);

		radop3.setActionCommand(Radio);

		radop4.setActionCommand(Radio);

		
		
		btnstart.addActionListener(this);
		
		btnprevious.addActionListener(this);

		btnnext.addActionListener(this);

		btnprevious.setActionCommand(Previous);

		btnnext.setActionCommand(Next);

		

	}

	public void actionPerformed(ActionEvent e) 
	{	
		String cmd = e.getActionCommand();

			
            					
		
		if (e.getSource()==btnstart) 
		{
							
            					
				try
				{
					Class.forName("oracle.jdbc.driver.OracleDriver");

					String url="jdbc:oracle:thin:@localhost:1521:xe";

					String username="hr";

					String password="hr";

					Connection connection=DriverManager.getConnection(url,username,password);

					Statement statement=connection.createStatement();
        					
			 		String query1="Select qno,question,op1,op2,op3,op4 from test where qno = 1";

					String query2="update test set your = 0 where qno in(1,2,3,4,5)";
					
					ResultSet resultset2=statement.executeQuery(query2);

					ResultSet resultset1=statement.executeQuery(query1);

					while(resultset1.next())
					{
						lblno.setText(resultset1.getString(1));

						lblque.setText(resultset1.getString(2));
											
						radop1.setText(resultset1.getString(3));

						radop2.setText(resultset1.getString(4));

						radop3.setText(resultset1.getString(5));

						radop4.setText(resultset1.getString(6));

						
					
						
					}
					

					connection.close();			
 				}
				catch(ClassNotFoundException cnfe)
				{
	
					System.out.print("ERROR LOADING DRIVER: " + cnfe);

				}			
				catch(SQLException se)
				{ 
					System.out.print("SQL Exception  " + se);

				}




		}




			
		radop1.setSelected(false);

		radop2.setSelected(false);

		radop3.setSelected(false);

		radop4.setSelected(false);	


		if(Radio.equals(cmd))
		{
			int qno = Integer.parseInt(lblno.getText());

			int ur_ans=0;				 		

			try
			{
				Class.forName("oracle.jdbc.driver.OracleDriver");

				String url="jdbc:oracle:thin:@localhost:1521:xe";

				String username="hr";
	
				String password="hr";

				Connection connection=DriverManager.getConnection(url,username,password);

				Statement statement=connection.createStatement();

				
				if(radop1.isSelected())
				{
					ur_ans=1;	
				}
				else if(radop2.isSelected())
				{
					ur_ans=2;	
				}
				else if(radop3.isSelected())
				{
					ur_ans=3;
				}					
				else if(radop4.isSelected())
				{
					ur_ans=4;	
				}

				
				

        					
			 	String query2="update test set your_answer =" +ur_ans + "where qno = " +qno;

				ResultSet resultset=statement.executeQuery(query2);
				
					
				connection.close();			
 			}
			catch(ClassNotFoundException cnfe)
			{
	
				System.out.print("ERROR LOADING DRIVER: " + cnfe);

			}			
			catch(SQLException se)
			{ 
				System.out.print("SQL Exception  " + se);

			}

						
				
		}



			
		if (Previous.equals(cmd)) 
		{
			
			int qno = Integer.parseInt(lblno.getText());

			String query1="";
	
			radop1.setSelected(false);

			radop2.setSelected(false);

			radop3.setSelected(false);

			radop4.setSelected(false);			
            					
			try
			{
				Class.forName("oracle.jdbc.driver.OracleDriver");

				String url="jdbc:oracle:thin:@localhost:1521:xe";

				String username="hr";

				String password="hr";

				Connection connection=DriverManager.getConnection(url,username,password);

				Statement statement=connection.createStatement();
				
				if(qno!=1)
        					
			 		query1="Select qno,question,op1,op2,op3,op4,your from test where qno ="+(qno-1);

				else

					query1="Select qno,question,op1,op2,op3,op4,your from test where qno = 5";



				ResultSet resultset=statement.executeQuery(query1);

				while(resultset.next())
				{
					lblno.setText(resultset.getString(1));

					lblque.setText(resultset.getString(2));
											
					radop1.setText(resultset.getString(3));

					radop2.setText(resultset.getString(4));

					radop3.setText(resultset.getString(5));

					radop4.setText(resultset.getString(6));

					int ur_ans=Integer.parseInt(resultset.getString(7));

					if (ur_ans==1)
					{
						radop1.setSelected(true);
					}
					else if (ur_ans==2)
					{
						radop2.setSelected(true);
					}
					else if (ur_ans==3)
					{
						radop3.setSelected(true);
					}
					else if (ur_ans==4)
					{
						radop4.setSelected(true);
					}
					else if(ur_ans==0)
					{
							
						radop1.setSelected(false);

						radop2.setSelected(false);

						radop3.setSelected(false);

						radop4.setSelected(false);
					}

					
						
				}
					

				connection.close();			
 			}
			catch(ClassNotFoundException cnfe)
			{
	
				System.out.print("ERROR LOADING DRIVER: " + cnfe);
			}			
			catch(SQLException se)
			{ 
				System.out.print("SQL Exception  " + se);

			}




		}	
		

		if (Next.equals(cmd)) 
		{
			
			int qno = Integer.parseInt(lblno.getText());

			String query1="";	
							
            			radop1.setSelected(false);

				radop2.setSelected(false);

				radop3.setSelected(false);

				radop4.setSelected(false);		
				try
				{
					Class.forName("oracle.jdbc.driver.OracleDriver");

					String url="jdbc:oracle:thin:@localhost:1521:xe";

					String username="hr";

					String password="hr";

					Connection connection=DriverManager.getConnection(url,username,password);

					Statement statement=connection.createStatement();
				
					if(qno!=5)
        					
			 			query1="Select qno,question,op1,op2,op3,op4,your from test where qno ="+(qno+1);

					else

						query1="Select qno,question,op1,op2,op3,op4,your from test where qno = 1";



					ResultSet resultset=statement.executeQuery(query1);

					while(resultset.next())
					{
						lblno.setText(resultset.getString(1));

						lblque.setText(resultset.getString(2));
											
						radop1.setText(resultset.getString(3));

						radop2.setText(resultset.getString(4));

						radop3.setText(resultset.getString(5));

						radop4.setText(resultset.getString(6));

						int ur_ans=Integer.parseInt(resultset.getString(7));

						if (ur_ans==1)
						{
							radop1.setSelected(true);
						}
						else if (ur_ans==2)
						{
							radop2.setSelected(true);
						}
						else if (ur_ans==3)
						{
							radop3.setSelected(true);
						}
						else if (ur_ans==4)
						{
							radop4.setSelected(true);
						}
						else if(ur_ans==0)
						{
							
							radop1.setSelected(false);

							radop2.setSelected(false);

							radop3.setSelected(false);

							radop4.setSelected(false);
						}

					
						
					}
					

					connection.close();			
 				}
				catch(ClassNotFoundException cnfe)
				{
	
					System.out.print("ERROR LOADING DRIVER: " + cnfe);

				}			
				catch(SQLException se)
				{ 
					System.out.print("SQL Exception  " + se);

				}




		}



	}



	public static void main(String args[])
	{

		Student student=new Student();

		student.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}




}

I don't see any comments in your code telling where and what you are trying to do.
Or where the problem is.

Can you post a small program that compiles and executes that demonstrates your problem?

With a frame, 2 panels, two radio buttons, a button group, one button and an actionListener. Probably less than 30 lines of code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.