Reputation: 384
I have a button to clear the text fields in the program that I am putting together but when I click it, it doesn't clear them. Here is my code plus the code for the button. I don't know what is wrong this is the way I shown to set up GUI's and this is the way it has always worked for me.
public class GradeCalculator extends JFrame implements ActionListener {
Container c;
JTextField gradeWeight1,gradeWeight2,gradeWeight3,gradeWeight4,gradeWeight5,gradeWeight6,
gradeWeight7,gradeWeight8,gradeWeight9,gradeWeight10;
JTextField gradeAch1,gradeAch2,gradeAch3,gradeAch4,gradeAch5,gradeAch6,gradeAch7,
gradeAch8,gradeAch9,gradeAch10;
JLabel score , weight;
JButton btnGPA, btnClear,btnCalc;
JPanel northP, southP;
public void actionPerformed(ActionEvent e) {
if(e.getSource() == btnClear){
gradeAch1.setText(null);
gradeWeight1.setText(null);
gradeAch2.setText(null);
gradeWeight2.setText(null);
gradeAch3.setText(null);
gradeWeight3.setText(null);
gradeAch4.setText(null);
gradeWeight4.setText(null);
gradeAch5.setText(null);
gradeWeight5.setText(null);
gradeAch6.setText(null);
gradeWeight6.setText(null);
gradeAch7.setText(null);
gradeWeight7.setText(null);
gradeAch8.setText(null);
gradeWeight8.setText(null);
gradeAch9.setText(null);
gradeWeight9.setText(null);
gradeAch10.setText(null);
gradeWeight10.setText(null);
}
}
public GradeCalculator(){
super("Grade Calculator");
c = getContentPane();
c.setLayout(null);
JPanel northP = new JPanel();
northP.setLayout(null);
northP.setBorder(BorderFactory.createTitledBorder("Enter your grades"));
northP.setSize(330,460);
northP.setLocation(2,0);
c.add(northP);
JLabel score = new JLabel("Grade you recieved..");
score.setSize(130,20);
score.setLocation(20,30);
northP.add(score);
JLabel weight = new JLabel("Weight of the grade..");
weight.setSize(140,20);
weight.setLocation(190,30);
northP.add(weight);
JButton btnClear = new JButton("New Calculation");
btnClear.setSize(150,20);
btnClear.setLocation(90,530);
btnClear.addActionListener(this);
btnClear.setMnemonic(KeyEvent.VK_N);
c.add(btnClear);
}
public static void main(String[] args) {
GradeCalculator app = new GradeCalculator();
app.setSize(350,600);
app.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
app.setVisible(true);
}
}
Upvotes: 1
Views: 79
Reputation: 285405
First General Suggestions:
null
but rather to ""
, the empty String.Now for your problem:
setText(...)
on the wrong references, on JButtons that aren't part of your displayed GUI. Is the listener code in a different class from that of the displayed GUI? Are you misusing inheritance by having the listener code class extend the GUI? Edit
Solution: it's my second point, that you're shadowing your btnClear variable. Don't re-declare it!
e.g.,
public GradeCalculator(){
super("Grade Calculator");
// ... etc...
// **** here ****
JButton btnClear = new JButton("New Calculation");
// .... etc...
}
change to:
public GradeCalculator(){
super("Grade Calculator");
// ... etc...
// **** here ****
btnClear = new JButton("New Calculation");
// .... etc...
}
The reason this is important, by re-declaring the variable in the constructor, you create a new variable that is visible only inside of the constructor. The btnClear field in the class is null since you never initialize it.
Upvotes: 1