Solved

I would like to display  data to the JTextArea

Posted on 2006-11-07
5
898 Views
Last Modified: 2012-06-27
The code is looking great and I could never have done it with out you, I would like to add a data stream (I think that is an ok way to phrase it), that would appear in the JTextArea, providing an amortization schedule; payment #, interest paid, and principal balance that would count down to zero, below is a couple of snippits of code that I put to help give an idea of what I want to do, and below that is the entire code . Thank you for all the help


JTextArea  area = new JTextArea("");
 area.setEditable(false);
 area.setText("Payment #     Principal balance    Interest paid");

//whole code
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.text.*;

public class Hw_prog2_     extends JFrame {
Hw_prog2_Event TRON = new Hw_prog2_Event(this);

     JButton Calculate;
       JButton Clear;
     JButton Exit;

     JTextArea area;
     JScrollPane scrollPane;
      
       JMenuBar menuBar;
       JMenuItem item;
       JToolBar toolBar;
      
        String fonts[ ] = {"Serif","SansSerif","Monospaced","Dialog","DialogInput"};

             

     JPanel File1 = new JPanel();
       JLabel PrincipalLabel = new JLabel("$Enter Loan Amount:", JLabel.CENTER); // create
       JTextField LonAmt = new JTextField(8);
      
     JPanel File2 = new JPanel();
     JLabel RateLabel = new JLabel("0.0Enter Interest Rate:", JLabel.CENTER); // create
       JTextField Rat = new JTextField(8);

     JPanel File3 = new JPanel();
     JLabel TermLabel = new JLabel("Enter Term-Years:", JLabel.CENTER); // create
       JTextField Trm = new JTextField(8);

     JPanel File4 = new JPanel();
       JLabel PaymentLabel = new JLabel("Monthly Payments:", JLabel.CENTER); // create
       JTextField MthlyPymnt = new JTextField(8);

     JPanel File5 = new JPanel(); // create fifth row (Buttons)

     JPanel File6 = new JPanel();
      
       JPanel File7 = new JPanel();
      
        JPanel File8 = new JPanel();

