problem with revalidating a jframe.

Posted by John Quesie on Stack Overflow See other posts from Stack Overflow or by John Quesie
Published on 2011-01-15T01:47:32Z Indexed on 2011/01/15 1:53 UTC
Read the original article Hit count: 507

Filed under:
|

I have this code which should take the radio button input do a little math and display a popup. which it does fine. but then it is supposed to re validate and ask the next question. when i get to the second question, the answer always comes out as the isSelected(true) value no matter which radio button you click on. SO to be clear the first time through it works fin but when the second question comes up, it just takes the default radio button every time.

public class EventHandler implements ActionListener {

private Main gui;

public EventHandler(Main gui){
    this.gui = gui;
}
public void actionPerformed(ActionEvent e){
    String answer = "";
    double val = 1;
    //get current answer set
    String [] anArr = gui.getAnswers(gui.currentStage, gui.currentQuestion);


    if(e.getSource() == gui.exit){
        System.exit(0);
    }
    if(e.getSource() == gui.submit){

        if(gui.a1.isSelected()){
           answer = anArr[0];
           val = gui.getScore(1);
        }
        if(gui.a2.isSelected()){
           answer = anArr[1];
           val = gui.getScore(2);
        }
        if(gui.a3.isSelected()){
           answer = anArr[2];
           val = gui.getScore(3);
        }
        if(gui.a4.isSelected()){
           answer = anArr[3];;
           val = gui.getScore(4);
        }
        JOptionPane.showMessageDialog(null, popupMessage(answer, val), "Your Answer", 1);

        //compute answer here

        //figure out what next question is to send
        gui.moveOn();
        gui.setQA(gui.currentStage, gui.currentQuestion);
        //resets gui
        gui.goWest();

        gui.q.revalidate();


    }
}
public String popupMessage(String ans, double val){
    //displays popup after an answer has been choosen

    gui.computeScore(val);
    String text = " You Answered " + ans + " Your score is now " + gui.yourScore ;

    return text;
}

}

public class Main extends JFrame { public JLabel question; public JButton exit; public JButton submit; public JRadioButton a1; public JRadioButton a2; public JRadioButton a3; public JRadioButton a4; public ButtonGroup bg; public double yourScore = 1; public int currentQuestion = 1; public String currentStage = "startup"; JPanel q;

public Main(){
    setTitle("Ehtics Builder");
    setLocation(400,400);
    setLayout(new BorderLayout(5,5));
    setQA("startup", 1);

    goNorth();
    goEast();
    goWest();
    goSouth();
    goCenter();

    pack();
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void goNorth(){

}
public void goWest(){

    q = new JPanel();
    q.setLayout(new GridLayout(0,1));
    q.add(question);
    bg.add(a1);
    bg.add(a2);
    bg.add(a3);
    bg.add(a4);
    a1.setSelected(true);
    q.add(a1);
    q.add(a2);
    q.add(a3);
    q.add(a4);

    add(q, BorderLayout.WEST);
    System.out.println();
}
public void goEast(){

}
public void goSouth(){
    JPanel p = new JPanel();
    p.setLayout(new FlowLayout(FlowLayout.CENTER));
    exit = new JButton("Exit");
    submit = new JButton("Submit");
    p.add(exit);
    p.add(submit);
    add(p, BorderLayout.SOUTH);

    EventHandler myEventHandler = new EventHandler(this);
    exit.addActionListener(myEventHandler);
    submit.addActionListener(myEventHandler);
}
public void goCenter(){

}


public static void main(String[] args) {
    Main open = new Main();
}
public String getQuestion(String type, int num){
    //reads the questions from a file
    String question = "";
    String filename = "";
    String [] ques;
    num = num - 1;
    if(type.equals("startup")){
        filename = "startup.txt";
    }else if(type.equals("small")){
        filename = "small.txt";
    }else if(type.equals("mid")){
        filename = "mid.txt";
    }else if(type.equals("large")){
        filename = "large.txt";
    }else{
        question = "error";
        return question;
    }

    ques = readFile(filename);

    for(int i = 0;i < ques.length;i++){
        if(i == num){
            question = ques[i];
        }
    }

    return question;
}
public String [] getAnswers(String type, int num){
    //reads the answers from a file
    String filename = "";
    String temp = "";
    String [] group;
    String [] ans;
    num = num - 1;
    if(type.equals("startup")){
        filename = "startupA.txt";
    }else if(type.equals("small")){
        filename = "smallA.txt";
    }else if(type.equals("mid")){
        filename = "midA.txt";
    }else if(type.equals("large")){
        filename = "largeA.txt";
    }else{
        System.out.println("Error");
    }

    group = readFile(filename);

    for(int i = 0;i < group.length;i++){
        if(i == num){
            temp = group[i];
        }
    }

    ans = temp.split("-");

    return ans;
}
public String [] getValues(String type, int num){
    //reads the answers from a file
    String filename = "";
    String temp = "";
    String [] group;
    String [] vals;
    num = num - 1;
    if(type.equals("startup")){
        filename = "startupV.txt";
    }else if(type.equals("small")){
        filename = "smallV.txt";
    }else if(type.equals("mid")){
        filename = "midV.txt";
    }else if(type.equals("large")){
        filename = "largeV.txt";
    }else{
        System.out.println("Error");
    }

    group = readFile(filename);

    for(int i = 0;i < group.length;i++){
        if(i == num){
            temp = group[i];
        }
    }

    vals = temp.split("-");

    return vals;
}
public String [] readFile(String filename){
    //reads the contentes of a file, for getQuestions and getAnswers
    String text = "";
    int i = -1;
    FileReader in = null;
    File f = new File(filename);
    try{
        in = new FileReader(f);
    }catch(FileNotFoundException e){
        System.out.println("file does not exist");
    }
    try{
        while((i = in.read()) != -1)
            text += ((char)i);
    }catch(IOException e){
        System.out.println("Error reading file");
    }
    try{
        in.close();
    }catch(IOException e){
        System.out.println("Error reading file");
    }

    String [] questions = text.split(":");
    return questions;
}
public void computeScore(double val){
    //calculates you score times the value of your answer
    yourScore = val * yourScore;
}
public double getScore(int aNum){
    //gets the score of an answer, stage and q number is already set in the class
    aNum = aNum - 1;
    double val = 0;
    double [] valArr = new double[4];
    for(int i = 0;i < getValues(currentStage, currentQuestion).length;i++){
        val = Double.parseDouble(getValues(currentStage, currentQuestion)[i]);
        valArr[i] = val;
    }
    if(aNum == 0){
        val = valArr[0];
    }
    if(aNum == 1){
        val = valArr[1];
    }
    if(aNum == 2){
        val = valArr[2];
    }
    if(aNum == 3){
        val = valArr[3];
    }
    // use current stage and questiion and trhe aNum to get the value for that answer
    return val;
}
public void nextQuestion(int num){
    //sets next question to use
    currentQuestion = num;
}
public void nextStage(String sta){
    // sets next stage to use
    currentStage = sta;
}
public void moveOn(){
    // uses the score and current question and stage to determine wher to go next and what stage to use next

    nextQuestion(2);
    nextStage("startup");
}
 public void setQA(String level, int num){
    String [] arr = getAnswers(level, num);
    question = new JLabel(getQuestion(level, num));
    bg = new ButtonGroup();
    a1 = new JRadioButton(arr[0]);
    a2 = new JRadioButton(arr[1]);
    a3 = new JRadioButton(arr[2]);
    a4 = new JRadioButton(arr[3]);
}

}

© Stack Overflow or respective owner

Related posts about java

Related posts about jframe