I have two issues with my code:
1.When I run the code the components of the GUI are very large. I have attached a screen shot of what it looks like. I'm not sure how to change the size. I've tried using:
nameOfButton.setSize(12,12); but that doesn't work.
2. I'm new to Java and not sure what statement to use to get information from a text file. I'm creating a GUI calculator and I have to read the interest rates from a text file in the action listener portion of the code. I was told that the annual interest statement was incorrect and i'm not sure what it should be.
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.NumberFormat;
import javax.swing.*;
/**
*
* @author
*/
public class Week5_2 extends JFrame implements ActionListener {
private Container contentPane = null;
private JSplitPane split = null;
private JPanel lfPanel = null;
private JScrollPane rtPanel = null;
private JLabel jl_principal;
private JTextField tf_principal;
private JLabel jl_menu;
private JComboBox jcb_menu;
private JLabel jl_monthlyPayment;
private JTextField tf_monthlyPayment;
private JTextArea amortization;
private JButton submit;
private JButton reset;
private JButton exit;
private String[] comboMenu = {"7 years at 5.35%", "15 years at 5.5%", "30 years at 5.75%"};
private String[] interestRates;
private NumberFormat fmt = NumberFormat.getInstance();
public Week5_2(){
super();
initalize();
}
private void initalize(){
interestRates = loadInterestRates("loadtest.txt");
// set up the number formatter
fmt.setGroupingUsed(true);
fmt.setMaximumFractionDigits(2);
fmt.setMinimumFractionDigits(2);
contentPane = this.getContentPane();
setTitle(" Mortgage Calculator");
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// set top pane with BoxLayout
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS));
// create a splitpane to layout out code
split = new JSplitPane();
// make the splitter up and down
split.setOrientation(JSplitPane.VERTICAL_SPLIT);
split.setDividerSize(2);
split.setDividerLocation(200); // set the divider a little off from the middle
//splitpane.setEnabled(false); // locks the divider bar
// Call the method to load the left side of the split pane
loadLeftSide();
// load the right side of the split pane
loadRightSide();
// add the split pane to the content pane
contentPane.add(split, null);
}
private String [] loadInterestRates(String filename)
{
// assumes you have 3 entries in your file
String [] retval = new String [3];
int index = 0;
try
{
File fromFile = new File(filename);
BufferedReader reader = new BufferedReader(new FileReader(fromFile));
String line = reader.readLine();
while(line != null)
{
retval[index] = line;
line = reader.readLine();
index++;
}
return retval;
}
catch (IOException e)
{
System.err.println(e.getMessage());
return null;
}
}
private void loadRightSide()
{
// display amortization chart in Text Area
rtPanel = new JScrollPane();
// make the scroll bars always be there
rtPanel.setHorizontalScrollBarPolicy(javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
rtPanel.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
// create border
rtPanel.setViewportBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
amortization = new JTextArea();
Font displayFont = new Font("Serif", Font.BOLD, 14);
// makes text area uneditable
amortization.setEditable(false);
amortization.setFont(displayFont);
rtPanel.setViewportView(amortization);
split.setRightComponent(rtPanel);
}
private void loadLeftSide (){
lfPanel = new JPanel();
lfPanel.setComponentOrientation(java.awt.ComponentOrientation.LEFT_TO_RIGHT);
// add border to panel
lfPanel.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.SoftBevelBorder.LOWERED));
//set Grid layout of left panel
lfPanel.setLayout(new GridLayout(5,2));
//create labels and set text that appears on label
jl_principal = new JLabel ("Mortgage Amount");
jl_menu = new JLabel ("Term and Interest");
jl_monthlyPayment = new JLabel ("Monthly Payment");
//create combo box and text fields
jcb_menu = new JComboBox(comboMenu);
jcb_menu.setEditable(false);
tf_principal = new JTextField();
tf_monthlyPayment = new JTextField();
tf_monthlyPayment.setEditable(false);
//create buttons
submit = new JButton ("Submit");
//add action listener
submit.addActionListener(this);
exit = new JButton ("Exit");
//add action listener
exit.addActionListener(this);
reset = new JButton ("Reset");
//add action listener
reset.addActionListener(this);
//lay out components on pane
lfPanel.add(jl_principal);
lfPanel.add(tf_principal);
lfPanel.add(jl_menu);
lfPanel.add(jcb_menu);
lfPanel.add(jl_monthlyPayment);
lfPanel.add(tf_monthlyPayment);
lfPanel.add(submit);
lfPanel.add(exit);
lfPanel.add(new JLabel(""));//empty cell
lfPanel.add(reset);
split.setLeftComponent(lfPanel);
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if (source == exit)//enables exit button to operate
{
System.exit(0);
}
if (source == reset)//enables reset button to operate
{
tf_principal.setText("");
tf_monthlyPayment.setText("");
amortization.setText("");
}
if(source == jcb_menu)//enables combo box to set selection by user
{
double select = (double) jcb_menu.getSelectedIndex();
jcb_menu.setSelectedIndex((int) (double) select);
}
int years;
double annualInterest;
int indexOne = jcb_menu.getSelectedIndex();//gets selected term and interest from combo box
double monthlyInt;
if (source == submit)//calculates mortgage when submit button is pushed
{
try
{
double principle = Double.parseDouble(tf_principal.getText().trim());
if(indexOne == 0)
{
years = 7;
String [] selection = interestRates;
annualInterest = selection[0].getDouble();
}
if (indexOne == 1)
{
years = 15;
String [] selection = interestRates;
annualInterest = selection[1].getDouble();
}
if (indexOne == 2)
{
years = 30;
String [] selection = interestRates;
annualInterest = selection[2].getDouble();
monthlyInt = (double) ((annualInterest / 100)/12);
Week5_2Cal calc = new Week5_2Cal (principle, years, monthlyInt);
if(principle>0&&years>0&&monthlyInt>0){//prevent negative numbers from being used
NumberFormat fmt = NumberFormat.getCurrencyInstance();
tf_monthlyPayment.setText("" + fmt.format(calc.getMonthlypayment()));//Gets mortgage payment from Calculation class and assigns to payment.
amortization.setText(calc.getAmortizationTableAsString());//Gets amortization schedule from Calculation class and assigns to JTextArea
} //if set payment
}//end index if
}//ends try
catch(NumberFormatException nfe)
{
JOptionPane.showMessageDialog(null, "Please make sure all your entries are numeric!", "USER INPUT ERROR", JOptionPane.ERROR_MESSAGE);
} //ends catch
}//ends submit if
}//ends action listener
public static void main(String[] args) {
Week5_2 w5 = new Week5_2();
w5.setVisible(true);
}//ends main
}//ends class