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 3 of 3

Thread: GUI help

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

    Default GUI help

    Hi can someone take a look at this and help me to figure out how to fix the code? I am using netbeans (as a requirement) and am trying to get my mortgage calculator GUI to work. Im not necessarily getting errors but Im just not to sure what code I am missing because I am not getting any results. The GUI comes up and I am able to enter the loan amount, interest rate, and years but when I hit calculate I get no results.

    package MortgageCalculator;

    import javax.swing.JTextField;


    /**
    *
    * @author
    */
    public class MortgageCalculatorGUI extends javax.swing.JFrame {

    /**
    * Creates new form MortgageCalculatorGUI
    */
    public MortgageCalculatorGUI() {
    initComponents();
    }


    private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    loanLabel = new javax.swing.JLabel();
    interestLabel = new javax.swing.JLabel();
    yearsLabel = new javax.swing.JLabel();
    calculatejButton = new javax.swing.JButton();
    clearjButton = new javax.swing.JButton();
    exitjButton = new javax.swing.JButton();
    loanAmountText = new javax.swing.JTextField();
    interestRateText = new javax.swing.JTextField();
    loanYearsText = new javax.swing.JTextField();
    jScrollPane1 = new javax.swing.JScrollPane();
    jTextArea1 = new javax.swing.JTextArea();

    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);

    loanLabel.setText("Loan Amount");

    interestLabel.setText("Interest Rate");

    yearsLabel.setText("Loan Years");

    calculatejButton.setText("Calculate");
    calculatejButton.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    calculatejButtonActionPerformed(evt);
    }
    });

    clearjButton.setText("Clear");
    clearjButton.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    clearjButtonActionPerformed(evt);
    }
    });

    exitjButton.setText("Exit");
    exitjButton.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    exitjButtonActionPerformed(evt);
    }
    });

    loanAmountText.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    loanAmountTextActionPerformed(evt);
    }
    });

    interestRateText.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    interestRateTextActionPerformed(evt);
    }
    });

    loanYearsText.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    loanYearsTextActionPerformed(evt);
    }
    });

    jTextArea1.setColumns(20);
    jTextArea1.setRows(5);
    jTextArea1.setBorder(null);
    jScrollPane1.setViewportView(jTextArea1);

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
    jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addGap(20, 20, 20)
    .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.TRAILING)
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addComponent(loanLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
    .addComponent(loanAmountText, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(0, 0, Short.MAX_VALUE))
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.TRAILING)
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addComponent(calculatejButton)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    .addComponent(clearjButton, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addGap(0, 0, Short.MAX_VALUE)
    .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.LEADING)
    .addComponent(yearsLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(interestLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
    .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.LEADING)
    .addComponent(loanYearsText, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(interestRateText, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))))
    .addGap(28, 28, 28)
    .addComponent(exitjButton, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(45, 45, 45))))
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addGap(49, 49, 49)
    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 262, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(0, 0, Short.MAX_VALUE))
    );
    jPanel1Layout.setVerticalGroup(
    jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addGap(12, 12, 12)
    .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.TRAILING)
    .addComponent(loanLabel)
    .addComponent(loanAmountText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGap(23, 23, 23)
    .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.BASELINE)
    .addComponent(interestLabel)
    .addComponent(interestRateText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGap(23, 23, 23)
    .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.BASELINE)
    .addComponent(yearsLabel)
    .addComponent(loanYearsText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGap(33, 33, 33)
    .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.BASELINE)
    .addComponent(calculatejButton)
    .addComponent(clearjButton)
    .addComponent(exitjButton))
    .addGap(26, 26, 26)
    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addContainerGap(55, Short.MAX_VALUE))
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(0, 23, Short.MAX_VALUE)
    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(0, 23, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(29, 29, 29)
    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addContainerGap(javax.swing.GroupLayout.DEFAULT_S IZE, Short.MAX_VALUE))
    );

    pack();
    }// </editor-fold>

    private void exitjButtonActionPerformed(java.awt.event.ActionEv ent evt) {
    System.exit(0);
    }

    private void clearjButtonActionPerformed(java.awt.event.ActionE vent evt) {
    loanYearsText.setText(null);
    loanAmountText.setText(null);
    interestRateText.setText(null);
    }

    private void loanAmountTextActionPerformed(java.awt.event.Actio nEvent evt) {

    }

    private void interestRateTextActionPerformed(java.awt.event.Act ionEvent evt) {

    }

    private void loanYearsTextActionPerformed(java.awt.event.Action Event evt) {

    }

    private void calculatejButtonActionPerformed(java.awt.event.Act ionEvent evt) {
    double loanAmount = 0.0;
    double loanYears = 0.0;
    double loanInterest = 0.0;
    double monthlyPayment;

    // Parse the text to a type double
    loanAmount = Double.parseDouble(jTextArea1.getText());
    loanYears = Double.parseDouble(jTextArea2.getText()); //I think this is wrong
    loanInterest = Double.parseDouble(jTextField3.getText()); // I think this is wrong

    // Perform the calculation
    monthlyPayment = loanAmount * ( (loanInterest /100/12) / (1 - Math.pow((1+(loanInterest /100/12)),-1*(loanYears*12) ) ));


    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {

    {
    for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
    if ("Nimbus".equals(info.getName())) {
    javax.swing.UIManager.setLookAndFeel(info.getClass Name());
    break;
    }
    }
    } catch (ClassNotFoundException ex) {
    java.util.logging.Logger.getLogger(MortgageCalcula torGUI.class.getName()).log(java.util.logging.Leve l.SEVERE, null, ex);
    } catch (InstantiationException ex) {
    java.util.logging.Logger.getLogger(MortgageCalcula torGUI.class.getName()).log(java.util.logging.Leve l.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
    java.util.logging.Logger.getLogger(MortgageCalcula torGUI.class.getName()).log(java.util.logging.Leve l.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
    java.util.logging.Logger.getLogger(MortgageCalcula torGUI.class.getName()).log(java.util.logging.Leve l.SEVERE, null, ex);
    }


    /*
    * Create and display the form
    */
    java.awt.EventQueue.invokeLater(new Runnable() {

    public void run() {
    new MortgageCalculatorGUI().setVisible(true);
    }
    });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton calculatejButton;
    private javax.swing.JButton clearjButton;
    private javax.swing.JButton exitjButton;
    private javax.swing.JLabel interestLabel;
    private javax.swing.JTextField interestRateText;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField loanAmountText;
    private javax.swing.JLabel loanLabel;
    private javax.swing.JTextField loanYearsText;
    private javax.swing.JLabel yearsLabel;
    // End of variables declaration
    }


  2. #2
    Junior Member
    Join Date
    Dec 2011
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: GUI help

    Alot of that code is netbeans generated I think the main portion I am having trouble with is in here

    private void exitjButtonActionPerformed(java.awt.event.ActionEv ent evt) {
    System.exit(0);
    }

    private void clearjButtonActionPerformed(java.awt.event.ActionE vent evt) {
    loanYearsText.setText(null);
    loanAmountText.setText(null);
    interestRateText.setText(null);
    }

    private void loanAmountTextActionPerformed(java.awt.event.Actio nEvent evt) {

    }

    private void interestRateTextActionPerformed(java.awt.event.Act ionEvent evt) {

    }

    private void loanYearsTextActionPerformed(java.awt.event.Action Event evt) {

    }

    private void calculatejButtonActionPerformed(java.awt.event.Act ionEvent evt) {
    double loanAmount = 0.0;
    double loanYears = 0.0;
    double loanInterest = 0.0;
    double monthlyPayment;

    // Parse the text to a type double
    loanAmount = Double.parseDouble(jTextArea1.getText());
    loanYears = Double.parseDouble(jTextArea2.getText()); //I think this is wrong
    loanInterest = Double.parseDouble(jTextField3.getText()); // I think this is wrong

    // Perform the calculation
    monthlyPayment = loanAmount * ( (loanInterest /100/12) / (1 - Math.pow((1+(loanInterest /100/12)),-1*(loanYears*12) ) ));


    }

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: GUI help

    Kindly provide SSCCE