Results 1 to 2 of 2
  1. #1
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    164
    Rep Power
    2

    Default Amortization JTable

    Hello everyone,

    I am creating an amortization schedule calculator and I'm having a bit of trouble with adding my table to the screen after I calculate everything.

    Here's my code. The problem is that in the calculatePayments() method, I want the JTable to be added to the JScrollPane but it doesn't seem to be working. Thank you very much for taking a look at this.

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.JTextField;
    import javax.swing.ScrollPaneConstants;
    
    
    public class Main extends JPanel implements ActionListener {
    	private static final long serialVersionUID = 1L;
    
    	public static final int WIDTH = 250;
    	public static final int HEIGHT = 500;
    
    	private GridBagLayout layout;
    	private GridBagConstraints gbc;
    
    	private JTextField amountF, interestF, lengthF;
    	private JLabel amountL, interestL, lengthL;
    	private JButton calcButton;
    
    	private JScrollPane pane;
    	private JTable table;
    
    	private double amount;
    	private double interestRate;
    	private double numMonths;
    	
    	private Object[][] data;
    	
    	private String[] columnNames = {"Payment #", "Interest", "Principal Paid", "Amount Left"};
    
    	// main constructor: creation of JPanel
    	public Main() {
    		setPreferredSize(new Dimension(WIDTH, HEIGHT));
    
    		init();
    	}
    
    	// init method: creates all components to add to panel and adds them
    	private void init() {
    		layout = new GridBagLayout();
    		layout.columnWidths = new int[] {150, 100};
    		layout.rowHeights = new int[] {50, 50, 50, 50, 100, 100, 100};
    		setLayout(layout);
    
    		gbc = new GridBagConstraints();
    
    		amountL = new JLabel("Amount of Money:");
    		gbc.gridx = 0;
    		gbc.gridy = 0;
    		gbc.insets = new Insets(0, -35, 0, 0);
    		add(amountL, gbc);
    
    		interestL = new JLabel("Interest %:");
    		gbc.gridx = 0;
    		gbc.gridy = 1;
    		gbc.insets = new Insets(0, -78, 0, 0);
    		add(interestL, gbc);
    
    		lengthL = new JLabel("Length of Payment:");
    		gbc.gridx = 0;
    		gbc.gridy = 2;
    		gbc.insets = new Insets(0, -30, 0, 0);
    		add(lengthL, gbc);
    
    		amountF = new JTextField();
    		gbc.gridx = 1;
    		gbc.gridy = 0;
    		gbc.fill = GridBagConstraints.HORIZONTAL;
    		gbc.insets = new Insets(0, -40, 0, 5);
    		add(amountF, gbc);
    
    		interestF = new JTextField();
    		gbc.gridy = 1;
    		gbc.insets = new Insets(0, -80, 0, 5);
    		add(interestF, gbc);
    
    		lengthF = new JTextField();
    		gbc.gridy = 2;
    		gbc.insets = new Insets(0, -35, 0, 5);
    		add(lengthF, gbc);
    
    		calcButton = new JButton("Calculate");
    		calcButton.addActionListener(this);
    		gbc.gridx = 0;
    		gbc.gridy = 3;
    		gbc.gridwidth = 2;
    		gbc.insets = new Insets(5, 5, 5, 5);
    		add(calcButton, gbc);
    
    		pane = new JScrollPane();
    		pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
    		pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    		gbc.gridx = 0;
    		gbc.gridy = 4;
    		gbc.gridwidth = 2;
    		gbc.gridheight = 3;
    		gbc.fill = GridBagConstraints.BOTH;
    		add(pane, gbc);
    
    	}
    
    	public void calculatePayments(double amt, double intRate, double months) {
    		double r = intRate / 12 / 100;
    		double p = amt;
    		double n = months;
    
    		data = new Object[(int)n][4];
    		
    		double exp = 1;
    
    		for(int i = 0; i < n; i++) {
    			exp *= (1 + r);
    		}
    
    		double monthlyPayment = (r * p * exp) / (exp - 1);
    
    		for(int i = 1; i <= n; i++) {
    
    			double intPaid = p * r;
    			double prinPaid = monthlyPayment - intPaid;
    			p -= prinPaid;
    			
    			data[i - 1][0] = i;
    			data[i - 1][1] = intPaid;
    			data[i - 1][2] = prinPaid;
    			data[i - 1][3] = p;
    		}
    		
    		table = new JTable(data, columnNames);
    		pane.add(table);
    	}
    
    	// start of program, JFrame with BorderLayout containing instance of Main
    	public static void main(String[] args) {
    		JFrame frame = new JFrame();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setResizable(false);
    		frame.setLayout(new BorderLayout());
    		frame.add(new Main(), BorderLayout.CENTER);
    		frame.pack();
    		frame.setLocationRelativeTo(null);
    		frame.setVisible(true);
    	}
    
    	public void actionPerformed(ActionEvent e) {
    		if(e.getSource() == calcButton) {
    			amount = Double.parseDouble(amountF.getText());
    			interestRate = Double.parseDouble(interestF.getText());
    			numMonths = Double.parseDouble(lengthF.getText()) * 12;
    
    			calculatePayments(amount, interestRate, numMonths);
    		}
    	}
    
    }

  2. #2
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    164
    Rep Power
    2

    Default Re: Amortization JTable

    Never mind I found a solution!

    What I had to do was this:

    Java Code:
    pane.getViewport().add(table);

Similar Threads

  1. Amortization Schedule Table Help.
    By pj6444 in forum New To Java
    Replies: 12
    Last Post: 01-14-2013, 05:20 AM
  2. Jtable inside Jtable
    By anchitanc in forum AWT / Swing
    Replies: 5
    Last Post: 05-26-2012, 08:10 PM
  3. Replies: 4
    Last Post: 04-02-2012, 07:57 PM
  4. Adding New JTable in JTable
    By anilkumar_vist in forum New To Java
    Replies: 0
    Last Post: 01-27-2010, 08:27 AM
  5. Replies: 2
    Last Post: 05-15-2008, 10:15 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
  •