Looks like I only made it a day and a half without a new question. I am not sure if that is good or bad.
I have everything just about the way I want it. I am now putting new buttons in to my GUI to manipulate everything put in my JList.
I added all the buttons to the GUI, put the architecture in so that I can just go in, one by one and edit them for functionality.
ADD works fine.
PREV is giving me an issue, and I am not even sure what it is.
I just want it to pull the last cd up so that I can look at it. Populate the given fields for possible modification should I decide to change or update anything.
What I have coded compiles, but after entering several cds, if I hit PREV I get:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:468)
at java.lang.Integer.parseInt(Integer.java:497)
at Inventory2.btnPrevActionPerformed(Inventory2.java:280)
at Inventory2.access$100(Inventory2.java:10)
at Inventory2$2.actionPerformed(Inventory2.java:155)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:5517)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3135)
at java.awt.Component.processEvent(Component.java:5282)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3984)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3819)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1791)
at java.awt.Component.dispatchEvent(Component.java:3819)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
This seems to indicate an issue with Integer vs Text or something like that with those lines. I do not understand that. Why would those lines be compatible in my ADD but not with the PREV.
I do not even know what problem I am trying to solve now.
Here is the complete class.
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 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 NumberFormat nstockFormat; // format field and parse numbers
private NumberFormat priceFormat; // format field and parse numbers
private JButton btnAdd; // first button
private JButton btnPrev; // previous button
private JButton btnNext; // next button
private JButton btnDel; // last button
private JButton btnFirst; // first button
private JButton btnLast; // last button
private JButton btnModify; // modify button
private JButton btnSave; // save button
private JButton btnSearch; // search 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 double total = 0; // variable for total inventory
private JList Inventorylist; // JList to take place of old array
private DefaultListModel listModel;
private JScrollPane jScrollPanel;
private float Invtotal = .00f;
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:");
// 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(5);
priceField = new JFormattedTextField(priceFormat);
priceField.setEditable(true);
priceField.setColumns(5);
itemField = new JTextField(4);
itemField.setEditable(true);
// JList
jScrollPanel = new JScrollPane();
Inventorylist = new JList();
currCD = 0;
// buttons
btnAdd = new JButton();
btnNext = new JButton();
btnPrev = new JButton();
btnDel = new JButton();
btnLast = new JButton();
btnFirst = new JButton();
btnModify = new JButton();
btnSave = new JButton();
btnSearch = 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(70,20));
artistField.setPreferredSize(new Dimension(70,20));
getContentPane().add(artistField);
// cd name
cdNameLabel.setText("CD Name");
getContentPane().add(cdNameLabel);
cdNameField.setMinimumSize(new Dimension(70,20));
cdNameField.setPreferredSize(new Dimension(70,20));
getContentPane().add(cdNameField);
// copies in stock
nstockLabel.setText("Copies In Stock");
getContentPane().add(nstockLabel);
nstockField.setMinimumSize(new Dimension(5,20));
nstockField.setPreferredSize(new Dimension(5,20));
getContentPane().add(nstockField);
//price of cd
priceLabel.setText("Price");
getContentPane().add(priceLabel);
priceField.setMinimumSize(new Dimension(20,20));
priceField.setPreferredSize(new Dimension(20,20));
getContentPane().add(priceField);
//item number of cd
itemLabel.setText("Item Number");
getContentPane().add(itemLabel);
itemField.setMinimumSize(new Dimension(5,20));
itemField.setPreferredSize(new Dimension(5,20));
getContentPane().add(itemField);
// add listeners
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)
{
btnPrevActionPerformed(evt);
}
});
getContentPane().add(btnPrev);
// NEXT
btnNext.setText("Next");
btnNext.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
btnAddActionPerformed(evt);
}
});
getContentPane().add(btnNext);
// SEARCH
btnSearch.setText("Search");
btnSearch.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
btnAddActionPerformed(evt);
}
});
getContentPane().add(btnSearch);
// FIRST
btnFirst.setText("First");
btnFirst.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
btnAddActionPerformed(evt);
}
});
getContentPane().add(btnFirst);
// LAST
btnLast.setText("Last");
btnLast.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
btnAddActionPerformed(evt);
}
});
getContentPane().add(btnLast);
// MODIFY
btnModify.setText("Modify");
btnModify.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
btnAddActionPerformed(evt);
}
});
getContentPane().add(btnModify);
// SAVE
btnSave.setText("Save");
btnSave.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
btnAddActionPerformed(evt);
}
});
getContentPane().add(btnSave);
// 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 newCD = new CdwArtist();
newCD.setArtist(artistField.getText());
newCD.setName(cdNameField.getText());
newCD.setItemno(Integer.parseInt(itemField.getText()));
newCD.setNstock(Integer.parseInt(nstockField.getText()));
newCD.setPrice(Float.parseFloat(priceField.getText()));
// Add cd to list
listModel.addElement(newCD);
currCD = listModel.size()-1; // sets currCD to added index
// Clear the text fields after add
artistField.setText(null);
cdNameField.setText(null);
itemField.setText(null);
nstockField.setText(null);
priceField.setText(null);
}// end ADD
private void btnPrevActionPerformed(ActionEvent evt)
{
// Grab Previous cd
CdwArtist newCD = (CdwArtist) listModel.get( currCD-- );
newCD.setArtist(artistField.getText());
newCD.setName(cdNameField.getText());
newCD.setItemno(Integer.parseInt(itemField.getText()));
newCD.setNstock(Integer.parseInt(nstockField.getText()));
newCD.setPrice(Float.parseFloat(priceField.getText()));
}// end PREV
// run it
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
new Inventory2().setVisible(true);
}
});
}
} // close classCan you please provide remining classes for this application. So far I do no see any problems here so copaling whole thing would be better
Hey, was just wondering... when you decrement currCD , how do you ensure that it isn't a negative number? Perhaps it is handled internally... but if it isn’t then wouldn’t that be an issue? Yeah was just wondering...
Peter, here is my first class
import java.util.*;
import java.text.*;
import java.lang.Comparable;
public class Compactdisk implements Comparable
{// begin class
//InventoryCD class has 5 fields
public String name; // Name of cd
public float price; // price of cd
public int itemno; // item number of cd
public int nstock; // how many units in stock
private int i; // cd counter for array
public float value; // value for single cd inventory
//Compact disk class constructor
public Compactdisk()
// 4 fields need to be set up
{
name = "";
price = .00f;
itemno = 0;
nstock = 0;
i = 0;
value = .00f;
}
// set values
public void setName(String diskName)
{
name = diskName;
}
public void setPrice(float cdPrice)
{
price = cdPrice;
}
public void setItemno(int cdItemno)
{
itemno = cdItemno;
}
public void setNstock(int cdStock)
{
nstock = cdStock;
}
public void setValue(float cdValue)
{
value = cdValue;
}
public void seti(int Count)
{
i = Count;
}
// return values
public String getName()
{
return (name);
}
public float getPrice()
{
return (price);
}
public int getItemno()
{
return (itemno);
}
public int getNstock()
{
return (nstock);
}
public int compareTo(Object in)
{
return ((Comparable)name.toLowerCase()).compareTo((Comparable)((Compactdisk)in).getName().toLowerCase());
}
// returns indivudual inventory value for a disk
public float getValue()
{
return (price * nstock);
}
}// end class And the class that extends 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 CdwArtist extends Compactdisk
{
private String artist; // artist performing on cd
private float restock; // restocking percentage
private float total; // inventory stock after restock
private float invtotal; // total inventory value of all cds
DecimalFormat formatter = new DecimalFormat("$0.00");
// Artist constructor
public CdwArtist()
{
artist = "";
restock = .05f;
total = .00f;
invtotal = .00f;
}
public CdwArtist(String in)
{
artist=in;
}
// get values
public void setArtist(String in)
{
artist=in;
}
public void setRestock(float cdRestock)
{
restock = cdRestock;
}
public void setTotal(float cdtotal)
{
total = cdtotal;
}
public void setInvtotal(float cdinvtotal)
{
invtotal = cdinvtotal;
}
// return value
public String getArtist()
{
return (artist);
}
// returns indivudual inventory value for a disk
public float getTotal()
{
return(price * nstock)+((price * nstock)* restock);
}
//new toString
public String toString()
{
return "<html>Artist: " + artist +
" CD Name: " + name +
" Item Number: " + itemno +
" Price: " + formatter.format(getPrice()) +
" In Stock: " + nstock +
" Stock Value: " + formatter.format(getValue()) +
" Restocking Fee: $" + restock +
" Total Inventory Value For Item: " + formatter.format(getTotal())+ "<hr></html>";
}
} //End Class
That is a good question Pooven. One I had not even considered until you asked it. I want to go back one record at a time, when I hit the very first record I would like to just loop around to the last record and start the process over again. Any suggestions?
One I had not even considered until you asked it. I want to go back one record at a time, when I hit the very first record I would like to just loop around to the last record and start the process over again. Any suggestions?
Well, think about your index in relation to the list. If the decrement puts it past 0 you know what you want the next value to be and listModel has a size() function to help you out.
Your parsing problem is clear from the exception message:
<strong>Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""</strong>
It's trying to parse an int from an empty string. If you look at the code in your previous handler, you have it backward. You want to set the text to the value from the object - not the other way around.
Well, think about your index in relation to the list. If the decrement puts it past 0 you know what you want the next value to be and listModel has a size() function to help you out.
As I do not know how many elements can be in my JList at any given moment, would something like
For CurrCD < 0 listModel.Last();
bet a better option?
Either way, I am not sure of the syntax for the actual command.
What is the "return" or "get" command, I guess.[CODE][ // Grab Previous cd
CdwArtist newCD = (CdwArtist) listModel.get( currCD-- );
For currCD < 0 listModel.size(99);
newCD.setArtist(artistField.getText());
newCD.setName(cdNameField.getText());/CODE]
just does not look right to me.
Of course it is all mute if I cannot find the problem with the PREV button anyways, cuz it does not work now to begin with! ;)
if (--currCD<0) currCD = listModel.size()-1;
and then listModel.get(currCD); would work just fine.
Your parsing problem is clear from the exception message:
It's trying to parse an int from an empty string. If you look at the code in your previous handler, you have it backward. You want to set the text to the value from the object - not the other way around.<strong>Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""</strong>
If I understand you, in the ADD I am setting the object to the value of the text field (so that I can do calculations and what not), but now that I am taking that information back from the list, I need to set the text back to the value of the object?
newCD.setItemno(Integer.parseInt(itemField.getText())); is obviously the way to do number 1.
Will you show me an example of how to put it back?
newCD.setText(Integer.parseInt(getItemno())); Is what my limited experience tells me, but of course that is wrong.
You simply call setText() on the textfield with the value from the cd object.
itemField.setText( newCD.getItemno());You simply call setText() on the textfield with the value from the cd object.
itemField.setText( newCD.getItemno());
makes perfect sense.
when I do it though
artistField.setText(newCD.getArtist());
cdNameField.setText(newCD.getName());
itemField.setText(newCD.getItemno());
nstockField.setText(newCD.getNstock());
priceField.setText(newCD.getPrice())
the old familiar
Inventory2.java:282: setText(java.lang.String) in javax.swing.text.JTextComponent cannot be applied to (int)
Shows up for all three. Possibly something to do with it being a FormattedTextField????
My bad, you'll need to use String.valueOf( newCD.getItemno() ) in the setText method for the numeric entries or a DecimalFormat formatter.
My bad, you'll need to use String.valueOf( newCD.getItemno() ) in the setText method for the numeric entries or a DecimalFormat formatter.
Or one of each as the case calls for here. That is great. Thanks for the help.
Can you explain the way the list model size works for me?
if (--currCD<0) currCD = listModel.size()-1;
CdwArtist newCD = (CdwArtist) listModel.get( currCD-- );
I understand if the currCD counter is less than zero, then currCD impliments listModel.size()-1;
What does the -1 refer to? What does that mean?
Right now, it works, "kind of". If I put in 3 cds, and then hit PREV it goes from the 3rd to the 2nd, from the 2nd to the first, and then from the 1st back to the 3rd .... then it just bounces back and forth from the 3rd to the 1st ... it never goes to the second again.
I need to understand what it is doing, because I have a NEXT button to impliment which does exactly the opposite of the PREV, and then a FIRST and LAST button which go to the first and last records respectively and know I will need to use this command again.
You don't want to get currCD-- from listModel, just currCD. You have already decremented it in the statement that handles the bounds rollover.
size()-1 is the index of the last item in the model, i.e. 10 items indexed from 0-9.
This whole function is wrong
private void btnPrevActionPerformed(ActionEvent evt)
{
// Grab Previous cd
CdwArtist newCD = (CdwArtist) listModel.get( currCD-- );
newCD.setArtist(artistField.getText());
newCD.setName(cdNameField.getText());
newCD.setItemno(Integer.parseInt(itemField.getText()));
newCD.setNstock(Integer.parseInt(nstockField.getText()));
newCD.setPrice(Float.parseFloat(priceField.getText()));
}// end PREV
Why do you try to pull info from text field if your text field is EMPTY ! ! ! You cleared out after reading data with add method
artistField.setText(null);
cdNameField.setText(null);
itemField.setText(null);
nstockField.setText(null);
priceField.setText(null); Read data from JList
oooooooooo. I see.
Then, when I start going the other way, ie the NEXT button, ... hmmm,
I would think it would have to stay -1, as that still means end of the list, but how do I incement up? To what end? I would not know, would I?
if (++currCD>???) currCD = listModel.size()-1;
CdwArtist newCD = (CdwArtist) listModel.get( currCD );
Ok, now you have got me perplexed. This affects also my First and Last records buttons as well.
I will not have to do an IF statement for those two, as I am just going to the first or last record, and the first record I know, so that statement should be easy
currCD = listModel.get()0; I think,
but for LAST, I do not know what last will be ... any help with this?
>Then, when I start going the other way, ie the NEXT button, ... hmmm,
What you have to do is just check if the NEXT is smaller then size of your list. When the moving position become equal to list size I would just simple disable button and that apply for oposite direction move, if moving position equals to zero disable PREVIOUS button.
>This affects also my First and Last records buttons as well.
>but for LAST, I do not know what last will be ... any help with this?
Simple, calling FIRST you call first position in the list, which is actualy zero position. For LAST there is method which actualy return last check API properly
with first I think you been trying to say
currCD = listModel.get(0); and last will be like thhis
currCD = listModel.lastElement();
don't forget deal with exception
>with first I think you been trying to say
currCD = listModel.get(0);and last will be like thhis
don't forget deal with exceptioncurrCD = listModel.lastElement();
His usage there misled you a bit. Actually, the currCD variable is just an int index for the current cd, so the get(0) and lastElement() won't apply since they return the objects in those positions.
This is a simple matter of keeping track of an index. listModel.size()-1 is the index of the last item. From there, implementing your next() and last() code should be trivial to figure out.
What exception? Exception to what? I do not know what that means.
I am still trying to wrap my brain around my NEXT button will go to that lastElement, and then loop back to the first again.
if (++currCD<0) currCD = listModel.size()+1;
does not work. I am not sure how to tell currCD which element number is lastElement. Greater than what is my question?
I am also not sure listModel.size()+1; is correct either.
if (++currCD > listModel.lastElement()) currCD = listModel.size()+1;
Keeps coming from my fingers, but I know currCD and lastElement cannot be compared like that.
Keeps coming from my fingers, but I know currCD and lastElement cannot be compared like that.
Which is why I keep pointing out listModel.size(). That is the current number of items in your list. That number-1 is the index of the last item. If your pointer equals the number of items, you have incremented too far and need to wrap back to the index of the first item.