             // create borders(Grid layout)
     public Hw_prog2_() {                     // Reference the main method
         
     super("McBride.Financial.MortCal"); setSize(890, 130);                 // set size
             
            // name Buttons
     Calculate = new JButton("Calculate");
       Clear = new JButton("New Amount");
       Exit = new JButton("Exit");
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // inform Frame to close

       GridLayout layout = new GridLayout(1, 1, 1, 1); // calls the
                                                                       
       Container pane = getContentPane(); // holds the feilds in place &
                                                   
       pane.setLayout(layout); // checks known parameters
      
          PrincipalLabel.setForeground(Color.blue);
          RateLabel.setForeground(Color.blue);
          TermLabel.setForeground(Color.blue);
          PaymentLabel.setForeground(Color.blue);

          Calculate.setForeground(Color.blue);
          Clear.setForeground(Color.blue);
          Exit.setForeground(Color.blue);
             

          pane.setForeground(Color.blue);

          // TRON listens for Button fuctions
       Calculate.addActionListener(TRON);
       Clear.addActionListener(TRON);
       Exit.addActionListener(TRON);
             // FlowLayout‚ components fall into place from left to right
       FlowLayout mgr = new FlowLayout(FlowLayout.CENTER, 10, 10); // sets

          File1.setLayout(mgr); // Directs the user were to place data
          File1.add(PrincipalLabel); // add Component
          File1.add(LonAmt); // add Component
              pane.add(File1);

          FlowLayout layout2 = new FlowLayout(FlowLayout.CENTER, 10, 10);
          File2.setLayout(layout2);
          File2.add(RateLabel);
          File2.add(Rat);
          pane.add(File2);

          FlowLayout layout3 = new FlowLayout(FlowLayout.CENTER, 10, 10);
          File3.setLayout(layout3);
          File3.add(TermLabel);
          File3.add(Trm);
          pane.add(File3);

          FlowLayout layout4 = new FlowLayout(FlowLayout.CENTER, 10, 10);
          MthlyPymnt.setEditable(false);
          File4.setLayout(layout4);
          File4.add(PaymentLabel);
          File4.add(MthlyPymnt);
          pane.add(File4);

          FlowLayout layout5 = new FlowLayout(FlowLayout.CENTER, 5, 5);
          File5.setLayout(layout5);
          File5.add(Calculate);
          File5.add(Clear);
          File5.add(Exit);
          pane.add(File5);

          FlowLayout layout6 = new FlowLayout(FlowLayout.CENTER);
          File6.setLayout(layout6);
              JTextArea area = new JTextArea(5, 10); // 5 rows and 10 columns
          JScrollPane scrollPane = new JScrollPane(area); // add the text area to
              File6.add(scrollPane);
            pane.add(File6);
             
              FlowLayout layout7 = new FlowLayout(FlowLayout.CENTER);
          File7.setLayout(layout7);
          JMenuBar bar = new JMenuBar();
          File7.add(bar);
       
          JMenu menu = new JMenu("Menu");
          JMenuItem item = new JMenuItem("5.35% at 7yrs");
          JMenuItem itema = new JMenuItem("5.50% at 15yrs");
          JMenuItem itemb = new JMenuItem("5.75% at 30yrs");
          bar.add(menu);
          menu.add(item);
          menu.add(itema);
          menu.add(itemb);
          pane.add(File7);
             
            item.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
                // this will get called
                Rat.setText("5.35");
                Trm.setText("7");
                TRON.LaunchComputation();
}
        });
        itema.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent event) {
                // this will get called
                Rat.setText("5.5");
                Trm.setText("15");
                TRON.LaunchComputation();
}
        });
          itemb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
                // this will get called
                Rat.setText("5.75");
                Trm.setText("30");
                TRON.LaunchComputation();
}
        });
          setVisible(true);
}

        public static void main(String[] arguments) {
     Hw_prog2_ frame = new Hw_prog2_();

}

        class Hw_prog2_Event implements ActionListener {
       Hw_prog2_ virtual;

       public Hw_prog2_Event(Hw_prog2_ in) {
             
                      virtual = in;              

}

      public void actionPerformed(ActionEvent event) {
      
               String command = event.getActionCommand();
                    
               if(event.getSource() == Calculate)
                    LaunchComputation();
               if(event.getSource() == Clear)
                    reset();
               if(event.getSource() == Exit)
                    System.exit(0);
}

      public    void LaunchComputation() { // sub-program to do math
               try {
                        double LonAmt = Double.valueOf(virtual.LonAmt.getText()).doubleValue(); // Object
                                                                                                             
                                                                                                              // string,
                                                                                                              // double
                                                                                                              // value
                                                                                                              // when
                                                                                                              // Tron
                                                                                                              // calls
                                                                                                              // virtual
                    double Rat = Double.valueOf(virtual.Rat.getText()).doubleValue();
                    int Trm = Integer.parseInt(virtual.Trm.getText());
                    int totalmonths = (Trm * 12);
                    double Payment = 0;
                    String monthlyPayment = new String();
                    NumberFormat currency = NumberFormat.getCurrencyInstance();
                    double I = (Rat / 100.0 / 12.0);

                    Payment = (LonAmt * I) / (1 - Math.pow(1 / (1 + I), totalmonths));
                    virtual.MthlyPymnt.setText(currency.format(Payment));
}
catch (NumberFormatException ex) {
}
}

          protected void reset() {
               virtual.LonAmt.setText(null);
               virtual.Rat.setText(null);
               virtual.Trm.setText(null);
               virtual.MthlyPymnt.setText(null);
}
}

}
0
Comment
Question by:charliebaby
  • 3
5 Comments
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 17896018
in your loop cacling the schedule use the append() mthod to append to your text area

line = ......
area.append(line+"\n");

http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JTextArea.html#append(java.lang.String)

Wrapping your text area in a scroll pane wioll allow it to be scraollabel

JScrollPane scroll = new JScrollPane(area);

// now add scroll (not area) to your gui
0
 

Author Comment

by:charliebaby
ID: 17896205

In this part of the code? Will it catch that data?

 item.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent event) {
                // this will get called
                Rat.setText("5.35");
                Trm.setText("7");
                TRON.LaunchComputation();
area.append(line+"\n");

            }

or here?

    public    void LaunchComputation() { // sub-program to do math
               try {
                    double LonAmt = Double.valueOf(virtual.LonAmt.getText()).doubleValue(); // Object
                                                               
0
 

Author Comment

by:charliebaby
ID: 17896231

I'M beat being 3am over here. can not think to clearly now but I am sure that was good advice, since I am going to close the question cause I cannot stay up any longer stareing at the screen, plese post an answer to my querry after if possiable. Thank you so very much,
0
 
LVL 3

Expert Comment

by:tang_tzuchi
ID: 17896269
hi,

you can append the data in your math function:

public    void LaunchComputation() { // sub-program to do math
               try {
                    double LonAmt = Double.valueOf(virtual.LonAmt.getText()).doubleValue(); // Object


area.append(*your payment,commision,etc*+"\n");

..............
0
 

Author Comment

by:charliebaby
ID: 17900525
thank you
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now