•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 423,510 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,700 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 3651 | Replies: 59 | Solved
![]() |
WOW, YOU could probably finish it in an hour!
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.
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 never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
•
•
•
•
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.
•
•
•
•
Display and toString() would come last.
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:
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.
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
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.
I'm no ones son, unforgiven.
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);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>
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>
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
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?
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() );
}
}); I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
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 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 never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
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.
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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Previous Thread: Keyword search in a web page
- Next Thread: Please help!!



Linear Mode