Question

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);
}

}

Was it helpful?

Solution

First General Suggestions:

  • First off, your code is begging you to use arrays or ArrayLists. This will simplify your code greatly and make it much easier to enhance, debug, and fix.
  • Don't set the text to null but rather to "", the empty String.
  • Don't set sizes or locations of any components but rather use the layout managers to do this for you.

Now for your problem:

  • I'm guessing here, because your code does not show us your error, but I suspect that you're calling 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?
  • Or does your code have more than one btnClear variable? Are you creating the button with a "shadow" variable, one that is re-declared in a construtor or method while the class field is null?

___________________________________________________________

Please show us more information and code for a more detailed and accurate answer.


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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top