After a lot of googling i still haven't found what im looking for, mostly because i don't know what i'm looking for.
Basically i want the lock in button to be disabled until one of the radio buttons is selected, and i only want one of the radio buttons to be selected at a a time.
I haven't done any formatting yet so its still ugly.
My Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.*;
public class Game extends JFrame
{
JLabel lblQuestion;
JRadioButton btA;
JRadioButton btB;
JRadioButton btC;
JRadioButton btD;
JButton btLock;
JTextField txtQuestion;
int question = 0;
public Game()
{
getContentPane().setLayout(null);
setupGUI();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void setupGUI()
{
txtQuestion = new JTextField();
txtQuestion.setLocation(10,10);
txtQuestion.setSize(100,25);
txtQuestion.setText(Integer.toString(question));
getContentPane().add(txtQuestion);
lblQuestion = new JLabel();
lblQuestion.setLocation(50,82);
lblQuestion.setSize(300,50);
lblQuestion.setText("No_Label");
getContentPane().add(lblQuestion);
btA = new JRadioButton();
btA.setLocation(50,160);
btA.setSize(100,50);
btA.setText("No_Label");
btA.setSelected(false);
getContentPane().add(btA);
btB = new JRadioButton();
btB.setLocation(250,160);
btB.setSize(100,50);
btB.setText("No_Label");
btB.setSelected(false);
getContentPane().add(btB);
btC = new JRadioButton();
btC.setLocation(50,240);
btC.setSize(100,50);
btC.setText("No_Label");
btC.setSelected(false);
getContentPane().add(btC);
btD = new JRadioButton();
btD.setLocation(250,240);
btD.setSize(100,50);
btD.setText("No_Label");
btD.setSelected(false);
getContentPane().add(btD);
btLock = new JButton();
btLock.setLocation(150,303);
btLock.setSize(100,50);
btLock.setText("Lock in");
getContentPane().add(btLock);
btLock.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
question = question + 1;
txtQuestion.setText(Integer.toString(question));
try{
ArrayList<String> list = questions(question);
}catch(Exception ex){}
}
});
setTitle("Who wants to be a millionare");
setSize(570,400);
setVisible(true);
setResizable(true);
setLocationRelativeTo(null);
}
public ArrayList<String> questions(int quesion) throws IOException{
File file = new File("questions.txt");
if (!file.exists()){
throw new FileNotFoundException("Could not find \"users\" file");
}
FileReader fr = new FileReader(file.getAbsoluteFile());
BufferedReader br = new BufferedReader(fr);
ArrayList<String> list = new ArrayList<String>();
String s;
for(int i = 0; i*4 <= quesion; i++){
br.readLine();
}
while((s=br.readLine()) !=null){
list.add(s);
}
br.close();
return list;
}
public static void main( String args[] )
{
new Game();
}
}
First you need to add an action listener to whichever RadioButton needs to be pressed before the button is active.
Like this:
someRadioButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
someButton.enabled(true);
}
});
And secondly for the only one RadioButton used at a time you need a ButtonGroup such as:
ButtonGroup myButtonGroup = new ButtonGroup();
myButtonGroup.add(someRadioButton);
Add all the RadioButtons you want into a group.
Hope this helps :)