Results 1 to 7 of 7
  1. #1
    Ceryph is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Default NetBeans GUI editor (drag/drop) and JTable data issue

    OK, so I've been trying to do some searching and I'm getting a bit confused.
    It's hard to find examples that address issues specifically when using the prebuilt components in NetBeans.

    I have added a Table to my GUI. I then went to the "model" property and removed the rows and one of the columns and then customized my column names.

    There is a single button that affects all of this code. This is a school project, so it's rather simple. I'm just having trouble getting my JTable to be updated after I add new rows.
    I've tried various things people have mentioned on forums, but just can't seem to get my Table to update. When I run my program, I don't get any errors when I click on my button, it just calculates the Monthly Payment, and does nothing with the Table. I'm hoping I'm even updating a row properly and that I'm simply having troubles with the Table displaying it.

    So, here is my code with the lines bolded that are related to the table; hopefully someone can point in me in the correct direction:

    Java Code:
    [B]import javax.swing.table.DefaultTableModel;[/B]
    
        private void btnCalculateActionPerformed(java.awt.event.ActionEvent evt) {                                             
            double principleValue = 200000.00;
            
            double[] APR = {5.35, 5.5, 5.75};
            int[] termLength = {7, 15, 30};
            
            int i = cboLoans.getSelectedIndex();
    
            double interestRate = APR[i] / 100 / 12;        
            int notePeriod = termLength[i] * 12;
            
            double monthlyPayment = calculateMonthlyPayment(principleValue, interestRate, notePeriod);
            
            this.txtMonthlyPayment.setText(Currency.format(monthlyPayment));
            
            //Initialize values for Amortization Table
            double loanBalance = notePeriod * monthlyPayment;
            double principalBalance = principleValue;
            double interestBalance = loanBalance - principalBalance;
            double totalPrincipalPaid = 0;
            double totalInterestPaid = 0;
            
            [B]DefaultTableModel model = (DefaultTableModel) tblAmortization.getModel();[/B]
            
            for (int x = 1; x < notePeriod + 1; x++) {
                //The portion of the payment that goes to interest is based on the remaining principal balance.
                double interestPaidThisMonth = interestRate * principalBalance;
    
                //The rest of the payment goes to principal.
                double principalPaidThisMonth = monthlyPayment - interestPaidThisMonth;
    
                //Update the remaining balances and total paid values
                totalPrincipalPaid += principalPaidThisMonth;
                principalBalance -= principalPaidThisMonth;
                totalInterestPaid += interestPaidThisMonth;
                interestBalance -= interestPaidThisMonth;         
                loanBalance -= monthlyPayment;
    
                //Add values to JTable
                [B]Object[] newRowData = {x, "$" + Currency.format(totalInterestPaid), "$" + Currency.format(loanBalance)};[/B]
                [B]model.addRow(newRowData);[/B]
                
            }
            [B]tblAmortization.setModel(model);[/B]
        }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I can't see anything wrong with your code on quick review of it, other than this line being redundant and thus unnecessary:

    Java Code:
    tblAmortization.setModel(model);
    Because you're already using the model from that table as the tablemodel that you update. So it seems that it is time for you to do some debugging. Sprinkle your code with println statements to make sure that code you think is being called is in fact being called, and to print out the value of various variables to see if their value is what you expect them to be. Also is the tblAmortization being displayed or could it be shadowed by a different JTable that is in fact being displayed?

    Oh, and welcome to the forum!!

  3. #3
    Ceryph is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Default

    Okey day.
    Thanks for the quick reply and the welcome. :)
    I guess it's time for debugging then...lol.

  4. #4
    Ceryph is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Default

    Well, I definitely only have one JTable on my form.
    I added this line of code just before the newRowData variable, and it displayed the expected values:
    JOptionPane.showMessageDialog(null, x + " : $" + Currency.format(totalInterestPaid) + " : $" + Currency.format(loanBalance));

    I'm really confused now as to why this isn't showing if it seemingly looks correctly coded.

    I'm not really sure what else to check. Am I definitely assigning/manipulating the model correctly?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Make sure that the structure of your table's model matches that of the data that you are entering in to it. For instance, does your JTable's model currently have 3 columns?

    Then you can make a minimal sample program to test this very method. I've done this for you, and if your table model is correct, the method works fine:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.text.NumberFormat;
    import javax.swing.*;
    import javax.swing.table.DefaultTableModel;
    
    public class FooSwing001 extends JPanel {
    
       private static final Object[] COLUMN_NAMES = {"Index", "Interest", "Balance"};
       private JComboBox cboLoans = new JComboBox(new String[]{"A", "B", "C"});
       private DefaultTableModel model = new DefaultTableModel(COLUMN_NAMES, 0);
       private JTable tblAmortization = new JTable(model);
       private JTextField txtMonthlyPayment = new JTextField(10);
       private NumberFormat currency = NumberFormat.getCurrencyInstance();
    
       public FooSwing001() {
          JButton calcButton = new JButton("Calculate");
          calcButton.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                btnCalculateActionPerformed(e);
             }
          });
          JPanel topPanel = new JPanel();
          topPanel.add(cboLoans);
          topPanel.add(txtMonthlyPayment);
          topPanel.add(calcButton);
    
          setLayout(new BorderLayout());
          add(new JScrollPane(tblAmortization), BorderLayout.CENTER);
          add(topPanel, BorderLayout.NORTH);
       }
    
       private void btnCalculateActionPerformed(java.awt.event.ActionEvent evt) {
          double principleValue = 200000.00;
    
          double[] APR = {5.35, 5.5, 5.75};
          int[] termLength = {7, 15, 30};
    
          int i = cboLoans.getSelectedIndex();
    
          double interestRate = APR[i] / 100 / 12;
          int notePeriod = termLength[i] * 12;
    
          double monthlyPayment = calculateMonthlyPayment(principleValue,
                   interestRate, notePeriod);
    
          this.txtMonthlyPayment.setText(currency.format(monthlyPayment));
    
          // Initialize values for Amortization Table
          double loanBalance = notePeriod * monthlyPayment;
          double principalBalance = principleValue;
          double interestBalance = loanBalance - principalBalance;
          double totalPrincipalPaid = 0;
          double totalInterestPaid = 0;
    
          DefaultTableModel model = (DefaultTableModel) tblAmortization.getModel();
    
          for (int x = 1; x < notePeriod + 1; x++) {
             System.out.println("x = " + x);
             // The portion of the payment that goes to interest is based on the
             // remaining principal balance.
             double interestPaidThisMonth = interestRate * principalBalance;
    
             // The rest of the payment goes to principal.
             double principalPaidThisMonth = monthlyPayment - interestPaidThisMonth;
    
             // Update the remaining balances and total paid values
             totalPrincipalPaid += principalPaidThisMonth;
             principalBalance -= principalPaidThisMonth;
             totalInterestPaid += interestPaidThisMonth;
             interestBalance -= interestPaidThisMonth;
             loanBalance -= monthlyPayment;
    
             // Add values to JTable
             // Object[] newRowData = {x, "$" + currency.format(totalInterestPaid),
             // "$" + currency.format(loanBalance)};
             Object[] newRowData = {x, currency.format(totalInterestPaid),
                currency.format(loanBalance)};
             model.addRow(newRowData);
    
          }
          // !! tblAmortization.setModel(model);
    
       }
    
       private double calculateMonthlyPayment(double principleValue,
                double interestRate, int notePeriod) {
          // TODO: Make a real calculation
          return 500; // simplified method
       }
    
       private static void createAndShowUI() {
          JFrame frame = new JFrame("FooSwing001");
          frame.getContentPane().add(new FooSwing001());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }

  6. #6
    Ceryph is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Default

    OK, so I don't know what was wrong, but I deleted the JTable I had, and added a new one, named it tblAmortization and everything started working.
    Not sure what happened...but thanks lol :)

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default

    Quote Originally Posted by Ceryph View Post
    ... I don't know what was wrong ... Not sure what happened ...
    Which is exactly why someone new to GUI coding shouldn't be using a visual designer. The next time you face a problem that seems similar, deleting and adding a component may not help, and you'll still not know what happened.

    db

Similar Threads

  1. AWT editor with drag and drop...
    By N00Bie in forum New To Java
    Replies: 0
    Last Post: 02-16-2011, 08:03 PM
  2. Drag and Drop within JTable
    By mine0926 in forum New To Java
    Replies: 1
    Last Post: 09-07-2010, 09:57 AM
  3. Drag and Drop in JTable
    By mahen.1981 in forum Advanced Java
    Replies: 0
    Last Post: 11-27-2009, 11:37 AM
  4. Drag and Drop Issue
    By kirly in forum Advanced Java
    Replies: 1
    Last Post: 04-16-2009, 05:04 AM
  5. Drag&Drop of a file in an editor
    By orl132 in forum SWT / JFace
    Replies: 1
    Last Post: 07-05-2008, 11:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •