new question
I'm currently trying to update a JTextField if the user selects a particular
radio button. It calls a function from another class to load an array. It
then calculates the average and assigns this to the result text field. I've
tried doing this both as just a variable and a static. I use print
statements and can see that the variable is set for a second and then
disappears. If I hit my calculate button a second time the variable appears
in the text field. I'm not sure why. I use a similar approach in a
different part of the program, albeit without radio buttons and it works.
can anyone shed and light?
Thanks
Art
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.util.ArrayList; // to use array list
import java.util.Scanner; // Needed for the Scanner class
import java.io.*; // Needed for the File class
public class CalcGrade extends JPanel
{
private JRadioButton numberGrade;//number grade radio button
private JRadioButton letterGrade;//letter grade radio button
private JRadioButton average;
private ButtonGroup bg;
private JButton calc;
private JTextField result;
private static String avg2="";
public CalcGrade()
{
//Create a gridlayout manager
//with 3 rows 1 column
setLayout(new GridLayout(5,1));
//radio buttons
numberGrade = new JRadioButton("Number grade");
letterGrade = new JRadioButton("Letter grade");
average = new JRadioButton("Average grade");
calc = new JButton("Calculate");
result = new JTextField();
//Add a border around the panel
// setBorder(BorderFactory.createTitledBorder(""));
add(calc);
add(result);
calc.addActionListener(new calcButtonListener());
//group radio buttons
bg = new ButtonGroup();
bg.add(numberGrade);
bg.add(letterGrade);
bg.add(average);
add(numberGrade);
add(letterGrade);
add(average);
result.setText(avg2);
}
private class calcButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
double holdTotal=0,avg=0;
int holdIndex=0;
TheStudent newStud = new TheStudent();
StudentTestPlusButtons curStudent = new StudentTestPlusButtons();
double[][]grades;
grades = newStud.getScores();
holdIndex = newStud.getIndex();
if (average.isSelected())
{
System.out.println("You are here");
holdTotal = grades[holdIndex][0]+grades[holdIndex][1]+grades[holdIndex][2];
avg = holdTotal/3;
System.out.println("average "+avg);
DecimalFormat formatter = new DecimalFormat("00.00");
avg2= Double.toString(avg);
System.out.println("average2 "+avg2);
result.setText(avg2);
}
/* else if (letterGrade.isSelected())
{
}
else
{
}*/
}
}
}