Re: new question
Art Cummings wrote:
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?
I'd try to convert CalcGrade into an SSCCE by adding a main method
public static void main(String[] args) {
Swingutilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("CalcGrade");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new CalcGrade());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
I haven't looked to see what else is required.
Thanks
Art
I can't help with your problem at the moment but I'll make some comments
on style.
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
{
I think you've removed all indentation. When posting to usenet I convert
tabs to spaces. I *always* use indentation to help readability of code.
I find it hard to read unindented code.
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));
Compare the above comments with the above statement. This is a classic
illustration of why comments should not merely say the same thing as the
code. Now I don't know if the comment is in error or if the code is in
error. This is a distraction.
I usually use comments to explain *WHY* I'm doing something, not *WHAT*
I'm doing. Sometimes I put high level goals in comments to identify the
purpose of a block of code.
//radio buttons
numberGrade = new JRadioButton("Number grade");
letterGrade = new JRadioButton("Letter grade");
average = new JRadioButton("Average grade");
I find it helpful to have the variable name give some kind of clue to
the variable type. For example I'd maybe have numberGradeButton. This
especially helps if you also have some other variables that hold the
values corresponding to the visual components (e.g. boolean numberGrade).
This is a controversial subject :-)
calc = new JButton("Calculate");
result = new JTextField();
//Add a border around the panel
// setBorder(BorderFactory.createTitledBorder(""));
I'd delete commented-out code before posting here. Its just more
distraction.
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);
Where did avg2 spring from? It is equal to "" at this point.
}
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();
For purposes of createing an SSCCE I'd maybe include a rudimentary inner
class named StudentTestPlusButtons with methods that return canned values.
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);
Are you saying this works ...
result.setText(avg2);
.... but this doesn't?
}
/* else if (letterGrade.isSelected())
{
}
else
{
}*/
}
}
}
Empty or commented out else clauses are a bad sign!
At least put some distinctive System.out.println("..."); in there.