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.
Code:
InterestRate monthlyPayment Totalpayment
5.0 188.71 11322.74
5.125 189.28 11357.13
.. .. ..
and so on up to 8%.
Re: Adding a table of text to a TextArea
I believe a JTextArea recognises HTML markup, so a <table> might work?
Re: Adding a table of text to a TextArea
Quote:
Originally Posted by
Tolls
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
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.
Here is what I have come up with!
My question is how do I put this print statement on the GUi
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.
Re: Here is what I have come up with!
Quote:
Originally Posted by
aortell24
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
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.
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.
Re: Adding a table of text to a TextArea
Quote:
Originally Posted by
JosAH
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!
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.
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?
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
Re: Adding a table of text to a TextArea
I only ever think in terms of fixed width...I have a mono-spaced mind.
:)
Re: Adding a table of text to a TextArea
Quote:
Originally Posted by
Tolls
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