I am sure anyone here for more than an hour is familiar with the project I have been working on.
I am currently researching listeners to try and get my fields to execute the proper calculations when moving from one to the next. Right now, even though I have those functions defined in classes, they are not being executed. Hopefully I will figure that out soon.
I also want to be able to go forwards and backwards through this list as it is created.
Lack of forsite left me without a counter in this application until now.
I am already in above my head with this being my first GUI and my first JLIST, but can someone look at this counter mechanism I am trying to put in and give me an idea of what is wrong with it?

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; // current product display to use for button action 
	private int i; // iterator 
	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(); 
		
		[B]//Instantiate object for JList
		for (currCD = 0; currCD <99; currCD++)
		
		// add values to textFields
		cdNameField.setText(currCD.getcdName());
		artistField.setText(currCD.getArtist()); 
		nstockField.setValue(new Float(currCD.getValue()));
		priceField.setValue(new Float("$"+currCD.getPrice()));
		itemField.setText(currCD.getItem());
		valueField.setValue(new Float("$" + currCD.getValue()));
		rstkField.setValue(new Float(currCD.getRestock()));
		totalField.setValue(new Float("$" + currCD.getValue()));[/B]
		
		
		// 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 buttons
		//ADD
		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
			CdwArtist cd = new CdwArtist(artistField.getText());
			cd.setName(cdNameField.getText());	
			cd.setItemno(Integer.parseInt(itemField.getText()));
			cd.setNstock(Integer.parseInt(nstockField.getText()));
			cd.setPrice(Float.parseFloat(priceField.getText()));
			cd.setValue(Float.parseFloat(valueField.getText()));
			cd.setTotal(Float.parseFloat(totalField.getText()));
			cd.setRestock(Float.parseFloat(rstkField.getText()));	
			
			// Add cd to list
			listModel.addElement(cd);
			
			// 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

currCD is going to be the number of the cd I am working on. Right now, in the bolded area, I am getting "Int cannot be rerefernced" pointing at the . in front of each "get". I do not now what this means and do not know what to do in order to eliminate the error and move forward.

Recommended Answers

All 59 Replies

I tried to add this listener in an attempt to have my Value field calculated upon leaving the Price field.

// listener to perform calc upon leaving the price field
		priceField.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent evt)
			{
				valueField.setValue("$"+new Float(CdwArtist.getValue()));
			}
		})

It tells me getValue cannot be referenced from a static context. I am not sure what that means.
I then tried just to do the calculation manually and replaced it with ("$" + (nstock * price));, but it tells me those symbols are not found.

Obviously I am not coding this right, but it is the logic that I am having the most trouble with.
I do not understand why I have to do this step (even when I get it right) if I can get the field defintion problem worked out.
When I define a field as "getValue", and value returns (nstock * price), as defined in my CdwArist class, then why do I have to create a listener in the first place?
Am I making any sence?

You are calling the method in a static context because you have used the class name rather than an object reference to call the getValue() function. You will need to use the current CD object for this call. Your method is almost correct though. You want to set the text of the value field to the result of cd.getValue().

valueField.setText( currentCd.getValue() );

The getValue() method will do the calculation for you, you just need the listeners to call that calculation at the right time and put the value into the field you want it displayed in.

commented: You beat me to it :-\ +1
//Instantiate object for JList
		for (currCD = 0; currCD <99; currCD++)
		
		// add values to textFields
		cdNameField.setText(currCD.getcdName());

You cannot call a method on an int variable, which is what you have tried to do here. You really don't even need this loop to try to set up all cds in the list in the first place. You add one when the user clicks 'add'. If you keep currCD updated to point to the current index of the CD in the list, you can access the object with the expression listModel.get(currCD); . You will need to cast it to CdwArtist to use it's methods though:

CdwArtist currentCD = (CdwArtist) listModel.get( currCD );

since the get() method returns the generic type Object.

It tells me getValue cannot be referenced from a static context. I am not sure what that means.

To answer your question about the static context error, you're trying to access CdwArtist's method .getValue() without ever making a CdwArtist object or declaring the getValue method as static (not recommended).

Try creating a new CdwArtist object and calling the getValue() method from it after it has been populated with data.

I then tried just to do the calculation manually and replaced it with ("$" + (nstock * price));, but it tells me those symbols are not found

Again, that's because nstock and price are inside of CdwArtist (Compactdisk) but you do not have a CdwArtist you are getting the value from. Create a new CdwArtist with it's values set and call the variables from the said object.

Remember, the computer only does what you tell it to. If you tell it to use something like a method or variable from another class without telling it it's in the other class, the computer doesn't know to go looking for it, it only knows that it doesn't have what it needs (hence the error).

:( While I'm making my post and trying to work with some errors on my own project, Ezzaral beats me to the punch and steals my thunder.

GG. :p

commented: hehe He can use the additional viewpoint. A different explanation may help it click! +2

:( While I'm making my post and trying to work with some errors on my own project, Ezzaral beats me to the punch and steals my thunder.

GG. :p

No, no, your thunder is still valid - the additional explanation may click in an area that mine was vague :)

All thunder is welcome here, as there is certainly none coming from me!
I do think I understand a little better. I added a cast to CdwArtist, andI made some changes that I think help, but got a new error that I have never seen before.

priceField.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent evt)
			{
			 [B]valueField.setValue("$"+new Float(currentCD.getValue()));[/B]
			}
		});

Inventory2.java:189: local variable currentCD is accessed from within inner class; needs to be declared final.
I do not know what to do with this. I know what it means to delcare something final, but not how or why it would apply to this statement.
I think I am getting dumber as the day goes on.

I'm not quite sure, but it might have something to do with the compiler not wanting action event modifying an instance of an object outside the scope of the external class. That's just an idea though, I haven't had a problem like that in a long time.

What happens when you declare that object final?

The only way I know to do that is like this:

priceField.addActionListener(new ActionListener()
		{
			final void actionPerformed(ActionEvent evt)
			{
			valueField.setValue("$"+new Float(currentCD.getValue()));
			}
		});

I am pretty sure this declares the action event as final, which is not right, but as I know no other way I do it, and then get.

Inventory2.java:187: actionPerformed(java.awt.event.ActionEvent) in  cannot implement actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener; attempting to assign weaker access privileges; was public
			final void actionPerformed(ActionEvent evt)
			           ^
Inventory2.java:189: local variable currentCD is accessed from within inner class; needs to be declared final
			valueField.setValue("$"+new Float(currentCD.getValue()));

It is only the local variables inside the method that declares the anonymous inner class that must be final. Here is an explanation that might help:
http://mindprod.com/jgloss/nestedclasses.html#ANONYMOUSVISIBILITY
I'm not sure where you have defined currentCD, but that may be the one that needs to be final. The local variables need to be final because you are defining a separate class to act upon them and it needs to be sure the local reference will remain intact.

