![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jul 2005
Posts: 15
Rep Power: 0
![]() |
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? |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Jun 2005
Posts: 16
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#3 |
|
Programming Guru
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|