Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Parsing from a GUI text field

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Parsing from a GUI text field

    Hello,

    Here is my issue. I am trying to parse the string entered into the JTextField of my GUI so that it can be used in a calculation. The GUI is in one class and the calculation is in another. I hope someone can help me. My code follows:

    public class GUI extends javax.swing.JFrame {
     
        MortgageCalculator calculate = new MortgageCalculator();
     
        //Creates new form MortgageCalculatorGUI
        public GUI() {
            initComponents();
        }
     
        /**
         * This method is called from within the constructor to initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is always
         * regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
        private void initComponents() {
     
            jPanel1 = new javax.swing.JPanel();
            JLabelPrincipal = new javax.swing.JLabel();
            JLabelInterestRate = new javax.swing.JLabel();
            JLabelTerm = new javax.swing.JLabel();
            jTextFieldPrincipal = new javax.swing.JTextField();
            jTextFieldInterestRate = new javax.swing.JTextField();
            jTextFieldTerm = new javax.swing.JTextField();
            jPanel2 = new javax.swing.JPanel();
            jButtonSubmit = new javax.swing.JButton();
            jButtonClear = new javax.swing.JButton();
            jScrollPane1 = new javax.swing.JScrollPane();
            jTextAreaMonthlyPayment = new javax.swing.JTextArea();
            jLabelMonthlyPaymentInfo = new javax.swing.JLabel();
            jScrollPane2 = new javax.swing.JScrollPane();
            jTextAreaAmortizationInfo = new javax.swing.JTextArea();
            jLabelAmortizationInfo = new javax.swing.JLabel();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
            jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
     
            JLabelPrincipal.setText("Principal");
     
            JLabelInterestRate.setText("Interest Rate");
     
            JLabelTerm.setText("Term");
     
            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(JLabelTerm, javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(JLabelInterestRate, javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(JLabelPrincipal, javax.swing.GroupLayout.Alignment.TRAILING))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jTextFieldTerm, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 55, Short.MAX_VALUE)
                            .addComponent(jTextFieldInterestRate, javax.swing.GroupLayout.Alignment.LEADING))
                        .addComponent(jTextFieldPrincipal, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(JLabelPrincipal)
                        .addComponent(jTextFieldPrincipal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(JLabelInterestRate)
                        .addComponent(jTextFieldInterestRate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(JLabelTerm)
                        .addComponent(jTextFieldTerm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
     
            jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
     
            jButtonSubmit.setText("Submit");
            jButtonSubmit.setDoubleBuffered(true);
            jButtonSubmit.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButtonSubmitActionPerformed(evt);
                }
            });
     
            jButtonClear.setText("Clear");
            jButtonClear.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButtonClearActionPerformed(evt);
                }
            });
     
            javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
            jPanel2.setLayout(jPanel2Layout);
            jPanel2Layout.setHorizontalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jButtonSubmit, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addComponent(jButtonClear)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
     
            jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButtonClear, jButtonSubmit});
     
            jPanel2Layout.setVerticalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButtonSubmit)
                        .addComponent(jButtonClear))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
     
            jTextAreaMonthlyPayment.setColumns(20);
            jTextAreaMonthlyPayment.setRows(5);
            jScrollPane1.setViewportView(jTextAreaMonthlyPayment);
     
            jLabelMonthlyPaymentInfo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
            jLabelMonthlyPaymentInfo.setText("Monthly Payment Information");
     
            jTextAreaAmortizationInfo.setColumns(20);
            jTextAreaAmortizationInfo.setEditable(false);
            jTextAreaAmortizationInfo.setRows(5);
            jScrollPane2.setViewportView(jTextAreaAmortizationInfo);
     
            jLabelAmortizationInfo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
            jLabelAmortizationInfo.setText("Loan Amortization Information");
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jScrollPane2)
                        .addComponent(jLabelMonthlyPaymentInfo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 0, Short.MAX_VALUE)
                        .addComponent(jScrollPane1)
                        .addComponent(jLabelAmortizationInfo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jLabelMonthlyPaymentInfo)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jLabelAmortizationInfo)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
                    .addContainerGap())
            );
     
            pack();
        }// </editor-fold>//GEN-END:initComponents
     
        // Action listener for Submit button
        private void jButtonSubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSubmitActionPerformed
     
    	calculate.displayLoanInformation(jTextAreaMonthlyPayment);
        calculate.amortization(jTextAreaAmortizationInfo);
        }//GEN-LAST:event_jButtonSubmitActionPerformed
     
        // Action listener for Clear button
        private void jButtonClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonClearActionPerformed
        jTextFieldPrincipal.setText("");
        jTextFieldTerm.setText("");
        jTextFieldInterestRate.setText("");
        jTextAreaMonthlyPayment.setText("");
        jTextAreaAmortizationInfo.setText("");
        }//GEN-LAST:event_jButtonClearActionPerformed
     
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
     
            //Create and display the form
            java.awt.EventQueue.invokeLater(new Runnable() {
     
                @Override
                public void run() {
                    new GUI().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify//GEN-BEGIN:variables
        private javax.swing.JLabel JLabelInterestRate;
        private javax.swing.JLabel JLabelPrincipal;
        private javax.swing.JLabel JLabelTerm;
        private javax.swing.JButton jButtonClear;
        private javax.swing.JButton jButtonSubmit;
        private javax.swing.JLabel jLabelAmortizationInfo;
        private javax.swing.JLabel jLabelMonthlyPaymentInfo;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JPanel jPanel2;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JScrollPane jScrollPane2;
        private javax.swing.JTextArea jTextAreaAmortizationInfo;
        private javax.swing.JTextArea jTextAreaMonthlyPayment;
        private javax.swing.JTextField jTextFieldInterestRate;
        private javax.swing.JTextField jTextFieldPrincipal;
        private javax.swing.JTextField jTextFieldTerm;
        // End of variables declaration//GEN-END:variables
    }

    import java.text.DecimalFormat;
    import javax.swing.JOptionPane;
    import javax.swing.JTextArea;
     
    public class MortgageCalculator {
     
        //Declare decimal format that will display selected variable as dollar amounts
        DecimalFormat Money = new DecimalFormat ("$###,##0.00");
     
        //Loan Amount, Interest Rate, and Term are hard coded
        double principal = 1000;
        double interestRate = 2;
        double term = 1;
     
        //Declare Monthly Payment
        double monthlyPayment = calculateMonthlyPayment(principal, interestRate, term);
     
        //Declare Total Interest Paid
        double totalInterestPaid = calculateTotalInterestPaid(principal, interestRate, term);
     
        //Monthly Payment calculation
        public double calculateMonthlyPayment(double principal, double interestRate, double term) {
            if ((principal < 1) || (principal > 1000)) {
                JOptionPane.showMessageDialog(null, "Please enter a number between 1 and 1000");
            }
            return (principal * (interestRate / 100) / 12)/
                    (1- Math.pow (1 + (interestRate / 100) / 12, - term * 12));
        }
     
        //Total Interest Paid calculation
        public double calculateTotalInterestPaid(double principal, double interestRate, double term) {
            return (principal * (interestRate / 100) /12) * (term * 12);
        }
     
    //    //Print loan information to the console
        public void displayLoanInformation(JTextArea LoanInfoOutput) {
            LoanInfoOutput.append("Loan Amount = " + 
                    (Money.format(principal)) + "\n");
            LoanInfoOutput.append("Interest Rate = " + interestRate + "%" + "\n");
            LoanInfoOutput.append("Term = " + term + " Years" + "\n");
            LoanInfoOutput.append("Monthly Mortgage Payment = " + 
                    (Money.format(monthlyPayment)) + "\n");
            LoanInfoOutput.append("Total Interest Paid = " + 
                    (Money.format(totalInterestPaid)));
        }
     
        //Print amortization information to the console
        public void amortization(JTextArea AmortizationOutput) {
     
            //Iterate amortization through the life of the loan
            for (int payment = 1; payment <= term * 12; payment++) {
     
                //Current monthly interest
                double currentMonthlyInterest = principal * (interestRate / 100) / 12;
                double newPrincipal = monthlyPayment - currentMonthlyInterest;
                double newPrincipalBalance = principal - newPrincipal;
                principal = newPrincipalBalance;
     
                //Print out the results of the loan amortization to the console
                AmortizationOutput.append("Payment " + payment + "\n");
                AmortizationOutput.append("Monthly Interest Paid = " + 
                        (Money.format(currentMonthlyInterest)) + "\n");
                AmortizationOutput.append("New Loan Balance = " + 
                        (Money.format(newPrincipalBalance)) + "\n");
                AmortizationOutput.append("\n");
            }
        }
    }

    import javax.swing.JTextArea;
     
    public class GUIOutput {
        public void ClearjTextAreaMonthlyPayment(JTextArea LoanInfoOutput) {
            LoanInfoOutput.setText("");
        }
     
        public void ClearjTextAreaAmortizationInfo(JTextArea AmortizationOutput) {
            AmortizationOutput.setText("");
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,823
    Thanks
    55
    Thanked 2,344 Times in 2,315 Posts

    Default Re: Parsing from a GUI text field

    Can you explain your problem?
    Getting a String from a text field
    Parsing a String
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Parsing from a GUI text field

    This is a mortgage calculator made up of three classes; GUI, GUIOutput, and MortgageCalculator, The GUIOutput class is still under development and not being used. The GUI class has three JTextFields for principal, interest rate, and term. I need to get the string values that would be entered in the fields to be converted to doubles so that they can be used in the calculations that are in the MortgageCalculator class.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,529
    My Mood
    Hungover
    Thanks
    144
    Thanked 647 Times in 552 Posts

    Default Re: Parsing from a GUI text field

    And which part of that are you having trouble with? Getting the String from the text field? Converting it to a double? Using it in calculations? Getting it into the other class?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,823
    Thanks
    55
    Thanked 2,344 Times in 2,315 Posts

    Default Re: Parsing from a GUI text field

    I need to get the string values that would be entered in the fields
    Where are the textfields defined? Can you access them directly and call any of their methods or do you need methods in the class that contains the textfields that you can call that will get the contents of the textfields and return them?
    The GUI class needs to have methods the MortgageCalculator class can call to get the values it needs.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Dec 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Parsing from a GUI text field

    As of right now the principal, interest rate, and term are hard coded in the Mortgagecalculator class. Ultimately I want to pull the value entered into the given text field and use it in the calculation. For Example A user will enter the value 1000 into the text field labeled principal of the GUI. When the press the "Submit" button. that value will be passed into the MortgageCalculator class, converted to a double and assigned to the principal variable so that it can be used in the calculations. I'm really not quite sure how I can better explain this.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,823
    Thanks
    55
    Thanked 2,344 Times in 2,315 Posts

    Default Re: Parsing from a GUI text field

    One class has the data, another class wants it.
    The class that wants it needs to call a method in the class that has it.
    Or the class that has the data can call a method in the class that wants it and pass it the data as a parameter in the method.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Dynamic Number Of Text Field
    By HamedAli in forum AWT / Java Swing
    Replies: 3
    Last Post: April 18th, 2012, 09:09 AM
  2. Text in field objects go out of bounds..
    By imsuperman05 in forum AWT / Java Swing
    Replies: 2
    Last Post: April 5th, 2012, 07:35 PM
  3. Tic Tac Toe Java Application Help w/ Importing Text Field
    By Big Bundy in forum What's Wrong With My Code?
    Replies: 10
    Last Post: March 30th, 2011, 03:08 AM
  4. Replies: 3
    Last Post: February 21st, 2011, 07:25 PM
  5. [SOLVED] Parsing a text file in java
    By tccool in forum What's Wrong With My Code?
    Replies: 13
    Last Post: November 16th, 2010, 05:23 AM