I hope this does not sound too dumb, but I actually understood that. I know what final is, and now I know why it is that way, but cannot for the life of me see why that affects this line, even if it is nested. The only other thing I can think of would be my cast, and I am not sure where else I would put it. Here is the code as it sits ...
I cannot believe you have not abandoned me and this project yet. You are a real glutten for punishment.

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; // current product display to use for button action 
	private int i; // iterator 
	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 );

		
	   // 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()
		{
			final void actionPerformed(ActionEvent evt)
			{
			valueField.setValue("$"+new Float(currentCD.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
			CdwArtist cd = new CdwArtist(artistField.getText());
			cd.setName(cdNameField.getText());	
			cd.setItemno(Integer.parseInt(itemField.getText()));
			cd.setNstock(Integer.parseInt(nstockField.getText()));
			cd.setPrice(Float.parseFloat(priceField.getText()));
			cd.setValue(Float.parseFloat(valueField.getText()));
			cd.setTotal(Float.parseFloat(totalField.getText()));
			cd.setRestock(Float.parseFloat(rstkField.getText()));	
			
			// Add cd to list
			listModel.addElement(cd);
			
			// 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

This is the piece that it is complaining about

//Instantiate object for JList
        CdwArtist currentCD = (CdwArtist) listModel.get( currCD );

because currentCD is local to the method and not final. However, even with that changed to final, it still would not behave as you would like.

You want to set the valueField text with the value from whatever CD you are currently on. You have a pointer for this, currCD, so you actually want

valueField.setValue( ((CdwArtist) listModel.get( currCD )).getValue() );

This gets the cd from the model and does the cast in one statement. You alternately move the currentCD declaration into the listener itself if that would make more sense to you

CdwArtist currentCD = (CdwArtist) listModel.get( currCD ); 
valueField.setValue( currentCD.getValue );

Both of these still depend on currCD pointing to the index of the cd in the list, so you must update currCD when you add a new cd, remove one, or move in the list.

OK, that makes sense to me. I like the first one better, one line keeps it simple.
Does this not leave me needing to initialize currentCd and currCD somewhere?

I would think currCD needs to be 0, but what about currentCD.

I also understand about currCD needing to update as I add, and delete, but I do not want to start that problem until I understand this first part, unless one not being finished will cause the other to error. Trying to solve more than one problem at a time was confusing me.

currentCD was just used as an example of a variable that points to the current CdwArtist object. currCD is an index pointer that can be used to retrieve a CdwArtist object from the list model by index. You can keep one or the other updated to point to your current record. It doens't matter which really, one is by index and the other by reference. Both will allow you to work with a specific entry in the list.

ok, I think I get that. curCD is my counter, currentCD is the object I am counting, right?

I am now able to compile again, but now I am getting the NullPointerException again, which from last time I remember tells me I probably do not have something initialized correctly.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at Inventory2.initComponents(Inventory2.java:91)
	at Inventory2.<init>(Inventory2.java:48)
	at Inventory2$6.run(Inventory2.java:283)
	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

If I read this like you did last time that puts my problem at the currentCD line I just put in, which makes sence as it is the new part I am trying to do, but I just do not see what could be missing.
I have currCD = 0;, I want to add CurrentCD currentCD = new CurrentCD in there, but I do not think that is what it needs, as this line should do that. I am feeling the pressure right now.
This counter is not as hard as I am making it. I still have to work through the field calculation problems once I get this working, and I am feeling more lost that ever.
Project is due tomorrow, and I am kind of wishing I had just stayed with the simple little 5 element array like everyone else, but I tried to get fancy and now I am paying the price.
I have never turned in an incomplete project before, but this is looking like a real possibility here. To top it all off I feel like I am just pestering you to death with questions that I know you think are just silly.
If this is getting to be a thorn in your side please, just let me know, and I will just scrap this whole class and see if I cannot go back and redo it the simple way before Friday. I would prefer this way, because I feel I am learning so much more, but I do not want to do so at the expense of your time and effort.

I don't see where you are populating your list model. I think you're trying to call the value from list model without having values in it.

I commented out that line such that:

//Instantiate object for JList
	currentCD = (CdwArtist) listModel.get( currCD );

looks like:

//Instantiate object for JList
	//	currentCD = (CdwArtist) listModel.get( currCD );

and the gui displays for me and prints out into the box. It's at least closer to what you were wanting I think.

You are right, I knew that line was the problem, but we put it in there yesterday to be able to label the cd products so that I would be able to use the buttons to move around in them. View one, the next, the previous etc etc etc.

That line has to be there for future functionality, I just do not think I did all I needed to do to make it work correctly.

Real problem is that now I am dazed and confused, I do not know what to do to move forward, and it is too late to go back to what I was trying to do before. I want to learn this stuff Ezzaral is showing me because it is so much cooler than the other way, but I think I may be above my head now and just becoming a nuisance. I hate coming so far with this code just to stop now though, I feel like a quitter if I do that.
I am trying to create a class (as a backup plan) just using the simple code from class, just so that I have something to turn in and salvage my grade, but I fear it will not be ready by tomorrow.

Other than you fixing the display for the CDs using toStrings instead of the object addres , implementing the delete button (remove the element from the array and repopulate the list display to reflect thus), and not allowing it to add CDs with blank values, what do you need to do?

To finish the display:
Add an area down at the bottom to display a toString representation of the CDs so that you won't have to add more GUI components and just clear/repopulate after every time a new item is selected using an event handler.

Unless there is some major functionality that you still don't have, you could probably finish what I listed above in less than an hour.

implementing the delete button (remove the element from the array and repopulate the list display to reflect thus), and not allowing it to add CDs with blank values, what do you need to do?

Removing the cd from the list with listModel.remove( currCD ); should update the list display automatically, so that's one less thing to worry about.

WOW, YOU could probably finish it in an hour! :icon_lol:

It is going to take me an hour to sit here and think each one through invididually, and a week to impliment AFTER I figure out what is what.

Delete button I am not worried about at the moment, empty fields either. It is the toString(), currCD, listeners, and actions for my fields that have my head spinning.

I have to remark out the CurrentCD line to get it to run, but I know I need it in there for currCD to work. Once that works
I think I just need a line in my buttons to finish it up, currCD++ and currCD-- should be all that is required. If there is more to that please let me know, because everything else needs that to work before I can start it.
The real thing I want to accomplish today is Field Functionality, to make my calculations populate the fields correctly. I need currCD and a bunch of listeners and actions to do that, and I am not sure about how to do those yet.
Display and toString() would come last. Ezzarral showed me some of that last week, but I am still quite shakey on it and cannot accomplish it without help.

I have to remark out the CurrentCD line to get it to run, but I know I need it in there for currCD to work.

No, if you stick with currCD and access the cd object with listModel.get(currCD), you do not need currentCD necessarily. It's just a convenience reference and is probably just confusing you.

Display and toString() would come last.

I thought you already put a toString() method on your CdwArtist class? There was discussion about an item number and line breaks but I thought you had that part in there. You don't really need to get all that fancy on the toString() representation of the object in the list.

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.

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);

Ok, I see. That was not the "currentCD" reference I was talking about. I meant a persistent one that you kept at the class level.
You do need to create a CdwArtist object there to add it to the list. Perhaps we should just call that "newCD" to avoid confusion. Also, since the index is 0 based, it would be easier to initialize currCD with a value of -1 when you declare it, so your add code would be:

private void btnAddActionPerformed(ActionEvent evt)
        {
            // Create cd to add
            CdwArtist newCD = new CdwArtist();
            newCD.setArtist(artistField.getText());
            newCD.setCdName(cdNameField.getText());    
            newCD.setItemNo(Integer.parseInt(itemField.getText());
            
                        <snip other sets>

            // Add cd to list
            listModel.addElement(newCD);
            currCD = listModel.size()-1;    // set currCD to the index that was added

That is a little clearer.
Do I leave the other one in also?
//CdwArtist currentCD = (CdwArtist) listModel.get( currCD );

Both ways I try it the other messages have gone - whew - and the only thing I am left with is
cannot find symbol on currCD here:
currCd = listModel.size()-1;

I thought maybe the -1 needed to be inside the paranthesis, but the message stayed the same so I put it back where you had it.

How can currCD work everywhere but here? <scratching head>

You have "currCd" - your variable is "currCD". Case matters.

You don't need the //CdwArtist currentCD = (CdwArtist) listModel.get( currCD ); line in there for anything.

I hate it when I am stupid. I looked for just that. :@

I would now expect my listener to work now that currCD is working, but it does not. It stays blank until I enter something manually.
I wrote this yesterday as a template before all this, so that if we got it working I could lay down ones for the other fields.
Am I missing something else?

// 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() );

			}
		});

ActionPerformed will only get triggered by hitting enter in the text field with the listener. Even if triggers though, you will not get the value from a new CD that you are entering, because it is not yet in the list model until you click "add". Instead, what will be set in the field will be the value of the last CD (the one that currCD points to). To alleviate that you would have to either calc the price from text fields that you do have, or have the "Add" button create a new blank CD to work with and put in a Save button that added it to the list model.

Yeah. I guess it would be hard to calculate using figures not really there yet.

I do not understand the concept of the ADD button scenario you just gave. How would creating a blank cd allow me to calculate those fields, when I cannot do it now?
The SAVE button I think I get, once all calculations are complete, then put it in the list.

It is the ADD part throwing me off. Can you elaborate for me?

I could explain more of an architecture that would let your price field get populated after the relevant data had been entered, but honestly it would require some additional changes that I don't really think you would find worth it just now.

Instead, consider this: Why not simply display the value of calculated fields in the toString() info that shows in the list entry and drop the text fields for them altogether? There is little need to provide text entry fields for data that the user doesn't need to enter or edit - such as values calculated from other inputs.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.