Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 4085 | Replies: 59 | Solved
![]() |
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?
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.
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();
//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()));
// 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 classcurrCD 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.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
I tried to add this listener in an attempt to have my Value field calculated upon leaving the Price field.
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?
// 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?
Last edited by no1zson : Jul 25th, 2007 at 2:24 pm.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
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().
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.
valueField.setText( currentCd.getValue() );
//Instantiate object for JList for (currCD = 0; currCD <99; currCD++) // add values to textFields cdNameField.setText(currCD.getcdName());
listModel.get(currCD);. You will need to cast it to CdwArtist to use it's methods though: CdwArtist currentCD = (CdwArtist) listModel.get( currCD );
•
•
•
•
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).
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.
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 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)
{
valueField.setValue("$"+new Float(currentCD.getValue()));
}
});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 never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
The only way I know to do that is like this:
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.
priceField.addActionListener(new ActionListener()
{
final void actionPerformed(ActionEvent evt)
{
valueField.setValue("$"+new Float(currentCD.getValue()));
}
});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())); I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






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.
Linear Mode