Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 6th, 2005, 12:18 PM   #1
jch02140
Newbie
 
Join Date: Jul 2005
Posts: 15
Rep Power: 0 jch02140 is on a distinguished road
Question Run-time problem

Hi I am trying to figure what I have done wrong at the code below.

Whenever I changed the value of any field, it prints out tons of error messages in the console...

import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;

public class AudioVideoItemDialog extends JDialog {
	//views
	private JTextField authorTextField;
	private JTextField titleTextField;
	private JTextField priceTextField;
	private JRadioButton audioRB, videoRB;

	private JSpinner aSpinner;
	private JButton okButton, cancelButton;

	//model
	private StoreItem aStoreItem;

	//transient model
	private String author, title;
	private int quantity;
	private float price;


	//Listeners
	private DocumentListener authorFieldListener;
	private DocumentListener titleFieldListener;
	private DocumentListener studioFieldListener;
	private ChangeListener spinnerListener;
	private DocumentListener priceFieldListener;

	//status
	private int status;
	public static final int APPROVE_OPTION = 0;
	public static final int CANCEL_OPTION = 1;

	public AudioVideoItemDialog(JFrame anOwner, boolean aBoolean, StoreItem 
anItem) {
		super(anOwner, aBoolean);
		initialize(anItem);
	}

