asiavoices
Programmer
Hello again everyone,
I have a problem that I think I might know what may have caused it but I cannot figure out a way to have it work.
Basically,
1. I add items from a form (name, price, etc.) into an ArrayList (I called mine myStuff).
2. I would like to display the items dynamically on another Panel.
3. Once another button (ie. the "Print" button) is clicked it opens up a new panel to show everything.
My problem is that if I create a new ArrayList() before each addition of an item from the form, it will always be one item only and overwrites the previous record.
So how could I make sure that:
a. Everytime it adds a record, it will append it to the ArrayList (myStuff) and not overwrite it.
b. If I want to access to this ArrayList (myStuff) from a method to another method, say display for a Panel, how do I do that?
Here's a section of my code.........
/* =============================
= ItsMe class =
============================= */
public class ItsMe extends JFrame implements ActionListener
{
.............
public static void main(String[] args)
{
ItsMe myLayout = new ItsMe();
myLayout.setVisible(true);
} // End of void main() method
..................
/* ----------- Constructor ------------- */
public ItsMe()
{
.................
descriptionField = new JTextField(15);
descriptionField.addActionListener(this);
priceField = new JTextField(8);
priceField.addActionListener(this);
isTaxField = new JCheckBox("Yes", true);
isTaxField.addActionListener(this);
addButton = new JButton("Purchase"
addButton.addActionListener(this);
.....................
totalButton = new JButton("PRINT"
totalButton.addActionListener(this);
......................
} // End of my constructor
public void actionPerformed(ActionEvent buttonEvent)
{
ArrayList myStuff = new ArrayList();
try
{
Object buttonClicked = buttonEvent.getSource();
if (buttonClicked == addButton)
{
..................
variables .....
if ((descriptionField == null) || (descCharsEntered < minDescChars))
{
errorStatus = 1;
showErrorMsg(1, descCharsEntered, itemEntered);
}
else if (priceCharsEntered < minPriceChars)
{
errorStatus = 1;
showErrorMsg(2, priceCharsEntered, Double.toString(priceEntered));
}
else
{
errorStatus = 0;
Item cidx = new Item(itemEntered, priceEntered , taxValueSelected);
myStuff.add(cidx);
numrecords++;
Vector vec = new Vector(myStuff);
tempVec = new JList(vec);
shoppingCart = new JScrollPane(tempVec, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
.......
descriptionField.setText(""
priceField.setText(""
}
} // End of Purchase button
//If the SUBTOTAL button is pressed
else if (buttonClicked == subTotalButton)
{
.......
}
//If the PRINT button is pressed
else if (buttonClicked == totalButton)
{
........
}
//If the LOGOUT button is pressed
else if (buttonClicked == logOutButton)
{
System.exit(0);
}
}
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "Sorry! Error in price format......" );
priceField.setText(""
errorStatus = 1;
}
} // end of actionPerformed() method
} //end of ItsMe Class
/* =============================
= Item class =
============================= */
public class Item
{
private String itemName;
private double itemPrice;
private boolean itemTax = false;
/* ---- This is my default constructor ------- */
public Item(String theName, double thePrice, boolean theTax)
{
setItemName(theName);
setItemPrice(thePrice);
setItemTax(theTax);
}
/* ---- setItemName() method ------- */
public void setItemName(String theName)
{
itemName = theName;
}
/* ---- setItemPrice() method ------- */
public void setItemPrice(double thePrice)
{
itemPrice = thePrice;
}
/* ---- setItemTax() method ------- */
public void setItemTax(boolean theTax)
{
itemTax = theTax;
}
/* ---- getName() returned method ------- */
public String getName()
{
return itemName;
}
/* ---- getPrice() returned method ------- */
public double getPrice()
{
return itemPrice;
}
/* ---- getTax returned method ------- */
public boolean getTax()
{
return itemTax;
}
public String toString()
{
return " " + itemName + " " + itemPrice + " " + itemTax;
}
} // end of Item Class scope
Thank you!,
Christopher
I have a problem that I think I might know what may have caused it but I cannot figure out a way to have it work.
Basically,
1. I add items from a form (name, price, etc.) into an ArrayList (I called mine myStuff).
2. I would like to display the items dynamically on another Panel.
3. Once another button (ie. the "Print" button) is clicked it opens up a new panel to show everything.
My problem is that if I create a new ArrayList() before each addition of an item from the form, it will always be one item only and overwrites the previous record.
So how could I make sure that:
a. Everytime it adds a record, it will append it to the ArrayList (myStuff) and not overwrite it.
b. If I want to access to this ArrayList (myStuff) from a method to another method, say display for a Panel, how do I do that?
Here's a section of my code.........
/* =============================
= ItsMe class =
============================= */
public class ItsMe extends JFrame implements ActionListener
{
.............
public static void main(String[] args)
{
ItsMe myLayout = new ItsMe();
myLayout.setVisible(true);
} // End of void main() method
..................
/* ----------- Constructor ------------- */
public ItsMe()
{
.................
descriptionField = new JTextField(15);
descriptionField.addActionListener(this);
priceField = new JTextField(8);
priceField.addActionListener(this);
isTaxField = new JCheckBox("Yes", true);
isTaxField.addActionListener(this);
addButton = new JButton("Purchase"
addButton.addActionListener(this);
.....................
totalButton = new JButton("PRINT"
totalButton.addActionListener(this);
......................
} // End of my constructor
public void actionPerformed(ActionEvent buttonEvent)
{
ArrayList myStuff = new ArrayList();
try
{
Object buttonClicked = buttonEvent.getSource();
if (buttonClicked == addButton)
{
..................
variables .....
if ((descriptionField == null) || (descCharsEntered < minDescChars))
{
errorStatus = 1;
showErrorMsg(1, descCharsEntered, itemEntered);
}
else if (priceCharsEntered < minPriceChars)
{
errorStatus = 1;
showErrorMsg(2, priceCharsEntered, Double.toString(priceEntered));
}
else
{
errorStatus = 0;
Item cidx = new Item(itemEntered, priceEntered , taxValueSelected);
myStuff.add(cidx);
numrecords++;
Vector vec = new Vector(myStuff);
tempVec = new JList(vec);
shoppingCart = new JScrollPane(tempVec, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
.......
descriptionField.setText(""
priceField.setText(""
}
} // End of Purchase button
//If the SUBTOTAL button is pressed
else if (buttonClicked == subTotalButton)
{
.......
}
//If the PRINT button is pressed
else if (buttonClicked == totalButton)
{
........
}
//If the LOGOUT button is pressed
else if (buttonClicked == logOutButton)
{
System.exit(0);
}
}
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "Sorry! Error in price format......" );
priceField.setText(""
errorStatus = 1;
}
} // end of actionPerformed() method
} //end of ItsMe Class
/* =============================
= Item class =
============================= */
public class Item
{
private String itemName;
private double itemPrice;
private boolean itemTax = false;
/* ---- This is my default constructor ------- */
public Item(String theName, double thePrice, boolean theTax)
{
setItemName(theName);
setItemPrice(thePrice);
setItemTax(theTax);
}
/* ---- setItemName() method ------- */
public void setItemName(String theName)
{
itemName = theName;
}
/* ---- setItemPrice() method ------- */
public void setItemPrice(double thePrice)
{
itemPrice = thePrice;
}
/* ---- setItemTax() method ------- */
public void setItemTax(boolean theTax)
{
itemTax = theTax;
}
/* ---- getName() returned method ------- */
public String getName()
{
return itemName;
}
/* ---- getPrice() returned method ------- */
public double getPrice()
{
return itemPrice;
}
/* ---- getTax returned method ------- */
public boolean getTax()
{
return itemTax;
}
public String toString()
{
return " " + itemName + " " + itemPrice + " " + itemTax;
}
} // end of Item Class scope
Thank you!,
Christopher