I am not able to click on the menu choices and have them fill out the form. I also am not certain that it is reading from the text file. Can someone check and let me know what I need to change in this program?

import java.awt.*;
import java.awt.event.*;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.font.*;
import java.awt.Font;
import java.awt.geom.*;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.Reader;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.io.FileReader;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;


public class Mortgage5 extends JFrame implements ActionListener
{
     JLabel Llabel; //amount label
     JTextField Ltextfield;//amount textfield
     JLabel Olabel;//option label
     JComboBox options;//option combobox
     JLabel Tlabel;//term label
     JTextField Ttextfield;//term textfield
     JLabel Rlabel;//rate label
     JTextField Rtextfield;//rate textfield
     JLabel Plabel;  //payment label
     JLabel $label;  //field for monthly payment amount
     JButton calculate;  //calculate button
     JButton reset;  //reset button
     JButton end;  //end button
     JTable table;//create table
     JMenuItem mnuExit = new JMenuItem("Exit");
     DefaultTableModel model;//table model
     int[] trmArray = {7,15,30};
     double[] intrstArray = {5.35,5.5,5.75};
     JButton graph;
     private float[]  yearlyPrinciple;
     private float[]  yearlyInterest;

     // header
   public Mortgage5()
     {
          super("Mortgage Payment Calculator");
          setDefaultLookAndFeelDecorated(true);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          load();
          init();
          pack();
          setVisible(true);


          // create a instance of a menu
        MenuBar mnuMyBar = new MenuBar();
        setMenuBar(mnuMyBar);


        // constructs File menu and populates
        Menu mnuMyFile = new Menu("File",true);
        mnuMyBar.add(mnuMyFile);
            MenuItem mnuMyFileExit = new MenuItem("Exit");
            mnuMyFile.add(mnuMyFileExit);

        // constructs File menu and populates
        Menu mnuAvailLoans = new Menu("Available Loans", true);
        mnuMyBar.add(mnuAvailLoans);
            MenuItem mnuAvailLoans1 = new MenuItem("5.35% Interest at 7 Years");
            mnuAvailLoans.add(mnuAvailLoans1);
            MenuItem mnuAvailLoans2 = new MenuItem("5.50% Interest at 15 Years");
            mnuAvailLoans.add(mnuAvailLoans2);
            MenuItem mnuAvailLoans3 = new MenuItem("5.75% Interest at 30 Years");
            mnuAvailLoans.add(mnuAvailLoans3);

        // add ActionListener to menu items
        mnuMyFileExit.addActionListener(this);
        mnuAvailLoans1.addActionListener(this);
        mnuAvailLoans2.addActionListener(this);
        mnuAvailLoans3.addActionListener(this);

        // add ActionCommand to menu items
        mnuMyFileExit.setActionCommand("Exit");
        mnuAvailLoans1.setActionCommand("Loans1");
        mnuAvailLoans2.setActionCommand("Loans2");
        mnuAvailLoans3.setActionCommand("Loans3");

            mFrame.setVisible(true);

               //exit listener
                mnuExit.addActionListener(this);  //exitButton

     }
            //reading from sequential file
  public void load()
     {
          Reader fis = null;
          if(fis == null)
              return;
          {
            try
               {

                   fis = new FileReader("rates.txt");

                    BufferedReader b = new BufferedReader( fis );

                    String[] line = b.readLine(  ).split(",");
                    trmArray = new int[line.length];
                    for ( int i = 0; i < line.length; i++ )
                    {
                         trmArray[ i ] = Integer.parseInt(line[i].trim());
                    }

                    line = b.readLine(  ).split(",");
                    intrstArray = new double[line.length];
                    for ( int i = 0; i < line.length; i++ )
                    {
                         intrstArray[ i ] = Double.parseDouble(line[i].trim());
                    }

               b.close();
               fis.close();
               }

               catch ( Exception e1 )
               {
                    e1.printStackTrace(  );
               }
            }
     }
     //creates labels, buttons and textfields
  public void init()
     {
          MortgageLayout customLayout = new MortgageLayout();

          Container con = getContentPane();
          con.setLayout(customLayout);

          con.setFont(new Font("Arial", Font.PLAIN, 12));
          con.setLayout(customLayout);

          Llabel = new JLabel("Mortgage Loan Amount $ (no comma)");//amount label
          con.add(Llabel);

          Ltextfield = new JTextField("");//amount textfield
          con.add(Ltextfield);

          Olabel = new JLabel("Preset Term & Interest Rate %");//option label
          con.add(Olabel);

          options = new JComboBox();
          con.add(options);

          options.addItem(" (choose rate)");
          options.addItem("7 years at 5.35%");
              options.addItem("15 years at 5.5%");
              options.addItem("30 years at 5.75%");
          options.setEnabled(true);

          {
                options.addItem("");
          }

          for (int i = 0; i < trmArray.length; i++)
          {
               options.addItem("");
          }

          Tlabel = new JLabel("Term (years)");//term label
          con.add(Tlabel);

          Ttextfield = new JTextField("");//term textfield
          con.add(Ttextfield);

          Rlabel = new JLabel("Interest Rate");//rate label
          con.add(Rlabel);

          Rtextfield = new JTextField("");//rate textfield
          con.add(Rtextfield);

          Plabel = new JLabel("Monthly Payment Amount");//payment label
          con.add(Plabel);

          $label = new JLabel("");//payment textfield
          con.add($label);

          calculate = new JButton("Calculate");//calculate button
          con.add(calculate);
          calculate.setBackground(Color.green);

          reset = new JButton("Reset");//reset button
          con.add(reset);
          reset.setBackground(Color.yellow);

          end = new JButton ("End");//end button
          con.add(end);
          end.setBackground(Color.red);

                    //table header names
          String[] columnNames = {"Payment #","Payment Amount", "Interest", "Principle Reduction",
                                             "Remaining Balance"};

          //create table and table model
          model = new DefaultTableModel(columnNames, 0);
          table = new JTable(model);
          JScrollPane scroll = new JScrollPane(table);
          table.setPreferredScrollableViewportSize(new Dimension (10, 600));
          con.add (scroll);

          graph = new JButton ("Display Graph");//Display Graph button
          con.add(graph);
          graph.setBackground(Color.blue);

          //action listeners
          Ltextfield.addActionListener(this); //loanfield
          options.addActionListener(this); //interestfield
          calculate.addActionListener(this);  //calButtion
          reset.addActionListener(this); //resetButton
          end.addActionListener(this);  //endButton
          graph.addActionListener(this);  //graphButton
     }


