GUI not executing properly

My GUI (Netbeans) will not work properly

Page 1 of 1

4 Replies - 985 Views - Last Post: 22 March 2009 - 09:18 AM Rate Topic: -----

#1 yooper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 13-March 09

GUI not executing properly

Post icon  Posted 21 March 2009 - 08:47 PM

I am having problems getting my gui assignment to execute. This project is in a JFrame and the goal is to get a loan calculator to execute in a GUI format. My GUI is displayed, however, the fields and buttons don't do anything.
Any assistance would be appreciated.

Thank you. Yooper


package autocalc;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
*
/*
* AutoCalc.java
*
* Created on Mar 21, 2009, 6:33:48 PM
*/

public class AutoCalc extends javax.swing.JFrame {
private double interestRate;
private double autoPrice = 1300.00;
private double downPayment = 100.00;
private int loanLength = 1;
private int numberOfPayments;
private double totalLoan;
private double monthlyPayment;

/** Creates new form AutoCalc */
public AutoCalc() {
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">
private void initComponents() {

buttonGroup1 = new javax.swing.ButtonGroup();
jInternalFrame1 = new javax.swing.JInternalFrame();
autoPriceLabel = new javax.swing.JLabel();
DownPaymentLabel = new javax.swing.JLabel();
loanLengthLabel = new javax.swing.JLabel();
montlyPaymentLabel = new javax.swing.JLabel();
initialDownPaymentField = new javax.swing.JFormattedTextField();
loanLengthField = new javax.swing.JFormattedTextField();
nonthlyPaymentField = new javax.swing.JFormattedTextField();
calculateMonthlyPaymentButton = new javax.swing.JButton();
autoPriceField = new javax.swing.JFormattedTextField();
greatCreditButton = new javax.swing.JRadioButton();
averageCreditButton = new javax.swing.JRadioButton();
poorCreditButton = new javax.swing.JRadioButton();
greatCreditLabel = new javax.swing.JLabel();
averageCreditLabel = new javax.swing.JLabel();
poorCreditLabel = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jInternalFrame1.setVisible(true);

autoPriceLabel.setText("Total Auto Purchase Price");

DownPaymentLabel.setText("Down Payment Amount");

loanLengthLabel.setText("Loan Length (yrs)");

montlyPaymentLabel.setText("Monthly Payment");

loanLengthField.setText("jFormattedTextField3");
loanLengthField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
loanLengthFieldActionPerformed(evt);
}
});

nonthlyPaymentField.setText("jFormattedTextField4");

calculateMonthlyPaymentButton.setText("Calculate Monthly Payment");
calculateMonthlyPaymentButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
calculateMonthlyPaymentButtonMouseClicked(evt);
}
});

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

greatCreditButton.setText("jRadioButton1");

averageCreditButton.setText("jRadioButton2");

poorCreditButton.setText("jRadioButton3");

greatCreditLabel.setText("Great Credit 5% Interest");
greatCreditLabel.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
greatCreditLabelMouseClicked(evt);
}
});

averageCreditLabel.setText("Average Credit 10% Interest");

poorCreditLabel.setText("Poor Credit 12% Interest");

