I have a label,4 radio button.I have retrieved data from sql.I need to display the retrieved question in label and its 4 option in each of radio button.I got it working to display one question.my trouble is in button click.when next button is clicked question should change...any more ideas is appreciable....
import javax.swing.*;
import javax.swing.JFrame;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
class guimtd extends JFrame
{
JButton NEXT;
JLabel pno,problem;
JPanel radioPanel;
JRadioButton option1,option2,option3,option4;
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "db1";
String driverName = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "mantra";
String query2="select * from destination";
//final JTextField text1,text2;
guimtd()
{
final JFrame f=new JFrame("exam");
f.setLayout(null);
pno = new JLabel();
problem = new JLabel();
option1 = new JRadioButton();
option2 = new JRadioButton();
option3 = new JRadioButton();
option4 = new JRadioButton();
ButtonGroup bgroup = new ButtonGroup();
bgroup.add(option1);
bgroup.add(option2);
bgroup.add(option3);
bgroup.add(option4);
radioPanel = new JPanel();
radioPanel.setLayout(new GridLayout(3, 1));
radioPanel.add(option1);
// option1.setSelected(true);
radioPanel.add(option2);
radioPanel.add(option3);
radioPanel.add(option4);
radioPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Choices"));
NEXT = new JButton("NEXT");
pno.setBounds(100,100,50,50);
problem.setBounds(150,100,400,100);
option1.setBounds(250,220,100,20);
option2.setBounds(250,270,100,20);
option3.setBounds(250,320,100,20);
option4.setBounds(250,370,100,20);
NEXT.setBounds(500,370,100,20);
f.setVisible(true);
f.setSize(1024,768);
option1.addActionListener(new radioaction());
NEXT.addActionListener(new nextaction());
f.add(pno);
f.add(problem);
//f.add(radioPanel);
f.add(NEXT);
f.repaint();
f.add(option1);
f.add(option2);
f.add(option3);
f.add(option4);
}
public class nextaction implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
int c=0;
try
{
Class.forName(driverName).newInstance();
con = DriverManager.getConnection(url+dbName, userName, password);
Statement st = con.createStatement();
ResultSet rs1 = st.executeQuery(query2);
while(rs1.next())
{
if(ae.getSource()==NEXT)
{
removeAll();
c++;
NEXT.setText("clicked " + c + " times!");
String d1=rs1.getString(1);
String d2=rs1.getString(2);
String d3=rs1.getString(3);
String d4=rs1.getString(4);
String d5=rs1.getString(5);
//String d5=rs1.getString(5);
pno.setText(d1);
problem.setText(d2);
problem.revalidate();
option1.setText(d3);
option2.setText(d4);
option3.setText(d5);
}
}
rs1.close();
st.close();
con.close();
}
catch(Exception e)
{
System.out.println("Error"+e);
}
}
}
public class radioaction implements ActionListener{
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null,"This is the " + e.getActionCommand() +
" radio button.");
}
}
}
class gui
{
public static void main(String arg[])
{
guimtd window = new guimtd();
window.setVisible(true);
}
}