     //action event from listeners
     public void actionPerformed(ActionEvent event)
     {
     Object source = event.getSource();
          if (source == calculate)
          {
               startCalculations();
          }

          if (source == reset)
          {
               reset();
          }

          if (source==options)
          {
               setRate();
          }

          if (source == end)
          {
               exit();
          }

          if (source == mnuExit)
          {
            exitGraph();
           }
          if (source == graph)
          {
               mFrame = new JFrame("Mortgage Graph");
               mFrame.getContentPane().add(new GraphP(yearlyPrinciple, yearlyInterest));
               mFrame.setSize(800,600);
               mFrame.setLocation(200,100);

             // create a instance of a menu
        MenuBar mnuMyBar = new MenuBar();
        setMenuBar(mnuMyBar);


        // constructs File menu and populates
        Menu mnuMyFile = new Menu("File",true);
        mnuMyBar.add(mnuMyFile);
            MenuItem mnuMyFileExit = new MenuItem("Exit");
            mnuMyFile.add(mnuMyFileExit);

           mFrame.setVisible(true);

               //exit listener
                mnuExit.addActionListener(this);  //exitButton
          }
        }
          public JFrame mFrame = new JFrame();


void exitGraph()
{

             mFrame.dispose();
             mFrame = null;
          }

          void setRate()
     {
          int index = options.getSelectedIndex();

          //term and interest error check
          if (index > 0)
          {
               try
               {
                    Ttextfield.setText(Integer.toString(trmArray[index-1]));
               }

               catch (NumberFormatException e)
               {
                    JOptionPane.showMessageDialog(null, "Invalid or missing Loan Term.  Please try again!",
                                                            "Message Dialog", JOptionPane.PLAIN_MESSAGE);
                    Ttextfield.setText(null);
               }

               try
               {
                    Rtextfield.setText(Double.toString(intrstArray[index-1]));
               }

               catch (NumberFormatException e)
               {
                    JOptionPane.showMessageDialog(null, "Invalid or missing Interest Rate.  Please try again!",
                                                       "Message Dialog", JOptionPane.PLAIN_MESSAGE);
                    Rtextfield.setText(null);
               }
          }
     }

     //calculation section
     void startCalculations()
     {
          Thread thisThread = Thread.currentThread();
          NumberFormat currency = NumberFormat.getCurrencyInstance();

          double amt = 0;
          double trm = 0;
          double intrst = 0;
          double moIn = 0;
          double moTrm = 0;
          double prin = 0;
          double paymt = 0;

          //amount error check
          try
          {
               amt = Double.parseDouble(Ltextfield.getText());
          }

          catch (NumberFormatException e)
          {
               JOptionPane.showMessageDialog(null, "Missing Amount or Use of Commas",
                                             "Message Dialog", JOptionPane.PLAIN_MESSAGE);
               Ltextfield.setText(null);
               Ttextfield.setText(null);
               Rtextfield.setText(null);
               options.setSelectedIndex(0);
          }

          //term and interest error check
          try
          {
               trm = Double.parseDouble(Ttextfield.getText());

          }

          catch (NumberFormatException e)
          {
               JOptionPane.showMessageDialog(null, "Invalid or missing Loan Term.  Please try again!",
                                                  "Message Dialog", JOptionPane.PLAIN_MESSAGE);
               Ttextfield.setText(null);
          }

          try
          {
               intrst = Double.parseDouble(Rtextfield.getText());
          }

          catch (NumberFormatException e)
          {
               JOptionPane.showMessageDialog(null, "Invalid or missing Interest Rate.  Please try again!",
                                             "Message Dialog", JOptionPane.PLAIN_MESSAGE);
               Rtextfield.setText(null);
          }

          if (amt > 0)
          {
               amt = Double.parseDouble(Ltextfield.getText());
               moIn = (intrst / 1200);//monthly interest rate
               moTrm = trm * 12;//number of payments
               paymt = (amt * moIn) / (1-Math.pow((1+moIn), -moTrm));//amount forumla
                  yearlyPrinciple = new float[(int)trm]; // initialize the arrays to store yearly principle and interest
                  yearlyInterest = new float[(int)trm];

               $label.setText("" + currency.format(paymt));

               double newPrin = amt;

               for (int i = 0; i < trm; i++)
               {
                       yearlyInterest[i] = 0.0f; //start at 0.0
                         yearlyPrinciple[i] = 0.0f; //start at 0.0
                       for(int j = 1; j <=12; j++)
                         {
                    double newIn = moIn * newPrin;//monthly interest amount
                    double reduct = paymt - newIn;//monthly principle
                         yearlyInterest[i] += newIn; // accumalate the interest
                         yearlyPrinciple[i] += reduct; //accumalate the principle
                         newPrin = newPrin - reduct;//balance

                    //stops showing negative balance
                    if (newPrin < 0)
                         newPrin = 0;
                    else
                         newPrin = newPrin;

                    //inserts different amounts into the table
                    model.addRow(new Object[] { Integer.toString((i*12) + j), currency.format(paymt),
                    currency.format(newIn), currency.format(reduct), currency.format(newPrin) });
                         }
                         //for testing purposes only
//                         model.addRow(new Object[] { Integer.toString(i), currency.format(0.0),
//                    currency.format(yearlyInterest[i]), currency.format(yearlyPrinciple[i]), currency.format(0.0) });
               }
          }

          //less than 0 error check
          if (amt < 0)
          {
               JOptionPane.showMessageDialog(null, "Please Enter Positive Numbers Only.",
                                             "Message Dialog", JOptionPane.PLAIN_MESSAGE);
               Ltextfield.setText(null);
          }

     }

     //resets all fields
     void reset()
     {
          Ltextfield.setText(null);
          Ttextfield.setText(null);
          Rtextfield.setText(null);
          options.setSelectedIndex(0);
          $label.setText(null);
          model.setRowCount(0);
     }

     //exit the program with thank you message
     void exit()
     {
          JOptionPane.showMessageDialog(null, "          Thank you for using \n Mortgage Calculator",
                                             "Message Dialog", JOptionPane.PLAIN_MESSAGE);

          System.exit(0);
     }

     public static void main(String args[])
     {
       
          EventQueue.invokeLater(new Runnable()
          {
               public void run()
               {

                  new Mortgage5().setVisible(true);
               }
          });
     }
}

//creates class for container layout and placement
class MortgageLayout implements LayoutManager{
     public MortgageLayout() {}
     
     public void addLayoutComponent(String name, Component comp) {}

     public void removeLayoutComponent(Component comp) {}

     public Dimension preferredLayoutSize(Container parent)
     {
          Dimension dim = new Dimension(0, 0);
          Insets insets = parent.getInsets();
          dim.width = 600 + insets.left + insets.right;
          dim.height = 425 + insets.top + insets.bottom;

          return dim;
     }

     public Dimension minimumLayoutSize(Container parent)
     {
          Dimension dim = new Dimension(0, 0);

          return dim;
     }

     public void layoutContainer(Container parent)
     {
          Insets insets = parent.getInsets();

          Component c;
          c = parent.getComponent(0);
               if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+8,250,24);}
          c = parent.getComponent(1);
               if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+8,175,24);}
          c = parent.getComponent(2);
               if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+40,250,24);}
          c = parent.getComponent(3);
               if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+40,150,24);}
          c = parent.getComponent(4);
               if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+72,250,24);}
          c = parent.getComponent(5);
               if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+72,96,24);}
          c = parent.getComponent(6);
               if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+104,250,24);}
          c = parent.getComponent(7);
               if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+104,112,24);}
          c = parent.getComponent(8);
               if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+136,250,24);}
          c = parent.getComponent(9);
               if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+136,112,24);}
          c = parent.getComponent(10);
               if (c.isVisible()) {c.setBounds(insets.left+50,insets.top+168,96,24);}
          c = parent.getComponent(11);
               if (c.isVisible()) {c.setBounds(insets.left+225,insets.top+168,112,24);}
          c = parent.getComponent(12);
               if (c.isVisible()) {c.setBounds(insets.left+400,insets.top+168,96,24);}
          c = parent.getComponent(13);
          if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+200,575,160);}
          c = parent.getComponent(14);
          if (c.isVisible()) {c.setBounds(insets.left+225,insets.top+375,112,24);}
     }
}