javax.swing.GroupLayout jInternalFrame1Layout = new javax.swing.GroupLayout(jInternalFrame1.getContentPane());
jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout);
jInternalFrame1Layout.setHorizontalGroup(
jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jInternalFrame1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(DownPaymentLabel)
.addComponent(autoPriceLabel)
.addComponent(loanLengthLabel)
.addComponent(montlyPaymentLabel)
.addGroup(jInternalFrame1Layout.createSequentialGroup()
.addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(jInternalFrame1Layout.createSequentialGroup()
.addComponent(averageCreditButton, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(5, 5, 5))
.addGroup(jInternalFrame1Layout.createSequentialGroup()
.addComponent(greatCreditButton, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
.addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(averageCreditLabel)
.addComponent(greatCreditLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)))
.addGroup(jInternalFrame1Layout.createSequentialGroup()
.addComponent(poorCreditButton, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(poorCreditLabel)))
.addGap(20, 20, 20)
.addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(initialDownPaymentField)
.addComponent(loanLengthField)
.addComponent(nonthlyPaymentField)
.addComponent(autoPriceField))
.addGap(362, 362, 362))
.addGroup(jInternalFrame1Layout.createSequentialGroup()
.addGap(189, 189, 189)
.addComponent(calculateMonthlyPaymentButton)
.addContainerGap(339, Short.MAX_VALUE))
);
jInternalFrame1Layout.setVerticalGroup(
jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jInternalFrame1Layout.createSequentialGroup()
.addGap(23, 23, 23)
.addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(autoPriceLabel)
.addComponent(autoPriceField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(DownPaymentLabel)
.addComponent(initialDownPaymentField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(loanLengthLabel)
.addComponent(loanLengthField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(montlyPaymentLabel)
.addComponent(nonthlyPaymentField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(11, 11, 11)
.addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(greatCreditLabel)
.addComponent(greatCreditButton))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(averageCreditButton)
.addComponent(averageCreditLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(poorCreditButton)
.addComponent(poorCreditLabel))
.addGap(12, 12, 12)
.addComponent(calculateMonthlyPaymentButton)
.addContainerGap(88, 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()
.addComponent(jInternalFrame1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jInternalFrame1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
);

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

private void greatCreditLabelMouseClicked(java.awt.event.MouseEvent evt) {

//Use of if else if statement for calculating interest rates
if (greatCreditButton.isSelected())
interestRate =.05; //interest rate for great credit

else if (averageCreditButton.isSelected())
interestRate = .10; //interest rate for average credit

else if (poorCreditButton.isSelected())
interestRate = .12; //interest rate for poor credit

//Get Numbers from Text Fields
autoPrice = Double.parseDouble(autoPurchasePriceField.getText());

downPayment = Double.parseDouble(initialDownPaymentField.getText());

loanLength = Integer.parseInt(loanLengthField.getText());

//Calculate Number of Payments
numberOfPayments = loanLength * 12;

//Calculate and Add Interest to Amount of Loan
totalLoan = (autoPrice - downPayment);

totalLoan = (totalLoan * interestRate) + totalLoan;

monthlyPayment = totalLoan /numberOfPayments;

//Set monthlyPaymentField
monthlyPaymentField.setValue(monthlyPayment);













}

private void calculateMonthlyPaymentButtonMouseClicked(java.awt.event.MouseEvent evt) {

}

private void autoPriceFieldActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void loanLengthFieldActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new AutoCalc().setVisible(true);
}

});
}

// Variables declaration - do not modify
private javax.swing.JLabel DownPaymentLabel;
private javax.swing.JFormattedTextField autoPriceField;
private javax.swing.JLabel autoPriceLabel;
private javax.swing.JRadioButton averageCreditButton;
private javax.swing.JLabel averageCreditLabel;
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JButton calculateMonthlyPaymentButton;
private javax.swing.JRadioButton greatCreditButton;
private javax.swing.JLabel greatCreditLabel;
private javax.swing.JFormattedTextField initialDownPaymentField;
private javax.swing.JInternalFrame jInternalFrame1;
private javax.swing.JFormattedTextField loanLengthField;
private javax.swing.JLabel loanLengthLabel;
private javax.swing.JLabel montlyPaymentLabel;
private javax.swing.JFormattedTextField nonthlyPaymentField;
private javax.swing.JRadioButton poorCreditButton;
private javax.swing.JLabel poorCreditLabel;
// End of variables declaration

}

Is This A Good Question/Topic? 0
  • +

Replies To: GUI not executing properly

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: GUI not executing properly

Posted 22 March 2009 - 05:56 AM

Welcome at DIC
Please respect rule #4 that was shown to you when you wote your post
:code:

Now as far as your GUI is concerned there is another issue.
We correct (or try to correct) GUI written by human beings but not code of GUI generated by a GUI generator.
A human can probably write your program with almost 1/10th the number of lines of code that you have.

When you use a GUI generator you may have forgot to check a checkbox somewhere or to select a radio button somewhere else and then the GUI generator generates hundredh of lines of code really hard to understand for human using weird layouts that programmers never use.

