Results 1 to 7 of 7
- 08-16-2011, 04:06 AM #1
Member
- Join Date
- Aug 2011
- Posts
- 4
- Rep Power
- 0
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] }
-
I can't see anything wrong with your code on quick review of it, other than this line being redundant and thus unnecessary:
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?Java Code:tblAmortization.setModel(model);
Oh, and welcome to the forum!!
- 08-16-2011, 04:19 AM #3
Member
- Join Date
- Aug 2011
- Posts
- 4
- Rep Power
- 0
Okey day.
Thanks for the quick reply and the welcome. :)
I guess it's time for debugging then...lol.
- 08-16-2011, 04:40 AM #4
Member
- Join Date
- Aug 2011
- Posts
- 4
- Rep Power
- 0
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?
-
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(); } }); } }
- 08-16-2011, 05:31 AM #6
Member
- Join Date
- Aug 2011
- Posts
- 4
- Rep Power
- 0
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 :)
- 08-16-2011, 09:17 AM #7
Similar Threads
-
AWT editor with drag and drop...
By N00Bie in forum New To JavaReplies: 0Last Post: 02-16-2011, 08:03 PM -
Drag and Drop within JTable
By mine0926 in forum New To JavaReplies: 1Last Post: 09-07-2010, 09:57 AM -
Drag and Drop in JTable
By mahen.1981 in forum Advanced JavaReplies: 0Last Post: 11-27-2009, 11:37 AM -
Drag and Drop Issue
By kirly in forum Advanced JavaReplies: 1Last Post: 04-16-2009, 05:04 AM -
Drag&Drop of a file in an editor
By orl132 in forum SWT / JFaceReplies: 1Last Post: 07-05-2008, 11:57 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks