Results 1 to 5 of 5
- 02-25-2012, 09:42 PM #1
Closing secondary window from button in primary window
Hi everyone,
I am taking PRG421 and I have the famous mortgage calculator with pie chart.
the Primary window opens and you put in the mortgae principle, choose rate/term from JCombo box,
hit calculate and the pie chart window opens.
Everything works fine... I just want to know if there is a way to close the chart window when I hit reset on the main calculator.
Here's my codes:
And the DisplayChart code:Java Code:/* *@Author: * PRG421 Week 5 Individual Assignment * sr-mf-003 change request 7 * McBride Mortgage Calculator V2.3 * * This program will calculate a monthly payment * and show amortization information for a mortgage * loan based on user input for mortgage principle, * and select term/rate combinations loaded from a * sequential file. Upon pressing calculate the * program displays a second window which contains * the total amount repaid over the life of the loan * and the percentage differentials between interest * and principle paid over the life of the loan and a * pie chart will illustrate these percentages. * The user has the option to loop back and compute * a new loan or exit the program. */ //import libraries import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.io.*; import java.text.*; import java.util.*; public class PRG421Week5 extends JFrame { //Setting decimal formats for currency display DecimalFormat dollars = new DecimalFormat("$###,###,###.00"); DecimalFormat round = new DecimalFormat("#########.00"); //Holds years, interest rate strings read from file ArrayList<String> standardTerms = new ArrayList<String>(); String[] rates; String[] years; //Declaring global GUI components private JLabel principleLabel = new JLabel(" Principle Amount: $"); private JTextField inputPrincipleField = new JTextField(); private JLabel termLabel = new JLabel(" Term of Loan:"); private JTextField outputTermField = new JTextField("7"); private JLabel rateLabel = new JLabel(" Rate: %"); private JTextField outputRateField = new JTextField("5.35"); private JLabel termRateLabel = new JLabel(" Term/Rate Option:"); private JComboBox termRateOption = new JComboBox(); private JLabel paymentLabel = new JLabel(" Monthly Payment: $"); private JTextField outputPaymentField = new JTextField(); private JTextField totalPaidField = new JTextField(); private JTextField blankField = new JTextField(); private JButton calculateButton = new JButton("Calculate"); private JButton resetButton = new JButton("Reset"); private JTextArea outputResultsArea = new JTextArea(10, 25); private JScrollPane scrollResults = new JScrollPane(outputResultsArea); private Container ContentPane = getContentPane(); public PRG421Week5(String title) { super.setTitle(title); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //calling readSequentialFile method readSequentialFile(); //Handling termRateOption events termRateOption.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { termRateOption.getSelectedItem(); //Setting the text of outputTermField and outputRateField based on termRateOption int cbIndex = termRateOption.getSelectedIndex(); if (cbIndex > -1) { outputTermField.setText(years[cbIndex]); outputRateField.setText(rates[cbIndex]); } } }); //Handling calculateButton events calculateButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //Try-catch loop resets all fields in the event of an exception try { //Getting user input double principle = Double.parseDouble(inputPrincipleField.getText()); double chartPrinciple = Double.parseDouble(inputPrincipleField.getText()); double interestRate = Double.parseDouble(outputRateField.getText()); double months = Integer.parseInt(outputTermField.getText()) * 12; //Calculating monthly payment double monthlyPayment = principle * Math.pow(1 + ((interestRate / 100) / 12), months) * ((interestRate / 100) / 12) / (Math.pow(1 + ((interestRate / 100) / 12), months) - 1); //Setting up variables to be carried to chart double roundMonthly = Math.round(monthlyPayment * 100.00) / 100.00; double totalPaid = roundMonthly * months; totalPaidField.setText("" + (round.format(totalPaid))); outputPaymentField.setText(dollars.format(monthlyPayment)); //calculate the amortization information and output to outputResultsArea int month; StringBuilder buffer = new StringBuilder(); buffer.append(" AMORTIZATION INFORMATION \n"); buffer.append(" ______________________________________ \n"); buffer.append(" \n "); buffer.append("Month(s)\tLoan Balance\t Interest Paid \n"); for (int i = 0; i < months; i++) { month = i + 1; double interest = principle * ((interestRate / 100) / 12); double balance = principle + interest - monthlyPayment; buffer.append(month).append("\t"); StringBuilder append = buffer.append(dollars.format(principle)).append("\t "); StringBuilder append1 = buffer.append(dollars.format(interest)).append("\n"); principle = balance; } outputResultsArea.setText(buffer.toString()); outputResultsArea.setCaretPosition(0); // Declaring DisplayChart as a component DisplayChart component = new DisplayChart(chartPrinciple, totalPaid); } catch (Exception ex) { inputPrincipleField.setText(""); termRateOption.setSelectedIndex(0); outputTermField.setText("7"); outputRateField.setText("5.35"); outputPaymentField.setText(""); totalPaidField.setText(""); outputResultsArea.setText(""); } } }); //Handling resetButton events resetButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { inputPrincipleField.setText(""); termRateOption.setSelectedIndex(0); outputTermField.setText("7"); outputRateField.setText("5.35"); outputPaymentField.setText(""); totalPaidField.setText(""); outputResultsArea.setText(""); } }); //Building the GU JPanel bottom = new JPanel(); bottom.setLayout(new GridLayout(2, 1)); JPanel centerPanel = new JPanel(); centerPanel.setLayout(new GridLayout(7, 2, 5, 5)); centerPanel.add(principleLabel); centerPanel.add(inputPrincipleField); centerPanel.add(termRateLabel); centerPanel.add(termRateOption); centerPanel.add(termLabel); centerPanel.add(outputTermField); outputTermField.setEditable(false); centerPanel.add(rateLabel); centerPanel.add(outputRateField); outputRateField.setEditable(false); centerPanel.add(paymentLabel); centerPanel.add(outputPaymentField); outputPaymentField.setEditable(false); centerPanel.add(totalPaidField); totalPaidField.setEditable(false); totalPaidField.setVisible(false); centerPanel.add(blankField); blankField.setEditable(false); blankField.setVisible(false); centerPanel.add(calculateButton); centerPanel.add(resetButton); outputResultsArea.setEditable(false); bottom.add(centerPanel); bottom.add(scrollResults); ContentPane.add(bottom); } // Reading the sequential file and populating the arrays private void readSequentialFile() { //try-catch loop notifies user of errors in sequential file and closes program try { String line; BufferedReader br = new BufferedReader(new FileReader("term.txt")); while ((line = br.readLine()) != null) { standardTerms.add(line); termRateOption.addItem(line); } br.close(); // Get sequential file line count int sequentialLineCount = standardTerms.size(); //populate the arrays rates = new String[sequentialLineCount]; years = new String[sequentialLineCount]; String[] temp; for (int index = 0; index < sequentialLineCount; index++) { temp = standardTerms.get(index).split(","); years[index] = temp[0]; rates[index] = temp[1]; } } catch (Exception ex) { JOptionPane.showMessageDialog(this, "Error reading sequential file. Program will now close."); System.exit(1); } } //Main method draws GUI public static void main(String[] args) { PRG421Week5 frame = new PRG421Week5("McBride Mortgage Calculator V2.3"); frame.setSize(320, 500); frame.setVisible(true); frame.setResizable(false); } }
The text file for the terms/rates is also attached...Java Code:import java.awt.*; import javax.swing.JFrame; import java.text.*; public class DisplayChart extends JFrame { private double chartPrinciple2; private double chartTotalPaid; private double chartInterest; private double interestPercent; private double principlePercent; private int roundPrinciplePercent; private int roundInterestPercent; private int piePercent; public DisplayChart(Double chartPrinciple, Double totalPaid) { super("McBride Mortgage Calculator V2.3 Chart Display"); setSize(420, 500); setLocation(320, 0); setResizable(false); setVisible(true); chartPrinciple2 = chartPrinciple; chartTotalPaid = totalPaid; chartInterest = totalPaid - chartPrinciple2; interestPercent = (chartInterest * 100) / totalPaid; principlePercent = (chartPrinciple2 * 100) / totalPaid; roundPrinciplePercent = (int) (Math.round (principlePercent*100)/100); roundInterestPercent = (int) (Math.round (interestPercent*100.00)/100.00); piePercent = (360*roundInterestPercent)/100; } @Override public void paint(Graphics comp) { DecimalFormat dollars = new DecimalFormat("$###,###,###.00"); DecimalFormat percent = new DecimalFormat("###"); Graphics2D comp2D = (Graphics2D) comp; comp.setColor(Color.red); comp.fillRect(40, 300, 15, 15); comp.setColor(Color.black); comp.drawString("Total Interest and Principle Paid " + dollars.format(chartTotalPaid), 40, 287); comp.drawString("Total Principal Paid " + dollars.format(chartPrinciple2) + " Which is " +percent.format(principlePercent) +"% of the Total Paid", 60, 312); comp.drawString("Total Interest Paid " + dollars.format(chartInterest) + " Which is " +percent.format(interestPercent) +"% of the Total Paid", 60, 337); comp.setColor(Color.blue); comp.fillRect(40, 325, 15, 15); comp.setColor(Color.red); comp.fillArc(115, 50, 200, 200, 0, 360); comp.setColor(Color.blue); comp.fillArc(115, 50, 200, 200, 90, (piePercent)); } }
Any help would be appreciated... I am sure I already have met the requirements for the program, but I want it to work the way i want it to work :)
- 02-25-2012, 11:09 PM #2
Member
- Join Date
- Feb 2012
- Posts
- 39
- Rep Power
- 0
Re: Closing secondary window from button in primary window
Doing (JFrame.EXIT_ON_CLOSE) closes all JFrames change it to (JFrame.DISPOSE_ON_CLOSE)
-
Re: Closing secondary window from button in primary window
I politely disagree with this. I suggest leaving the JFrame's defaultCloseOperation unchanged. The dependent window shouldn't even be a JFrame but a JDialog, and it sounds like you want a non-modal one. When you're done with the dialog, simply call dispose() on it.
Edit: but also don't forget to make your DisplayChart variable a class field, not a field that is declared inside of a method and thus buried inside of a method and invisible to the rest of the class. Otherwise your dialog won't be in the scope of your reset button listener code, and there will be no way that reset can call dispose() on it.Last edited by Fubarable; 02-25-2012 at 11:21 PM.
- 02-26-2012, 12:56 AM #4
-
Similar Threads
-
how to disable the parent window when child window is open
By ayushi in forum Java ServletReplies: 1Last Post: 07-25-2011, 10:24 AM -
Applying action event on window closing?
By asifzbaig in forum AWT / SwingReplies: 2Last Post: 06-13-2011, 07:25 PM -
Brand new to java ... window is closing
By drw4d in forum New To JavaReplies: 5Last Post: 03-20-2011, 05:48 PM -
Checking if a button was pressed in a Window.
By Valkyrie in forum New To JavaReplies: 2Last Post: 12-15-2009, 05:28 AM -
how can we call Logout servlet by closing window
By nagesh1811 in forum Java ServletReplies: 2Last Post: 07-11-2008, 07:41 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks