public void itemStateChanged(ItemEvent e){}

Hello! I am a student majoring in IT - Programming and have very little experience writing code and really need some help here. I am having trouble with my code. I seems that I have the correct braces, however, I could use some guidance on this. I've posted my code for full view:

import java.awt.*;// a java class that provides graphical user interfaces (GUIs)

import java.awt.event.*;// the listener interface for receiving item events

import javax.swing.*;// a GUI component kit for building UIs & handling end user input

import java.awt.Graphics;//

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.ItemEvent;

import java.awt.event.ItemListener;

import java.text.NumberFormat;

import java.util.*;

//import javax.swing.border.TitledBorder;// interface to describe the border around the edges of a swing component.

class GraphPanel extends JPanel {// open graph panel class

int[] bars = new int[0];

public void setValues(int[] values) {

bars = values;

}

public void paint(Graphics g) {

Graphics2D g2d = (Graphics2D) g;

g2d.setBackground(Color.BLUE);

g2d.clearRect(0, 0, getWidth(), getHeight());

}

}

public class ElizabethJacksonMortgageWeek2 extends JFrame implements ItemListener, ActionListener {

NumberFormat formatter = NumberFormat.getCurrencyInstance(Locale.US);/*currency number formatter*/

/* Defining and Using JPanel to set-up and hold the labels & text fields components */

JPanel p1 = new JPanel();

JLabel loanlabel = new JLabel("Enter Loan Amount, no commas: ", JLabel.RIGHT);

JTextField loanamount = new JTextField(10);

JPanel p2 = new JPanel();

JLabel termlabel = new JLabel("Enter Loan Term: ", JLabel.RIGHT);

JTextField term = new JTextField(10);

JPanel p3 = new JPanel();

JLabel interestlabel = new JLabel("Enter Mortgage Rate, with appropriate decimal: ", JLabel.RIGHT);

JTextField interestrate = new JTextField(10);

JPanel p4 = new JPanel();

JLabel paymentlabel = new JLabel("Your monthly payment is: ", JLabel.RIGHT);

JTextField monthlypayment = new JTextField(10);

JPanel p5 = new JPanel();

JButton calc = new JButton("Calculate Mortgage Payment");

JButton reset = new JButton("Reset");

JButton cancel = new JButton("Cancel");

GraphPanel p6 = new GraphPanel();

public ElizabethJacksonMortgageWeek2(){

super(" Free Mortgage Calculator");

setSize(700, 800);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Container pane = getContentPane();

pane.setLayout(layout);

GridLayout layout = new GridLayout(10, 20, 30, 5);

FlowLayout layout1 = new FlowLayout(FlowLayout.LEFT, 5, 5);

p1.setLayout(layout1);

p1.addLabel(loanLabel);

p1.add(loanamount);

pane.add(p1);

FlowLayout layout2 = new FlowLayout(FlowLayout.LEFT, 5, 5);

p2.setLayout(layout2);

p2.add(termLabel);

p2.add(term);

pane.add(p2);

FlowLayout layout3 = new FlowLayout(FlowLayout.LEFT, 5, 5);

p3.setLayout(layout3);

p3.add(interestLabel);

p3.add(interestrate);

pane.add(p3);

FlowLayout layout4 = new FlowLayout(FlowLayout.LEFT, 5, 5);

p4.setLayout(layout4);

p4.add(paymentLabel);

p4.add(paymentamount);

pane.add(p4);

FlowLayout layout5 = new FlowLayout(FlowLayout.CENTER, 5, 5);

p5.setLayout(layout5);

p5.add(calc);

p5.add(reset);

p5.add(cancel);

ElizabethJacksonMortgageWeek2.this.setVisible(false);

pane.add(p5);

FlowLayout layout6 = new FlowLayout(FlowLayout.LEFT);

pane.add(p6);

setVisible(true);

/* Register listeners */

calc.addActionListener(this);

reset.addActionListener(this);

cancel.addActionListener(this);

pack();

}

//public static void main(String[] arguments) {

//ElizabethJacksonMortgageWeek2 frame = new ElizabethJacksonMortgageWeek2();

//}

/* Function to handle action events */

public void actionPerformed(ActionEvent e) {

/*reset input text fields*/

if(e.getSource().equals(reset))

loanamount.setText(null);

term.setText(null);

interestrate.setText(null);

monthlypayment.setText(null);

p6.setValues(new int[0]);

/* exiting the application*/

if(e.getSource().equals(cancel))

System.exit(0);

/*calculate the monthly mortgage payment*/

if(e.getSource().equals(calc));

}

public static void main (String args [ ]) {

ElizabethJacksonMortgageWeek2 frame = new ElizabethJacksonMortgageWeek2();

double prvalue, rate, interest;/* Modern processors that have been optimized for high-speed mathematical calculations */

interest = (rate/100);/* Monthly interest rate */

int years;/* Number of years on loan */

interest = ((prvalue * rate) / 12);

/* Computation of monthly mortgage payment */

double mopay = (prvalue * interest) / (1 - Math.pow(1/(1 + interest), years * 12));

//monthlypayment.setText(formatter.format(mopay));

}

public void itemStateChanged(ItemEvent e){}

[5311 byte] By [machine2a] at [2007-10-1 16:37:09]
# 1
If you're going to post code please wrap it in the code tags. It makes it MUCH easier to read.Just click the [Code] button, then paste your code between the [code] and [/code] tags.Regards,Tim
TimRyanNZa at 2007-7-11 0:58:18 > top of Java-index,Archived Forums,Java 2 Software Development Kit (J2SE SDK)...
# 2

Hi,

You have 2 classes in that code you have posted. Java requires that each class be in its own file. Thuis graphPanel and ElizabethJacksonMortgageWeek2 need to be in separate files. Hope you have this right.

The other thing I found was you need one more closing brace for your class.

public void itemStateChanged(ItemEvent e){}

} <--

This is the sort of thing any IDE is equipped to do and you would do well to get yourself one. I recommend IntelliJ or JBuilder enterprise.

Hope this helps.

Cheers,

vidyut

vidyuta at 2007-7-11 0:58:18 > top of Java-index,Archived Forums,Java 2 Software Development Kit (J2SE SDK)...
# 3

> Java requires that each class be in its own

> file.

No it doesn't.

> Thuis graphPanel and

> ElizabethJacksonMortgageWeek2 need to be in separate

> files.

No they don't need to be in separate files. The restriction says there can be at most one top-level public class in a source file. GraphPanel is not a public class, so it can certainly be in the same source file as ElizabethJacksonMortgageWeek2.

> Hope you have this right.

You need to get some things right too...

Torgila at 2007-7-11 0:58:18 > top of Java-index,Archived Forums,Java 2 Software Development Kit (J2SE SDK)...