	public void initialize(StoreItem anItem) {
		aStoreItem = anItem;
		author=anItem.getAuthor();
		title = anItem.getTitle();
		quantity = anItem.getQuantity();
		price = anItem.getPrice();

		setTitle("Edit audio/video item");
		setLocation(100, 100);
		setSize(400, 300);
		setResizable(false);
		addWindowListener(new WindowAdapter() {
			public void windowOpened(WindowEvent anEvent) {
				update();
			}
			public void windowClosing(WindowEvent anEvent) {
				cancelButtonClicked();
			}
		});

		//build the views and set all the Listeners
		GridLayout layout = new GridLayout(6,2,5,5);
		getContentPane().setLayout(layout);

		JLabel labelAuthor = new JLabel("Author: ");
		labelAuthor.setHorizontalAlignment(SwingConstants.RIGHT);
		getContentPane().add(labelAuthor);

		authorTextField = new JTextField();
		authorFieldListener = new DocumentListener() {
			public void changedUpdate(DocumentEvent theEvent) {
				changedAuthorTextField();
			}
			public void insertUpdate(DocumentEvent theEvent) {
				changedAuthorTextField();
			}
			public void removeUpdate(DocumentEvent theEvent) {
				changedAuthorTextField();
			}
		};
		authorTextField.getDocument().addDocumentListener(authorFieldListener);
		getContentPane().add(authorTextField);

		JLabel labelArtist1 = new JLabel("Title: ");
		labelArtist1.setHorizontalAlignment(SwingConstants.RIGHT);
		getContentPane().add(labelArtist1);

		titleTextField = new JTextField();
		titleFieldListener = new DocumentListener() {
			public void changedUpdate(DocumentEvent theEvent) {
				changedTitleTextField();
			}
			public void insertUpdate(DocumentEvent theEvent) {
				changedTitleTextField();
			}
			public void removeUpdate(DocumentEvent theEvent) {
				changedTitleTextField();
			}
		};
		titleTextField.getDocument().addDocumentListener(titleFieldListener);
		getContentPane().add(titleTextField);



		//third row
		JLabel empty=new JLabel("");
		getContentPane().add(empty);

		JPanel panelRB=new JPanel();         //here you need to add RadioButtonListener
    	ButtonGroup aButtonGroup=new ButtonGroup();
    	audioRB=new JRadioButton("Audio",true);
    	aButtonGroup.add(audioRB);
    	panelRB.add(audioRB);

    	videoRB=new JRadioButton("Video");
    	aButtonGroup.add(videoRB);
    	panelRB.add(videoRB);

    	getContentPane().add(panelRB);

		JLabel labelQuantity = new JLabel("Quantity: ");
		labelQuantity.setHorizontalAlignment(SwingConstants.RIGHT);
		getContentPane().add(labelQuantity);

		aSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 100, 1));
		spinnerListener = new ChangeListener() {
			public void stateChanged(ChangeEvent anEvent) {
				changedSpinner();
			}
		};
		aSpinner.addChangeListener(spinnerListener);
		getContentPane().add(aSpinner);

		JLabel labelPrice = new JLabel("Price: ");
		labelPrice.setHorizontalAlignment(SwingConstants.RIGHT);
		getContentPane().add(labelPrice);

		priceTextField = new JTextField();
		priceFieldListener = new DocumentListener() {
			public void changedUpdate(DocumentEvent theEvent) {
				changedPriceTextField();
			}
			public void insertUpdate(DocumentEvent theEvent) {
				changedPriceTextField();
			}
			public void removeUpdate(DocumentEvent theEvent) {
				changedPriceTextField();
			}
		};
		priceTextField.getDocument().addDocumentListener(priceFieldListener);
		getContentPane().add(priceTextField);

		JLabel labelEmpty = new JLabel("");
		getContentPane().add(labelEmpty);

		JPanel aPanel = new JPanel();
		okButton = new JButton("Ok");
		okButton.setMnemonic('O');
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent anEvent) {
				okButtonClicked();
			}
		});
		aPanel.add(okButton);

		cancelButton = new JButton("Cancel");
		cancelButton.setMnemonic('C');
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent anEvent) {
				cancelButtonClicked();
			}
		});
		aPanel.add(cancelButton);

		FlowLayout aLayout = new FlowLayout();
		aLayout.setAlignment(FlowLayout.RIGHT);
		aPanel.setLayout(aLayout);
		getContentPane().add(aPanel);
	}

	    private void changedAuthorTextField() {
			author = authorTextField.getText();
			update();
		}

		private void changedTitleTextField() {
			title = titleTextField.getText();
			update();
		}


		private void changedSpinner(){
			quantity = ((Integer) aSpinner.getValue()).intValue();
			update();
		}

		private void changedPriceTextField(){
			try {
				price = Float.parseFloat(priceTextField.getText());
			}
			catch (NumberFormatException anException) {
			// do nothing
			}
			update();
		}

		private void okButtonClicked() {
			aStoreItem.setAuthor(author);
			aStoreItem.setTitle(title);
			aStoreItem.setQuantity(quantity);
			aStoreItem.setPrice(price);
			status = APPROVE_OPTION;
			dispose();
		}

		private void cancelButtonClicked() {
			status = CANCEL_OPTION;
			dispose();
		}

		//update
		private void update() {
			disableListeners();
			updateAuthorTextField();
			updateTitleTextField();
			updateSpinner();
			updatePriceTextField();
			enableListeners();
		}


		private void updateAuthorTextField() {
			if (!authorTextField.getText().equals(author)) {
				authorTextField.setText(author);
			}
		}

		private void updateTitleTextField() {
			if (!titleTextField.getText().equals(title)) {
				titleTextField.setText(title);
			}
		}


		private void updateSpinner() {
			aSpinner.setValue(new Integer(quantity));
		}

		private void updatePriceTextField() {               //again number format
			try {
				float newPrice = Float.parseFloat(priceTextField.getText());
				if (newPrice != price) {
					priceTextField.setText(new Float(price).toString());
				}
			} catch (NumberFormatException anException) {
				//priceTextField.setText(new Float(price).toString());
			}
		}

		//Enable all the Listeners
		private void enableListeners(){
			authorTextField.getDocument().addDocumentListener(authorFieldListener);
			titleTextField.getDocument().addDocumentListener(titleFieldListener);
			aSpinner.addChangeListener(spinnerListener);
		}

		//Disable all the Listeners
		private void disableListeners(){
			authorTextField.getDocument().removeDocumentListener(authorFieldListener);
			titleTextField.getDocument().removeDocumentListener(titleFieldListener);
			aSpinner.removeChangeListener(spinnerListener);
		}

		public StoreItem getItem() {
			return aStoreItem;
		}

		public int getStatus() {
			return status;
		}
}

What have I done wrong?
jch02140 is offline   Reply With Quote
Old Aug 8th, 2005, 8:07 PM   #2
proghelper
Newbie
 
Join Date: Jun 2005
Posts: 16
Rep Power: 0 proghelper is on a distinguished road
Lightbulb

In the error messages you get on the console, you would get the line numbers of the code corresponding to error location.

You can use those information to help debug the program.

---------------------

Programming ( Assignment / Project ) Help
proghelper is offline   Reply With Quote
Old Aug 9th, 2005, 2:34 AM   #3
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
can you output the error messages please.
__________________
"Put your hand on a hot stove for a minute, and it seems like an hour. Sit with a pretty girl for an hour, and it seems like a minute. THAT'S relativity."

- Albert Einstein
Berto is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:20 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC