Two column list with JOptionPane  
Author Message
Jennifer Szonye





PostPosted: 2005-12-10 18:42:00 Top

java-programmer, Two column list with JOptionPane Yes, this is a homework question. My assignment is:
Modify the mortgage program using a graphical user interface to allow the
user to input the amount of a mortgage and then select from a menu of
mortgage loans: 7 year at 5.35%, 15 year at 5.5 %, and 30 year at 5.75%.
Use an array for the different loans. Display the mortgage payment amount.
Then, list the loan balance and interest paid for each payment over the term
of the loan. Allow the user to loop back and enter a new amount and make a
new selection, or quit. Insert comments in the program to document the
program.

I have been able to complete all of it except the part about list the loan
balance and interest paid. I used JOptionPane to do all the graphical user
interface and the menu of mortgages (see code below), but can't seem to come
up with a way to display the information in JOptionPane. If there is
another way that will work please point me in the right direction.

private static double option(double inputAmount)
{
// declares variables
double amount = inputAmount;
double monthlyPayment = 0.0;
boolean done = false;

// loop while not done
while (!done)
{
// declare and set size of/initializes array
double loanInfo[][] = new double [][] {{5.35, 5.5, 5.75},{7,15,30}};

// creates an array to hold the options
String smallList[] = {"7 year at 5.35%","15 year at 5.5%","30 year at
5.75%"};
String option = (String)JOptionPane.showInputDialog(null,"Which loan
option?","Options",JOptionPane.QUESTION_MESSAGE,null,smallList,smallList[smallList.length-1]);

for(int count = 0; count < smallList.length; count++)
{
// checks answers for match and sends info to calculate payment
if(option == smallList[count])
{
// calls method to calculate the monthly payment
monthlyPayment = calculationsPayment(amount, loanInfo[0][count],
loanInfo[1][count]);
done = true;
}
}
}
return monthlyPayment;
}
--
Jennifer Szonye


 
Rhino





PostPosted: 2005-12-10 23:20:00 Top

java-programmer >> Two column list with JOptionPane
"Jennifer Szonye" <email***@***.com> wrote in message
news:439ab115$0$81303$email***@***.com...
> Yes, this is a homework question. My assignment is:
> Modify the mortgage program using a graphical user interface to allow the
> user to input the amount of a mortgage and then select from a menu of
> mortgage loans: 7 year at 5.35%, 15 year at 5.5 %, and 30 year at 5.75%.
> Use an array for the different loans. Display the mortgage payment
> amount. Then, list the loan balance and interest paid for each payment
> over the term of the loan. Allow the user to loop back and enter a new
> amount and make a new selection, or quit. Insert comments in the program
> to document the program.
>
> I have been able to complete all of it except the part about list the loan
> balance and interest paid. I used JOptionPane to do all the graphical
> user interface and the menu of mortgages (see code below), but can't seem
> to come up with a way to display the information in JOptionPane. If there
> is another way that will work please point me in the right direction.
>
> private static double option(double inputAmount)
> {
> // declares variables
> double amount = inputAmount;
> double monthlyPayment = 0.0;
> boolean done = false;
>
> // loop while not done
> while (!done)
> {
> // declare and set size of/initializes array
> double loanInfo[][] = new double [][] {{5.35, 5.5, 5.75},{7,15,30}};
>
> // creates an array to hold the options
> String smallList[] = {"7 year at 5.35%","15 year at 5.5%","30 year at
> 5.75%"};
> String option = (String)JOptionPane.showInputDialog(null,"Which loan
> option?","Options",JOptionPane.QUESTION_MESSAGE,null,smallList,smallList[smallList.length-1]);
>
> for(int count = 0; count < smallList.length; count++)
> {
> // checks answers for match and sends info to calculate payment
> if(option == smallList[count])
> {
> // calls method to calculate the monthly payment
> monthlyPayment = calculationsPayment(amount, loanInfo[0][count],
> loanInfo[1][count]);
> done = true;
> }
> }
> }
> return monthlyPayment;
> }
> --

A JOptionPane is a reasonable way to prompt for a single piece of data, to
allow a user to make a simple choice, or to give a message to the user but
it is not a substitute for a GUI. I think your instructor is visualizing
something a bit more involved than a JOptionPane.

If it were my assignment, I'd use a JList or JComboBox to display the
different mortgage combinations in your smallList variable. I'd probably
display the monthly payment amount in a JTextBox and put the interest amount
and the declining balance for each month of the mortgage in a JTable. I'd
put these different components together on a JFrame or JDialog if I were
writing an application or in a JApplet if I wanted an applet. I might use a
JSplitPane to separate the inputs, the information the mortgage applicant
gives, from the outputs, the repayment information. You can make the GUI
quite simple or you can make it quite elaborate, depending on how much you
want it to do and how "friendly" you want it to be.

Have a look at the variety of different Swing components available to you at
http://java.sun.com/docs/books/tutorial/uiswing/components/components.html.
I think you'll see that you can do a lot better by using a combination of
components than by trying to push a JOptionPane way beyond its intended
purpose.

Rhino


 
Jennifer Szonye





PostPosted: 2005-12-12 11:06:00 Top

java-programmer >> Two column list with JOptionPane Thank you Rhino. I mashed everything into JOptionPane because that as far
as we have read in the book the school wants us to use. It doesn't go into
much depth about anything and I am getting most my information from
searching the web. The classes are only 5 weeks long, so I am under a lot
of pressure to get these done. I also haven't been able to figure out
exactly how the other options work. The site you have listed looks like it
has some good information, I will read it.
Thank you again.

--
Jennifer Szonye


 
 
Rhino





PostPosted: 2005-12-13 2:24:00 Top

java-programmer >> Two column list with JOptionPane
"Jennifer Szonye" <email***@***.com> wrote in message
news:439ce93d$0$81311$email***@***.com...
> Thank you Rhino. I mashed everything into JOptionPane because that as far
> as we have read in the book the school wants us to use. It doesn't go
> into much depth about anything and I am getting most my information from
> searching the web. The classes are only 5 weeks long, so I am under a lot
> of pressure to get these done.

Oh dear! Your course is far from ideal on so many levels....

Five weeks of a course that doesn't show you any GUI components beyond
JOptionPane is not going to make a Java expert of you. It may be enough to
qualify you for a job somewhere though.

Clearly, you are going to have to do a lot of work on your own if you really
want to master Java. Luckily, much of that work can probably take place on
the job and in your spare time after you get the job.

> I also haven't been able to figure out exactly how the other options
> work. The site you have listed looks like it has some good information, I
> will read it.

The Java Tutorial, which I cited, has good explanations - and many
examples - of how to use each GUI component. It should be very helpful in
getting you started with these components. It won't take you very long to
learn the basics of the small number of components you need for your
assignment. Keep it simple for now; you can always make something flashier
when you know more about the GUI components.

> Thank you again.
>
Good luck with your course!

Rhino


 
 
Jennifer Szonye





PostPosted: 2005-12-16 14:34:00 Top

java-programmer >> Two column list with JOptionPane Thank you for your help so far. I have been doing a lot of studing
(especially when I am suppose to be working). This course is part of an IT
degree, but I would like to be able to use it and develope writing Java more
after the class is over.

Using the site and many books I was able to construct a text area with the
information I wanted, but I am having trouble getting the event handler to
work. I have sent a copy to my instructor asking for help, but with
vacation comming up, I don't know when he will get back to me.

Could you please look over this piece of code with the event handler and
tell me what I am doing wrong? I hate asking, but I have tried every thing
I can think of. Or point me to another place to look, my books all have
different examples and I think I am jumbling them all together. Not to
mention getting more confused. I think I am missing some code.

Thanks!

// method for printing the loan balance and interest payments per month
public static void viewArea(double amount, double monthlyPayment, double
interestRate)
{
// declares variables
double interestPayment = 0.0;
double balanceReduction = 0.0;
double newBalance = amount;

int paymentNumber = 1;

//text area for displaying results
int AREA_ROWS = 30;
int AREA_COLUMNS = 30;

JFrame frame = new JFrame();
JPanel panel = new JPanel();
JTextArea textArea = new JTextArea(AREA_ROWS, AREA_COLUMNS);
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);

// sets button
final JButton yesButton = new JButton("Another Mortgage");
final JButton noButton = new JButton("Quit");

// puts buttons and scroll on frame
panel.add(yesButton);
panel.add(noButton);
panel.add(scrollPane);

frame.setContentPane(panel);

class buttonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
int programGo = 1;

if(yesButton.isSelected())
{
programGo = 1;
}
else if (noButton.isSelected())
{
programGo = 0;
}
MortgageMainWeek3(programGo);
}
}

frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

while (amount >= balanceReduction)
{
// converts amount to currancy format
NumberFormat currency = NumberFormat.getCurrencyInstance();

// calculates the interst for the month
interestPayment = amount * interestRate;

// calculate the amount to deduct from principle
balanceReduction = monthlyPayment - interestPayment;

// calculate the new balance of the loan
newBalance = amount - balanceReduction;

// set amount equal to new principal
amount = newBalance;

// prints the loan balance and the interest payments per month
textArea.append("Payment# "+paymentNumber+" :\n Balance Owed:
"+currency.format(newBalance)+" \n Interest owed:
"+currency.format(interestPayment)+"\n");

// increases counter by 1
paymentNumber++;
}
ActionListener listener = new buttonListener();
yesButton.addActionListener(listener);
noButton.addActionListener(listener);
}

--
Jennifer Szonye


 
 
Mark Thomas





PostPosted: 2005-12-16 16:37:00 Top

java-programmer >> Two column list with JOptionPane Jennifer

Try the following:

Jennifer Szonye wrote:
> if(yesButton.isSelected())
if (e.getsource == yesButton)
> {
> programGo = 1;
> }
> else if (noButton.isSelected())
else if (e.getSource == noButton)

Mark
 
 
Oliver Wong





PostPosted: 2005-12-17 6:15:00 Top

java-programmer >> Two column list with JOptionPane
"Rhino" <email***@***.com> wrote in message
news:6fjnf.934$email***@***.com...

> Clearly, you are going to have to do a lot of work on your own if you
> really want to master Java. Luckily, much of that work can probably take
> place on the job and in your spare time after you get the job.

Spare time after you get an programming job. That's a laugh. =)

- Oliver


 
 
Jennifer Szonye





PostPosted: 2005-12-18 6:46:00 Top

java-programmer >> Two column list with JOptionPane If you knew my husband you would be laughing. He is constantly shoving
programming books at me. The newest one he wants me to read is about good
programming style and Dr. Scheme.

--
Jennifer Szonye
"Oliver Wong" <email***@***.com> wrote in message
news:L%Gof.5681$ic1.3954@edtnps90...
>
> "Rhino" <email***@***.com> wrote in message
> news:6fjnf.934$email***@***.com...
>
>> Clearly, you are going to have to do a lot of work on your own if you
>> really want to master Java. Luckily, much of that work can probably take
>> place on the job and in your spare time after you get the job.
>
> Spare time after you get an programming job. That's a laugh. =)
>
> - Oliver
>


 
 
Jennifer Szonye





PostPosted: 2005-12-18 6:47:00 Top

java-programmer >> Two column list with JOptionPane I tried that one. Here is the error I get:

C:\Documents and Settings\Jenni\My Documents\U of Phoenix School\POS
407\Week 3\Individual Assignment\JenniferSzonyeMortgageUse.java:127: cannot
resolve symbol
symbol : variable e
location: class buttonListener
if(e.getsource == yesButton)
^
C:\Documents and Settings\Jenni\My Documents\U of Phoenix School\POS
407\Week 3\Individual Assignment\JenniferSzonyeMortgageUse.java:131: cannot
resolve symbol
symbol : variable e
location: class buttonListener
else if (e.getsource == noButton)

--
Jennifer Szonye
"Mark Thomas" <anon> wrote in message
news:43a27c7f$0$82632$email***@***.com...
> Jennifer
>
> Try the following:
>
> Jennifer Szonye wrote:
>> if(yesButton.isSelected())
> if (e.getsource == yesButton)
>> {
>> programGo = 1;
>> }
>> else if (noButton.isSelected())
> else if (e.getSource == noButton)
>
> Mark


 
 
Mark Thomas





PostPosted: 2005-12-18 8:20:00 Top

java-programmer >> Two column list with JOptionPane Jennifer Szonye wrote:
> I tried that one. Here is the error I get:
>
> C:\Documents and Settings\Jenni\My Documents\U of Phoenix School\POS
> 407\Week 3\Individual Assignment\JenniferSzonyeMortgageUse.java:127: cannot
> resolve symbol
> symbol : variable e
> location: class buttonListener
> if(e.getsource == yesButton)
> ^
> C:\Documents and Settings\Jenni\My Documents\U of Phoenix School\POS
> 407\Week 3\Individual Assignment\JenniferSzonyeMortgageUse.java:131: cannot
> resolve symbol
> symbol : variable e
> location: class buttonListener
> else if (e.getsource == noButton)
>
Come on Jennifer - look further. OK, in your actionPerformed method
you've named your ActionEvent 'event' not 'e'. So replace 'e' with
'event' & try again.

Mark
 
 
Jennifer Szonye





PostPosted: 2005-12-19 13:31:00 Top

java-programmer >> Two column list with JOptionPane No, I caught that. I still get the errors.

--
Jennifer Szonye
"Mark Thomas" <anon> wrote in message
news:43a4aadc$0$63085$email***@***.com...
> Jennifer Szonye wrote:
>> I tried that one. Here is the error I get:
>>
>> C:\Documents and Settings\Jenni\My Documents\U of Phoenix School\POS
>> 407\Week 3\Individual Assignment\JenniferSzonyeMortgageUse.java:127:
>> cannot resolve symbol
>> symbol : variable e
>> location: class buttonListener
>> if(e.getsource == yesButton)
>> ^
>> C:\Documents and Settings\Jenni\My Documents\U of Phoenix School\POS
>> 407\Week 3\Individual Assignment\JenniferSzonyeMortgageUse.java:131:
>> cannot resolve symbol
>> symbol : variable e
>> location: class buttonListener
>> else if (e.getsource == noButton)
>>
> Come on Jennifer - look further. OK, in your actionPerformed method
> you've named your ActionEvent 'event' not 'e'. So replace 'e' with
> 'event' & try again.
>
> Mark


 
 
Jennifer Szonye





PostPosted: 2005-12-19 17:04:00 Top

java-programmer >> Two column list with JOptionPane Thank you! It works. You can not believe the relief I feel now.

--
Jennifer Szonye
"Jennifer Szonye" <email***@***.com> wrote in message
news:43a25fd1$0$58112$email***@***.com...
> Thank you for your help so far. I have been doing a lot of studing
> (especially when I am suppose to be working). This course is part of an
> IT degree, but I would like to be able to use it and develope writing Java
> more after the class is over.
>
> Using the site and many books I was able to construct a text area with the
> information I wanted, but I am having trouble getting the event handler to
> work. I have sent a copy to my instructor asking for help, but with
> vacation comming up, I don't know when he will get back to me.
>
> Could you please look over this piece of code with the event handler and
> tell me what I am doing wrong? I hate asking, but I have tried every
> thing I can think of. Or point me to another place to look, my books all
> have different examples and I think I am jumbling them all together. Not
> to mention getting more confused. I think I am missing some code.
>
> Thanks!
>
> // method for printing the loan balance and interest payments per month
> public static void viewArea(double amount, double monthlyPayment, double
> interestRate)
> {
> // declares variables
> double interestPayment = 0.0;
> double balanceReduction = 0.0;
> double newBalance = amount;
>
> int paymentNumber = 1;
>
> //text area for displaying results
> int AREA_ROWS = 30;
> int AREA_COLUMNS = 30;
>
> JFrame frame = new JFrame();
> JPanel panel = new JPanel();
> JTextArea textArea = new JTextArea(AREA_ROWS, AREA_COLUMNS);
> textArea.setEditable(false);
> JScrollPane scrollPane = new JScrollPane(textArea);
>
> // sets button
> final JButton yesButton = new JButton("Another Mortgage");
> final JButton noButton = new JButton("Quit");
>
> // puts buttons and scroll on frame
> panel.add(yesButton);
> panel.add(noButton);
> panel.add(scrollPane);
>
> frame.setContentPane(panel);
>
> class buttonListener implements ActionListener
> {
> public void actionPerformed(ActionEvent event)
> {
> int programGo = 1;
>
> if(yesButton.isSelected())
> {
> programGo = 1;
> }
> else if (noButton.isSelected())
> {
> programGo = 0;
> }
> MortgageMainWeek3(programGo);
> }
> }
>
> frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
> frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
> frame.setVisible(true);
>
> while (amount >= balanceReduction)
> {
> // converts amount to currancy format
> NumberFormat currency = NumberFormat.getCurrencyInstance();
>
> // calculates the interst for the month
> interestPayment = amount * interestRate;
>
> // calculate the amount to deduct from principle
> balanceReduction = monthlyPayment - interestPayment;
>
> // calculate the new balance of the loan
> newBalance = amount - balanceReduction;
>
> // set amount equal to new principal
> amount = newBalance;
>
> // prints the loan balance and the interest payments per month
> textArea.append("Payment# "+paymentNumber+" :\n Balance Owed:
> "+currency.format(newBalance)+" \n Interest owed:
> "+currency.format(interestPayment)+"\n");
>
> // increases counter by 1
> paymentNumber++;
> }
> ActionListener listener = new buttonListener();
> yesButton.addActionListener(listener);
> noButton.addActionListener(listener);
> }
>
> --
> Jennifer Szonye
>