RSS Forums RSS
Please support our Java advertiser: Lunarpages Java Web Hosting

First JLIST, used with GUI Inventory

Join Date: Jul 2007
Posts: 186
Reputation: no1zson is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Junior Poster

Re: First JLIST, used with GUI Inventory

  #23  
Jul 26th, 2007
You are right about the toString(), and I can leave it like that for now, I will lose points because I only want to display one at a time, small problem.
For the currCd, I though for sure I understood what you were saying. I re-coded it, as so:
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.text.*;
import java.lang.*;

public class Inventory2 extends JFrame
{
	private JLabel cdNameLabel; // name label 
	private JLabel artistLabel; // item number label 
	private JLabel nstockLabel; // units in stock label 
	private JLabel priceLabel; // price each label 
	private JLabel itemLabel; // item number label 
	private JLabel valueLabel; // value of that item label 
	private JLabel rstkLabel; // cost to restock label 
	private JLabel totalLabel; // total value of inventory label 
	private JTextField cdNameField; // name display 
	private JTextField artistField; // artist display 
	private JFormattedTextField nstockField; // units in stock display 
	private JFormattedTextField priceField; // price each display 
	private JTextField itemField; // item number display 
	private JFormattedTextField valueField; // value of that item display 
	private JFormattedTextField rstkField; // cost to restock display
	private JFormattedTextField totalField; // value of all inventory combine 
	private NumberFormat nstockFormat; // format field and parse numbers 
	private NumberFormat priceFormat; // format field and parse numbers 
	private NumberFormat valueFormat; // format field and parse numbers 
	private NumberFormat rstkFormat; // format field and parse numbers 
	private NumberFormat totalFormat;
	private JButton btnAdd; // first button 
	private JButton btnPrev; // previous button 
	private JButton btnNext; // next button 
	private JButton btnDel; // last button 
	private JPanel buttonJPanel; // JPanle to hold buttons 
	private JPanel fieldJPanel; // JPanel to hold labels and displays 
	private JPanel fontJPanel; // JPanel to display logo 
	private int currCD; 
//	private int currentCD;  
	private double total = 0; // variable for total inventory 
	private JList Inventorylist; // JList to take place of old array
	private DefaultListModel listModel;
	private JScrollPane jScrollPanel;  

	public Inventory2() // create class and method to perform GUI build 
	{ 
		initComponents(); 
	} 

	private void initComponents() 
	{ 
		// create label names 
		cdNameLabel = new JLabel("CD Name:"); 
		artistLabel = new JLabel("Artist:");
		nstockLabel = new JLabel("In Stock:"); 
		priceLabel = new JLabel("Each Item Cost:$"); 
		itemLabel = new JLabel("Item Number:"); 
		valueLabel = new JLabel("Value of Item Inventory:$"); 
		rstkLabel = new JLabel("Cost to Re-Stock Item:$"); 
		totalLabel = new JLabel("Total Value of Inventory:$"); 
		
		// initial fields
		cdNameField = new JTextField(25);
		cdNameField.setEditable(true);
		artistField = new JTextField(15);
		artistField.setEditable(true);
		nstockField = new JFormattedTextField(nstockFormat);
		nstockField.setEditable(true);
		nstockField.setColumns(10);
		priceField = new JFormattedTextField(priceFormat);
		priceField.setEditable(true);
		priceField.setColumns(10);
		itemField = new JTextField(5);
		itemField.setEditable(true);
		valueField = new JFormattedTextField(valueFormat);
		valueField.setEditable(true);
		valueField.setColumns(10);
		rstkField = new JFormattedTextField(rstkFormat);
		rstkField.setEditable(true);
		rstkField.setColumns(5);
		totalField = new JFormattedTextField(totalFormat);
		totalField.setColumns (10);
		totalField.setEditable(true);
				
		// JList
		jScrollPanel = new JScrollPane();
		Inventorylist = new JList(); 
		
		//Instantiate object for JList
	//	CdwArtist currentCD = (CdwArtist) listModel.get( currCD );
		currCD = 0;
		
		
	   // add values to textFields
	//	cdNameField.setText(currentCD.getcdName());
	//	artistField.setText(currentCD.getArtist()); 
	//	nstockField.setValue(new Float(currentCD.getValue()));
	//	priceField.setValue(new Float("$"+curentrCD.getPrice()));
	//	itemField.setText(currCD.getItem());
	//	valueField.setValue(new Float("$" + currentCD.getValue()));
	//	rstkField.setValue(new Float(currentCD.getRestock()));
	//	totalField.setValue(new Float("$" + currentCD.getValue()));
		
		
		// buttons 
		btnAdd = new JButton(); 
		btnNext = new JButton(); 
		btnPrev = new JButton(); 
		btnDel = new JButton(); 
		
		getContentPane().setLayout(new FlowLayout());
		
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

		// place textFields and labels
		
		//artist
		artistLabel.setText("Artist"); 
		getContentPane().add(artistLabel); 

		artistField.setMinimumSize(new Dimension(75,19)); 
		artistField.setPreferredSize(new Dimension(75,19));
		getContentPane().add(artistField); 
		
		// cd name
		cdNameLabel.setText("CD Name"); 
		getContentPane().add(cdNameLabel); 

		cdNameField.setMinimumSize(new Dimension(75,19)); 
		cdNameField.setPreferredSize(new Dimension(75,19));
		getContentPane().add(cdNameField);
		
		// copies in stock
		nstockLabel.setText("Copies In Stock"); 
		getContentPane().add(nstockLabel); 

		nstockField.setMinimumSize(new Dimension(75,19)); 
		nstockField.setPreferredSize(new Dimension(75,19));
		getContentPane().add(nstockField); 
		
		//price of cd
		priceLabel.setText("Price"); 
		getContentPane().add(priceLabel); 

		priceField.setMinimumSize(new Dimension(75,19)); 
		priceField.setPreferredSize(new Dimension(75,19));
		getContentPane().add(priceField); 
		
		//item number of cd
		itemLabel.setText("Item Number"); 
		getContentPane().add(itemLabel); 

		itemField.setMinimumSize(new Dimension(75,19)); 
		itemField.setPreferredSize(new Dimension(75,19));
		getContentPane().add(itemField);; 
		
		// value of individual cd in stock
		valueLabel.setText("Value"); 
		getContentPane().add(valueLabel); 

		valueField.setMinimumSize(new Dimension(75,19)); 
		valueField.setPreferredSize(new Dimension(75,19));
		getContentPane().add(valueField); 
		
		// restocking fee
		rstkLabel.setText("Restock Fee"); 
		getContentPane().add(rstkLabel); 

		rstkField.setMinimumSize(new Dimension(75,19)); 
		rstkField.setPreferredSize(new Dimension(75,19));
		getContentPane().add(rstkField);
		
		// total value of inventory
		totalLabel.setText("Total Inventory Value"); 
		getContentPane().add(totalLabel); 

		totalField.setMinimumSize(new Dimension(75,19)); 
		totalField.setPreferredSize(new Dimension(75,19));
		getContentPane().add(totalField);	
		
		
		//ADD listeners
		
		// listener to perform calc upon leaving the price field
		priceField.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent evt)
			{
			valueField.setValue( ((CdwArtist) listModel.get( currCD )).getValue() );

			}
		});
		
		btnAdd.setText("Add");
		btnAdd.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent evt)
			{
				btnAddActionPerformed(evt);
			}
		});
		getContentPane().add(btnAdd);
		 
		// PREVIOUS
		btnPrev.setText("Previous");
		btnPrev.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent evt)
			{
				btnAddActionPerformed(evt);
			}
		});
		getContentPane().add(btnPrev);
		
		// NEXT
		btnNext.setText("Next");		btnNext.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent evt)
			{
				btnAddActionPerformed(evt);
			}
		});
		getContentPane().add(btnNext);
				
		// DELETE
		btnDel.setText("Delete");
		btnDel.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent evt)
			{
				btnAddActionPerformed(evt);
			}
		});
		getContentPane().add(btnDel);
		
		// new Jlist model
		listModel = new DefaultListModel();
		Inventorylist.setModel(listModel);
		
		jScrollPanel.setViewportView(Inventorylist);
		
		getContentPane().add(jScrollPanel);
		
		pack();
	}// close
	
			
		private void btnAddActionPerformed(ActionEvent evt)
		{
			// Create cd to add
			currCD++;
			artistField.getText(currCD.getArtist());
			cdNameField.getText(currCD.getcdName());	
			Integer.parseInt(itemField.getText(currCD.getitemno()));
			Integer.parseInt(nstockField.getText(currCD.getNstock()));
			Float.parseFloat(priceField.getText(currCD.getPrice()));
			Float.parseFloat(valueField.getText(currCD.getValue()));
			Float.parseFloat(totalField.getText(currCD.getTotal()));
			Float.parseFloat(rstkField.getText(currCD.getrestock()));	
			
			// Add cd to list
			
			
			listModel.addElement(currCD);
			
			// Clear the text fields after add
			artistField.setText(null);
			cdNameField.setText(null);	
			itemField.setText(null);
			nstockField.setText(null);
         priceField.setText(null);
			valueField.setText(null);
			rstkField.setText(null);
			totalField.setText(null);
		}// end ADD
		
			
		// run it
		public static void main(String args[])
		{
		java.awt.EventQueue.invokeLater(new Runnable()
			{
			public void run()
				{
				new Inventory2().setVisible(true);
				}
			});
		}
			
} // close class
		
but I get those same errors as yesterday that led us off down the other road. I do not know what these mean.
Inventory2.java:253: int cannot be dereferenced
artistField.getText(currCD.getArtist());
^
Inventory2.java:254: int cannot be dereferenced
cdNameField.getText(currCD.getName());
^
Inventory2.java:255: int cannot be dereferenced
Integer.parseInt(itemField.getText(currCD.getitemno()))


I appreciate your perserverance with me. I will not quit as long as you do not. :o)

I am also coding my backup plan at the same time though in case we just decide this one is above me.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
Reply With Quote  
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 5:45 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC