Hi ,

For the following code , I intended to have a dialog box with labels balance , payment no : and a button named calculation. When the button will be clicked it should show the next balance according to the equation used in the code .
But its showing balance = 0(initial value for variable class) , payment no =1(the first decleration) and after the desired click its doing nothing. I am also trying to stop it at k=36! I think the problem lies with the event handler!
Can someone please help me out.

Thanks a lot.


import java.text.*; 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.Math.*; 

public class BalanceRemaining 

	private static int k =1;
	private static int n =36;
	private static double i=.0075;
	private static double payment = 165.25;
	private static double balance  ;
	private static JLabel balanceLabel;
	private static JLabel paymentNo;
	private static JButton calculateButton;
	private static DecimalFormat dollar = new DecimalFormat( "$###,##0.00" ); 

	private static class ButtonHandler implements ActionListener 
	/*Can not understand the implementation ! Should show the initial balance when   k = 1 and then after each  click the next balance*What is the error here!:S!?/
		public void actionPerformed(ActionEvent event)
			while ( k<36)
			balance = payment*((1-Math.pow((1+i),(double)(k-n)))/i); 
	public static void main (String[] args)
		JFrame outputFrame;
		Container outputPane;
		ButtonHandler buttonListener;
		outputFrame= new JFrame();
		outputPane.setLayout(new GridLayout(3,1));
		[balanceLabel= new JLabel (dollar.format(balance), JLabel.LEFT); 
		paymentNo= new JLabel("Payment Number: "+ k);
		calculateButton= new JButton("Calculate");
		buttonListener = new ButtonHandler();
8 Years
Discussion Span
Last Post by haquer02

In situations like this I always look at the "static" aspects of my code first and try to get rid of as much "static" code as possible. I would create a constructor and stick most of my code from "main" into that constructor. I've never been all that great at using the "static" modifier, butn I generally use it as little as possible. Sometimes when I get it working without the "static" modifier, I start sticking it back in, but I usually don't bother.

Right now buttonListener doesn't belong to a class. I think you want to make it a data member of a class.


Got it fixed , my main problem was the loop ... when the event listener object called the event handler by clicking the button it calculated the whole thing after a click ...should have been an increment after each click (using if loop for that). Basic logic error from my side.

I used the static aspect cause this class is not going to be part of a package or it's not going to be called ever by me!!

Still a beginner , learning the basics

N:B: I forgot to put in the label modifiers on the previous code !

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.