title: java Clear Table on button click
body: <p>I am writing a Mortgage Calculator for class and I have it working the way I need it to, except everytime I click the "Calculate" button it will just continue to add to the table instead of the table clearing and showing new values. I know my code might look a little sloppy and I have some things commented out that don't need to be there because I'm still working it, but do you have any suggestions?</p>
  
  <p><strong>FYI</strong> I am still a beginner learning Java and it has taken me over 20hrs to get this far (and i"m pretty proud of myself!) Thank you!!</p>
  
  <pre><code>//Import all required Packages
  import javax.swing.*;
  import javax.swing.table.*;
  import java.awt.*;
  import java.text.*;
  import java.awt.event.*;
  
  public class MortgageCalculator extends JFrame implements ActionListener {
  
  
  // Loan Values
  double intPrincipal, interestRate, calcPayment, monthlyInterest, currentInterest, principalPaid, newBalance;
  
  int totalMonths;
  
  double[] loanInterest = {5.35, 5.5, 5.75}; // Yearly interest in decimal form
  int[] loanTerm = {7, 15, 30}; // Total months of term
  String principal;
  String comboArray[] = {"7 Years at 5.35%", "15 Years at 5.5%", "30 Years at 5.75%"};
  int termYears, termMonths, done, i=0, m=0, p=0;
  
  
  
  //Set up panels
  JPanel contentPanel;
  
  //Set up labels
  JLabel mortgageLabel, paymentLabel, termLabel;
  
  //Set up buttons
  JButton calculateButton, clearButton, exitButton;
  
  //TextFields
  JTextField txtMortgage = new JTextField(10);
  JTextField txtPayment = new JTextField(10);
  
  //New Text Area
  JTextArea textarea = new JTextArea();
  
  DecimalFormat df = new DecimalFormat("$###,###.00"); //Formatting the results to decimal form
  
  
  //Combo Box
  JComboBox loansList = new JComboBox();
  
  DefaultTableModel model = new DefaultTableModel();
  JTable table = new JTable(model);
  
  //Build GUI
      public MortgageCalculator()
      {
      super();
      initializeContent();
      }
  
      public void initializeContent()
                  {
                      this.setSize(700, 500);
                      this.setLocation(0, 0);
                      this.setContentPane(contentPanel());
                      this.setTitle("Mortgage Calculator");
                  }
  
      public JPanel contentPanel()
                  {
  
                      contentPanel = new JPanel();
                      contentPanel.setLayout(null);
  
  
                      //Add labels to the  panel
                      mortgageLabel = new JLabel("Mortgage:");
                      mortgageLabel.setLocation(200, 30);
                      mortgageLabel.setSize(100, 25);
                      contentPanel.add(mortgageLabel);
  
                      termLabel = new JLabel("Term &amp; Rate:");
                      termLabel.setLocation(183, 55);
                      termLabel.setSize(100, 30);
                      contentPanel.add(termLabel);
  
                      paymentLabel = new JLabel("Monthly Payment:");
                      paymentLabel.setLocation(158, 85);
                      paymentLabel.setSize(100, 30);
                      contentPanel.add(paymentLabel);
  
                      //Text Fields
  
                      txtMortgage = new JTextField(10);
                      txtMortgage.setLocation(280, 30);
                      txtMortgage.setSize(150, 25);
                      contentPanel.add(txtMortgage);
  
                      txtPayment = new JTextField(10);
                      txtPayment.setLocation(280, 85);
                      txtPayment.setSize(150, 25);
                      contentPanel.add(txtPayment);
  
  
                      //Combo Box
  
                      loansList.addItem(comboArray[0]);
                      loansList.addItem(comboArray[1]);
                      loansList.addItem(comboArray[2]);
                      loansList.setLocation(280, 55);
                      loansList.setSize(150, 25);
                      loansList.addActionListener(this);
                      contentPanel.add(loansList);
  
  
                      //textarea.setPreferredSize(new Dimension(650, 300));
  
  
                       //JScrollPane scroller = new JScrollPane(textarea);
                       JScrollPane scroller = new JScrollPane(table);
                       contentPanel.add(scroller);
                       scroller.setSize(650,300);
                       scroller.setLocation(20, 150);
  
  
                       textarea.setLineWrap(true);
  
                      model.addColumn("Payment Number");
                      model.addColumn("Current Interest");
                      model.addColumn("Principal Paid");
                      model.addColumn("New Balance");
  
  
  
                      //Buttons
                      exitButton = new JButton("Exit");
                      exitButton.setLocation(450, 30);
                      exitButton.setSize(100, 25);
                      contentPanel.add(exitButton);
  
                      clearButton = new JButton("Clear");
                      clearButton.setLocation(450, 55);
                      clearButton.setSize(100, 25);
                      contentPanel.add(clearButton);
  
  
                      calculateButton = new JButton("Calculate");
                      calculateButton.setLocation(450, 85);
                      calculateButton.setSize(100, 25);
                      contentPanel.add(calculateButton);
  
                      //setup up buttons
                      calculateButton.addActionListener(this);
                      clearButton.addActionListener(this);
                      exitButton.addActionListener(this);
  
  
                      return contentPanel;
  
                  }
  
  
  
  
      //Define actions performed for buttons
      public void actionPerformed(ActionEvent e)
      {
  
              String arg = e.getActionCommand();
              if (e.getSource() == loansList) {
                  switch (loansList.getSelectedIndex()) {
                          case 0:
                                  i = 0;
                                  break;
                          case 1:
                                  i = 1;
                                  break;
                          case 2:
                                  i = 2;
                                  break;
                  }
              }
  
              if (arg == "Calculate")
              {
  
                  txtPayment.setText("");
                  principal = txtMortgage.getText();
                  try {
                              intPrincipal = Double.parseDouble(principal);
                              if (intPrincipal &lt;= 0) throw new NumberFormatException();
                      }
                      catch(NumberFormatException n){
                              txtPayment.setText("Please Enter a Postive Numeric Number");
                              done = 1;
                      }
                  if (done == 1)
                          done = 0;
                  else {
  
                              interestRate = loanInterest[i];
                              termYears = loanTerm[i];
                              monthlyInterest = interestRate/(12*100); //calculates monthly interest
                              termMonths = termYears*12; //calculates term length in months
                              calcPayment = monthlyInterest*intPrincipal/(1-Math.pow((1+monthlyInterest), -termMonths)); //calculates monthly payment
                              txtPayment.setText(" " + df.format(calcPayment));
  
                              for (m=0; m&lt;=totalMonths; m++) {
                                  totalMonths = loanTerm[i]*12;
                                  currentInterest = intPrincipal * monthlyInterest;
                                  principalPaid = calcPayment - currentInterest;
                                  newBalance = intPrincipal - principalPaid;
                                  intPrincipal = newBalance;
  
                                 /* printAndAppend(
                                  (m+1) + "            " +
                                  df.format(currentInterest) + "                        " +
                                  df.format(principalPaid) + "            " +
                                  df.format(newBalance) + "\n");
                                  //textarea.setText(df.format(currentInterest));
  
                                  if(intPrincipal &lt;= 1){ break;}*/
  
  
                                  // Create a couple of columns
  
  
                                  model.addRow(new Object[]{m+1, df.format(currentInterest), df.format(principalPaid), df.format(newBalance)});
  
                                  if(intPrincipal &lt;= 1){ break;}
  
  
  
                              }
  
  
              }
      }
  
              else if (e.getSource() == clearButton)
              {
                  txtMortgage.setText(""); //clear Mortgage textfield
                  txtPayment.setText(""); //clear Payment textfield
                  txtMortgage.requestFocusInWindow(); //move cursor back to Mortgage textfield
                  loansList.setSelectedIndex(0);
              }
  
              else if (e.getSource() == exitButton)
                  System.exit(0);
          }
  
      public void printAndAppend(String text) {
          textarea.append(text);
      }
  
      public static void main(String[] args)
      {
          new MortgageCalculator().setVisible(true);
      }
  
  }
  </code></pre>