Home Ask Login Register

Developers Planet

Your answer is one click away!

xtechnoflash February 2016

Enter the Loan amount and number of years. Display on the table the Interest, Monthly Payment and total payment

I am having issues with the code below, I have commented out what I am having issues with and the problem in detail but just in case here is the issue at hand. When I click on the Show Table Button, it does not display my calculations in the table, but if I add in a writeline("get working") at certain places in the If statements it works perfect. Im just not sure what I need to do in order to display the calculations on the table.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

 * Developer: 
 * Date: 2/8/2016
 * Purpose:Enter the Loan amount and number of years. Display on the table the Interest, Monthly Payment and total payment.
 *          in the number of years from a text field, and it should display the 
 *          monthly and total payment
public class Exercise17_13 extends JFrame {

    // add Panes, Panels and Labels 
    JScrollPane jScrollPane1 = new JScrollPane();
    JPanel jPanel1 = new JPanel();
    JTextArea jta = new JTextArea();
    TitledBorder titledBorder;
    JLabel jlblNumberYears = new JLabel("Number of Years");
    JLabel jlbLoanAmount = new JLabel("Loan Amount");
    JTextField jtfNumberYears = new JTextField(3);
    JTextField jtfLoanAmount = new JTextField(8);

    ButtonGroup btg = new ButtonGroup();
    JButton jbtShowTable = new JButton();

    public Exercise17_13() {

        //sets a Border around the program
        titledBorder = new TitledBorder("");
        this.setSize(new Dimension(400, 300));

        //add the actionListeners 
        jtfNumberYears.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
            //set the ShowTable button to receive ActionListener
        jbtShowTable.setText("Show Table");


Bombe March 2016

Okay, there are a couple of things wrong with it:

  • What’s up with the if (e.getSource() == ...) statements? That method is only called when the button is pressed, there is no need to check the source of the event. And besides, you’re only executing The Real Magic when the source is two different things. At once.
  • Then you are trying to parse a Double from a String containing three whitespace-separated numbers. What is that even trying to achieve?
  • Then you are also overwriting your text area for every iteration of the “foo” loop. An append (with maybe a newline attached after each line) works better.
  • As for the jbtShowTable.isSelected: I'm not sure why think this would be necessary as the method you’re in is called by the ActionListener registered with the button. If this method gets called somebody has had to push the button. :)

After I made these changes numbers started to appear in the “table.” If they are correct I don’t know but at least you can see them now. :)

Post Status

Asked in February 2016
Viewed 3,743 times
Voted 4
Answered 1 times


Leave an answer

Quote of the day: live life