So write your GUI yourself (it is a very profitable exercise: one day you will have to open the one written by somebody else) and we will be glad to fix it but don't expect us to figure out what you did wrong building your GUI through a GUI generator.

Happy coding

This post has been edited by pbl: 22 March 2009 - 05:58 AM

Was This Post Helpful? 1
  • +
  • -

#3 Charlie IronGleet  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Re: GUI not executing properly

Posted 22 March 2009 - 07:35 AM

I'm glad you posted this question. I was myself just wondering why GUIs are still hand coded and now I totally understand why. I'm just a novice and have just begun learning how to write GUI's but this guy is right -- it can be done with so much less code!

Amazing!

Why do automated GUI generators spit out sooo much code like this???

Charles IronGleet
Was This Post Helpful? 0
  • +
  • -

#4 Charlie IronGleet  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Re: GUI not executing properly

Posted 22 March 2009 - 08:12 AM

The difference in code is dramatic. I just put together this applet with a basic gui to calculate loans.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;

public class LoanApplet extends JApplet {
 
  private JTextField jtfAnnualInterestRate = new JTextField();
  private JTextField jtfNumberOfYears = new JTextField();
  private JTextField jtfLoanAmount = new JTextField();
  private JTextField jtfMonthlyPayment = new JTextField();
  private JTextField jtfTotalPayment = new JTextField();
  private JButton jbtComputeLoan = new JButton("Compute Payment");

  public void init() {
   
	jtfMonthlyPayment.setEditable(false);
	jtfTotalPayment.setEditable(false);
	jtfAnnualInterestRate.setHorizontalAlignment(JTextField.RIGHT);
	jtfNumberOfYears.setHorizontalAlignment(JTextField.RIGHT);
	jtfLoanAmount.setHorizontalAlignment(JTextField.RIGHT);
	jtfMonthlyPayment.setHorizontalAlignment(JTextField.RIGHT);
	jtfTotalPayment.setHorizontalAlignment(JTextField.RIGHT);

	JPanel p1 = new JPanel(new GridLayout(5, 2));
	p1.add(new JLabel("Annual Interest Rate"));
	p1.add(jtfAnnualInterestRate);
	p1.add(new JLabel("Number of Years"));
	p1.add(jtfNumberOfYears);
	p1.add(new JLabel("Loan Amount"));
	p1.add(jtfLoanAmount);
	p1.add(new JLabel("Monthly Payment"));
	p1.add(jtfMonthlyPayment);
	p1.add(new JLabel("Total Payment"));
	p1.add(jtfTotalPayment);
	p1.setBorder(new
	  TitledBorder("Enter interest rate, year and loan amount"));
	JPanel p2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
	p2.add(jbtComputeLoan);
	add(p1, BorderLayout.CENTER);
	add(p2, BorderLayout.SOUTH);
	jbtComputeLoan.addActionListener(new ButtonListener());
  }

  
  private class ButtonListener implements ActionListener {

	public void actionPerformed(ActionEvent e) {
	  double interest =
		Double.parseDouble(jtfAnnualInterestRate.getText());
	  int year =
		Integer.parseInt(jtfNumberOfYears.getText());
	  double loanAmount =
		Double.parseDouble(jtfLoanAmount.getText());
	  Loan loan = new Loan(interest, year, loanAmount);
	  jtfMonthlyPayment.setText(String.format("%.2f",
		loan.getMonthlyPayment()));
	  jtfTotalPayment.setText(String.format("%.2f",
		loan.getTotalPayment()));
	}
  }
}



I guess brevity truly IS the soul of clarity :>

May your stones pass w/o pain,
IronGleet

This post has been edited by Charlie IronGleet: 22 March 2009 - 08:13 AM

Was This Post Helpful? 0
  • +
  • -

#5 saumya9888  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 08-August 08

Re: GUI not executing properly

Posted 22 March 2009 - 09:18 AM

coding gui by yourself may be good practice but it doesnt mean that gui builders are not good...now a novice should not start from a gui builder thats a truth because you will be just dragging and dropping without caring whats going on behind the scenes...but when you know whats happening inside i feel that using gui builder not only saves time but also make the gui resize compatible and clean...
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1