//how to add items and prices to a listbox //and keep running totals and counts import javax.swing.*; import java.util.*; //used to sort list box items import java.text.*; //so we can format strings public class frmCounts extends javax.swing.JFrame { //**********CLASS-LEVEL VARIABLES*********** //---create the new model to hold our listbox items DefaultListModel model = new DefaultListModel(); //--- class varaible to hold our total amt due double runningTotal = 0.00; int numItems = 0; //---create my decimal format to use when we display totals NumberFormat currency = new DecimalFormat("$#.00"); public frmCounts() { initComponents(); // center the form this.setLocationRelativeTo(null); lbItems.setModel(model); //tell listbox which model to use so it will display data in Jlist txtItem.requestFocusInWindow(); //set cursor to first box initializeForm(); //add our initial items to the listbox } private void initializeForm() { //Note the ScrollBar Policies CAN be set in the Properties Winddow. I just wanted to make sure to remember to show them to you //---do not allow scrolling horizontal so they can't see prices. But allow vertical scrolling jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); //---Add some initial items to our model. Model is the object that holds our data. // Notice how I added prices. Make them spaced to the right so they won't be visible on the form model.addElement("Hats 14.95"); model.addElement("Golf tees 2.98"); model.addElement("Balls 47.69"); model.addElement("Shirts 28.75"); model.addElement("Wind Breaker 19.59"); model.addElement("Putters 89.79"); sortListBox(model); } private void sortListBox (DefaultListModel dlm) { //since you cannot sort the JLIST control, you read the data from the model, convert to an array, // and sort the temp array. Then you have to put the sorted array elements back into the model. Object[] myArray = dlm.toArray(); //grab all items, put into an object array Arrays.sort(myArray); //sort the new array dlm.clear(); //start clean for (Object x : myArray) //enhanced for loop (for-each loop) dlm.addElement(x); //add the elements back into the model in sorted order lbItems.setSelectedIndex(0); } private void btnAddActionPerformed(java.awt.event.ActionEvent evt) { //Add item to cart // get the entire string that was selected String theItem = lbItems.getSelectedValue().toString(); //---now take the first 15 chars and put in the textbox txtItem.setText(theItem.substring(0, 15).trim()); //---now grab the price & display it String thePrice = theItem.substring(20).trim(); txtCost.setText(thePrice); //---now add item to our total runningTotal += Double.parseDouble(thePrice); //---increment numItems numItems ++; } private void btnCheckoutActionPerformed(java.awt.event.ActionEvent evt) { // Display totals lblNumItems.setText("You purchased " + numItems + " items today."); lblTotalSpent.setText("You spent " + currency.format(runningTotal) + " today. Thanks."); } private void btnClearActionPerformed(java.awt.event.ActionEvent evt) { // reset the form txtItem.setText(""); txtCost.setText(""); lblTotalSpent.setText(""); lblNumItems.setText(""); lbItems.setSelectedIndex(0); // reset to first item //---reset running total and count runningTotal = 0.0; numItems = 0; //---set cursor txtItem.requestFocusInWindow(); }