class GraphP extends JPanel
{
    final int
        HPAD = 60,
        VPAD = 40;
    int[] data;
    Font font;
     float[] principleData;
     float[] interestData;


    public GraphP(float[] p, float[] i)
    {

     principleData = p;
     interestData = i;

        font = new Font("lucida sans regular", Font.PLAIN, 16);
        setBackground(Color.white);
    }

    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setFont(font);
        FontRenderContext frc = g2.getFontRenderContext();
        int w = getWidth();
        int h = getHeight();
        // scales
        float xInc = (w - HPAD - VPAD) / (interestData.length - 1);//11f;  //distance between each plot
        float yInc = (h - 2*VPAD) / 10f;
        int[] dataVals = getDataVals();        //min and max values for y-axis
        float yScale = dataVals[2] / 10f;

        // ordinate (y - axis)
        g2.draw(new Line2D.Double(HPAD, VPAD, HPAD, h - VPAD));
        // plot tic marks
        float x1 = HPAD, y1 = VPAD, x2 = HPAD - 3, y2;
        for(int j = 0; j < 10; j++)
        {
            g2.draw(new Line2D.Double(x1, y1, x2, y1));
            y1 += yInc;
        }
        // labels
        String text; LineMetrics lm;
        float xs, ys, textWidth, height;
        for(int j = 0; j <= 10; j++)
        {
            text = String.valueOf(dataVals[1] - (int)(j * yScale));
            textWidth = (float)font.getStringBounds(text, frc).getWidth();
            lm = font.getLineMetrics(text, frc);
            height = lm.getAscent();
            xs = HPAD - textWidth - 7;
            ys = VPAD + (j * yInc) + height/2;
            g2.drawString(text, xs, ys);
        }

        // abcissa (x - axis)
        g2.draw(new Line2D.Double(HPAD, h - VPAD, w - VPAD, h - VPAD));
        // tic marks
        x1 = HPAD; y1 = h - VPAD; y2 = y1 + 3;
        for(int j = 0; j < interestData.length; j++)
        {
            g2.draw(new Line2D.Double(x1, y1, x1, y2));
            x1 += xInc;
        }
        // labels
        ys = h - VPAD;
        for(int j = 0; j < interestData.length; j++)
        {
            text = String.valueOf(j + 1);
            textWidth = (float)font.getStringBounds(text, frc).getWidth();
            lm = font.getLineMetrics(text, frc);
            height = lm.getHeight();
            xs = HPAD + j * xInc - textWidth/2;
            g2.drawString(text, xs, ys + height);
        }

        // plot data
          float yy2 = 0, yy1 = 0, xx2 = 0, xx1;
        x1 = HPAD;
          xx1 = HPAD;
        yScale = (float)(h - 2*VPAD) / dataVals[2];

        for(int j = 0; j < interestData.length; j++)
        {
          g.setColor(Color.blue);
          y1 = VPAD + (h - 2*VPAD) - (principleData[j] - dataVals[0]) * yScale;

            if(j > 0)
            g2.draw(new Line2D.Double(x1, y1, x2, y2));
            x2 = x1;
            y2 = y1;
            x1 += xInc;

          g.setColor(Color.red);
          yy1 = VPAD + (h - 2*VPAD) - (interestData[j] - dataVals[0]) * yScale;
               if(j > 0)
            g2.draw(new Line2D.Double(xx1, yy1, xx2, yy2));
            xx2 = xx1;
            yy2 = yy1;
            xx1 += xInc;

        }
    }

    private int[] getDataVals()
    {
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
          int j = interestData.length -1;
          max = (int)principleData[j];
          min = (int)interestData[j];
        int span = max - min;
        return new int[] { min, max, span };
    }
}

rate.txt
7,5.35
15,5.5
30,5.75

Re: Help with Menu 80 80

Ehmm, you forgot to check actionPerformed for your menu options and based on that to fill in your values to appropriate fields

public void actionPerformed(ActionEvent event)
{
    /*
      *Rest of if statement here
      */
     if(event.getActionCommand().equals("Loans1"))
     {
          System.out.println("First option selected");
     }
     if(event.getActionCommand().equals("Loans2"))
     {
          System.out.println("Second option selected");
     }
     if(event.getActionCommand().equals("Loans3"))
     {
          System.out.println("Third option selected");
     }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.