Results 1 to 14 of 14
  1. #1
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default Adding a table of text to a TextArea

    Hello all, I have to create a program that allows the user to enter the loan and loan period in the number of years from a text field, and should display
    the monthly and total payments for each interest rate starting from 5 percent to 8 percent, with increments of one-eight, in a text area.My question is how do I put a table of text into a text area.Here is what I need.

    Java Code:
    InterestRate            monthlyPayment                    Totalpayment
    5.0                        188.71                                  11322.74
    5.125                     189.28                                  11357.13
    ..                           ..                                         ..
    and so on up to 8%.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,085
    Rep Power
    20

    Default Re: Adding a table of text to a TextArea

    I believe a JTextArea recognises HTML markup, so a <table> might work?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,648
    Blog Entries
    7
    Rep Power
    21

    Default Re: Adding a table of text to a TextArea

    Quote Originally Posted by Tolls View Post
    I believe a JTextArea recognises HTML markup, so a <table> might work?
    Nope, JTextAreas are kind of stupid when it comes to formatted text; use a JEditorPane or a JTextPane for that purpose; I'd personally use a simple JTable for this purpose.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default Re: Adding a table of text to a TextArea

    Thanks guys I use JFormDesiner to do my GUI and I could not figure out how to add a third coolum to the JTable am I missing something or do I have to do that with code.

  5. #5
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default Here is what I have come up with!

    My question is how do I put this print statement on the GUi

    Java Code:
    public class Quiz2CApp
    {
        public static void main(String[] args)
        {
           LoanTable GUI = new LoanTable();
        }
    }
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    import javax.swing.GroupLayout;
    /*
     * Created by JFormDesigner on Mon Jun 11 11:50:51 CDT 2012
     */
    
    
    
    /**
     * @author Adam Ortell
     */
    public class LoanTable extends JFrame {
        public LoanTable() {
            initComponents();
        }
    
        private void initComponents() {
            // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
            // Generated using JFormDesigner non-commercial license
            panel1 = new JPanel();
            label1 = new JLabel();
            textField1 = new JTextField();
            label2 = new JLabel();
            textField2 = new JTextField();
            button1 = new JButton();
            scrollPane1 = new JScrollPane();
    
            //======== this ========
            Container contentPane = getContentPane();
    
            //======== panel1 ========
            {
    
                //---- label1 ----
                label1.setText("Loan Amount:");
    
                //---- label2 ----
                label2.setText("Number of Years:");
    
                //---- button1 ----
                button1.setText("Show Table");
    
                GroupLayout panel1Layout = new GroupLayout(panel1);
                panel1.setLayout(panel1Layout);
                panel1Layout.setHorizontalGroup(
                    panel1Layout.createParallelGroup()
                        .addGroup(panel1Layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(label1, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(textField1, GroupLayout.PREFERRED_SIZE, 134, GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(label2, GroupLayout.PREFERRED_SIZE, 119, GroupLayout.PREFERRED_SIZE)
                            .addGap(1, 1, 1)
                            .addComponent(textField2, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
                            .addGap(50, 50, 50)
                            .addComponent(button1, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
                            .addGap(0, 19, Short.MAX_VALUE))
                );
                panel1Layout.setVerticalGroup(
                    panel1Layout.createParallelGroup()
                        .addGroup(panel1Layout.createSequentialGroup()
                            .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(panel1Layout.createParallelGroup()
                                .addGroup(GroupLayout.Alignment.TRAILING, panel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                    .addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                    .addComponent(label2)
                                    .addComponent(label1))
                                .addGroup(GroupLayout.Alignment.TRAILING, panel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                    .addComponent(textField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                    .addComponent(button1))))
                );
            }
    
            GroupLayout contentPaneLayout = new GroupLayout(contentPane);
            contentPane.setLayout(contentPaneLayout);
            contentPaneLayout.setHorizontalGroup(
                contentPaneLayout.createParallelGroup()
                    .addComponent(panel1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(scrollPane1, GroupLayout.DEFAULT_SIZE, 588, Short.MAX_VALUE)
            );
            contentPaneLayout.setVerticalGroup(
                contentPaneLayout.createParallelGroup()
                    .addGroup(contentPaneLayout.createSequentialGroup()
                        .addComponent(panel1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(scrollPane1, GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE))
            );
            pack();
            setLocationRelativeTo(getOwner());
            // JFormDesigner - End of component initialization  //GEN-END:initComponents
        }
    
        // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
        // Generated using JFormDesigner non-commercial license
        private JPanel panel1;
        private JLabel label1;
        private JTextField textField1;
        private JLabel label2;
        private JTextField textField2;
        private JButton button1;
        private JScrollPane scrollPane1;
        // JFormDesigner - End of variables declaration  //GEN-END:variables
        class actionEventHandler implements ActionListener
        {
            public void actionPerformed(ActionEvent actionEvent)
            {
                double numerator;
                double denominator;
                double b, e;
                double numYears;
                double principal;
    
    
                System.out.println("Loan Amount: ");
                principal = Double.parseDouble(textField1.getText());
                System.out.println("Number of Years: ");
                numYears = Double.parseDouble(textField2.getText());
                System.out.println("Interest Rate | Monthly Payment | Total Payment");
    
    
                for ( double intRate = 5; intRate <= 8; intRate = Math.round((intRate + .125) * 1000.0)/1000.0 )
                {
                    numerator = intRate/100.0 * principal / 12;
                    e = -(12 * numYears);
                    b = (intRate/100.0 / 12) + 1.0;
                    denominator = 1.0 - Math.pow(b, e);
                    System.out.println( intRate + "% | $" + Math.round((numerator / denominator)*100.0) / 100.0 +
                            " | $" + Math.round(((numerator / denominator) * 12 * numYears) * 100.0) / 100.0);
                }
            }
        }
    }
    What do you think would be the best way to add that print statement to a GUI.
    Thanks alot guys.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default Re: Here is what I have come up with!

    Quote Originally Posted by aortell24 View Post
    What do you think would be the best way to add that print statement to a GUI.
    Learn AWT / Swing.

    Write code.

    Visual designers are for experts who are already beyond that. Not for beginners.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default Re: Here is what I have come up with!

    I have figured it out now I am just trying to get the columns aligned with the Interest and monthly payments. I always feel like that GUI designer was cheating my teacher swears by them.Thanks for the tip.

  8. #8
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default Re: Adding a table of text to a TextArea

    Thanks I always though that was cheating but my teacher swears by them. I figured it out , but is there a way to format JtextArea.appends output like you can with
    printf.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,085
    Rep Power
    20

    Default Re: Adding a table of text to a TextArea

    Quote Originally Posted by JosAH View Post
    Nope, JTextAreas are kind of stupid when it comes to formatted text; use a JEditorPane or a JTextPane for that purpose; I'd personally use a simple JTable for this purpose.

    kind regards,

    Jos
    Oh right.
    I would have suggested a JTable (which is sort of the obvious answer), but I got the impression from the OP that the JTextArea was a requirement.
    Hey, it's a class exercise and we all know how odd the requirements for those can be!
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default Re: Adding a table of text to a TextArea

    It was a requirement to use a textArea.Which I still don't understand being how it could not be formatted correctly.Thanks for all the help guys.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,085
    Rep Power
    20

    Default Re: Adding a table of text to a TextArea

    If you use printf to generate the strings then that might help.
    That way you could sort out padding at least.
    Then just append that string to the area?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,648
    Blog Entries
    7
    Rep Power
    21

    Default Re: Adding a table of text to a TextArea

    And: use a fixed width font (possibly in combination with tabs instead of spaces).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,085
    Rep Power
    20

    Default Re: Adding a table of text to a TextArea

    I only ever think in terms of fixed width...I have a mono-spaced mind.
    :)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  14. #14
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,648
    Blog Entries
    7
    Rep Power
    21

    Default Re: Adding a table of text to a TextArea

    Quote Originally Posted by Tolls View Post
    I only ever think in terms of fixed width...I have a mono-spaced mind.
    :)
    Me too; I prefer all character columns neatly layed out next to each other; all the rest are post modern frillies and they make wibbly columns ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Displaying text in textarea with a button
    By brendan89 in forum New To Java
    Replies: 1
    Last Post: 11-29-2011, 04:53 PM
  2. show text file to textArea
    By louboulos in forum New To Java
    Replies: 14
    Last Post: 06-12-2011, 01:16 PM
  3. Adding to TextArea
    By nawl in forum New To Java
    Replies: 10
    Last Post: 05-21-2010, 07:56 AM
  4. Replies: 0
    Last Post: 02-25-2010, 11:30 AM
  5. Appending text to TextArea
    By deepthought015 in forum AWT / Swing
    Replies: 3
    Last Post: 05-01-2009, 02:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •