943,917 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 5821
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 29th, 2007
0

Getting my data back from my JList

Expand Post »
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.
Java Syntax (Toggle Plain Text)
  1. import java.util.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. import javax.swing.event.*;
  6. import java.text.*;
  7. import java.lang.*;
  8.  
  9. public class Inventory2 extends JFrame
  10. {
  11. private JLabel cdNameLabel; // name label
  12. private JLabel artistLabel; // item number label
  13. private JLabel nstockLabel; // units in stock label
  14. private JLabel priceLabel; // price each label
  15. private JLabel itemLabel; // item number label
  16. private JTextField cdNameField; // name display
  17. private JTextField artistField; // artist display
  18. private JFormattedTextField nstockField; // units in stock display
  19. private JFormattedTextField priceField; // price each display
  20. private JTextField itemField; // item number display
  21. private NumberFormat nstockFormat; // format field and parse numbers
  22. private NumberFormat priceFormat; // format field and parse numbers
  23. private JButton btnAdd; // first button
  24. private JButton btnPrev; // previous button
  25. private JButton btnNext; // next button
  26. private JButton btnDel; // last button
  27. private JButton btnFirst; // first button
  28. private JButton btnLast; // last button
  29. private JButton btnModify; // modify button
  30. private JButton btnSave; // save button
  31. private JButton btnSearch; // search button
  32. private JPanel buttonJPanel; // JPanle to hold buttons
  33. private JPanel fieldJPanel; // JPanel to hold labels and displays
  34. private JPanel fontJPanel; // JPanel to display logo
  35. private int currCD;
  36. private double total = 0; // variable for total inventory
  37. private JList Inventorylist; // JList to take place of old array
  38. private DefaultListModel listModel;
  39. private JScrollPane jScrollPanel;
  40. private float Invtotal = .00f;
  41.  
  42. public Inventory2() // create class and method to perform GUI build
  43. {
  44. initComponents();
  45. }
  46.  
  47. private void initComponents()
  48. {
  49. // create label names
  50. cdNameLabel = new JLabel("CD Name:");
  51. artistLabel = new JLabel("Artist:");
  52. nstockLabel = new JLabel("In Stock:");
  53. priceLabel = new JLabel("Each Item Cost:$");
  54. itemLabel = new JLabel("Item Number:");
  55.  
  56.  
  57. // initial fields
  58. cdNameField = new JTextField(25);
  59. cdNameField.setEditable(true);
  60. artistField = new JTextField(15);
  61. artistField.setEditable(true);
  62. nstockField = new JFormattedTextField(nstockFormat);
  63. nstockField.setEditable(true);
  64. nstockField.setColumns(5);
  65. priceField = new JFormattedTextField(priceFormat);
  66. priceField.setEditable(true);
  67. priceField.setColumns(5);
  68. itemField = new JTextField(4);
  69. itemField.setEditable(true);
  70.  
  71. // JList
  72. jScrollPanel = new JScrollPane();
  73. Inventorylist = new JList();
  74. currCD = 0;
  75.  
  76.  
  77. // buttons
  78. btnAdd = new JButton();
  79. btnNext = new JButton();
  80. btnPrev = new JButton();
  81. btnDel = new JButton();
  82. btnLast = new JButton();
  83. btnFirst = new JButton();
  84. btnModify = new JButton();
  85. btnSave = new JButton();
  86. btnSearch = new JButton();
  87.  
  88. getContentPane().setLayout(new FlowLayout());
  89.  
  90. setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  91.  
  92.  
  93. // place textFields and labels
  94.  
  95. //artist
  96. artistLabel.setText("Artist");
  97. getContentPane().add(artistLabel);
  98.  
  99. artistField.setMinimumSize(new Dimension(70,20));
  100. artistField.setPreferredSize(new Dimension(70,20));
  101. getContentPane().add(artistField);
  102.  
  103. // cd name
  104. cdNameLabel.setText("CD Name");
  105. getContentPane().add(cdNameLabel);
  106.  
  107. cdNameField.setMinimumSize(new Dimension(70,20));
  108. cdNameField.setPreferredSize(new Dimension(70,20));
  109. getContentPane().add(cdNameField);
  110.  
  111. // copies in stock
  112. nstockLabel.setText("Copies In Stock");
  113. getContentPane().add(nstockLabel);
  114.  
  115. nstockField.setMinimumSize(new Dimension(5,20));
  116. nstockField.setPreferredSize(new Dimension(5,20));
  117. getContentPane().add(nstockField);
  118.  
  119. //price of cd
  120. priceLabel.setText("Price");
  121. getContentPane().add(priceLabel);
  122.  
  123. priceField.setMinimumSize(new Dimension(20,20));
  124. priceField.setPreferredSize(new Dimension(20,20));
  125. getContentPane().add(priceField);
  126.  
  127. //item number of cd
  128. itemLabel.setText("Item Number");
  129. getContentPane().add(itemLabel);
  130.  
  131. itemField.setMinimumSize(new Dimension(5,20));
  132. itemField.setPreferredSize(new Dimension(5,20));
  133. getContentPane().add(itemField);
  134.  
  135.  
  136. // add listeners
  137.  
  138. btnAdd.setText("Add");
  139. btnAdd.addActionListener(new ActionListener()
  140. {
  141. public void actionPerformed(ActionEvent evt)
  142. {
  143. btnAddActionPerformed(evt);
  144. }
  145. });
  146. getContentPane().add(btnAdd);
  147.  
  148. // PREVIOUS
  149. btnPrev.setText("Previous");
  150. btnPrev.addActionListener(new ActionListener()
  151. {
  152. public void actionPerformed(ActionEvent evt)
  153. {
  154. btnPrevActionPerformed(evt);
  155. }
  156. });
  157. getContentPane().add(btnPrev);
  158.  
  159. // NEXT
  160. btnNext.setText("Next");
  161. btnNext.addActionListener(new ActionListener()
  162. {
  163. public void actionPerformed(ActionEvent evt)
  164. {
  165. btnAddActionPerformed(evt);
  166. }
  167. });
  168. getContentPane().add(btnNext);
  169.  
  170. // SEARCH
  171. btnSearch.setText("Search");
  172. btnSearch.addActionListener(new ActionListener()
  173. {
  174. public void actionPerformed(ActionEvent evt)
  175. {
  176. btnAddActionPerformed(evt);
  177. }
  178. });
  179. getContentPane().add(btnSearch);
  180.  
  181. // FIRST
  182. btnFirst.setText("First");
  183. btnFirst.addActionListener(new ActionListener()
  184. {
  185. public void actionPerformed(ActionEvent evt)
  186. {
  187. btnAddActionPerformed(evt);
  188. }
  189. });
  190. getContentPane().add(btnFirst);
  191.  
  192. // LAST
  193. btnLast.setText("Last");
  194. btnLast.addActionListener(new ActionListener()
  195. {
  196. public void actionPerformed(ActionEvent evt)
  197. {
  198. btnAddActionPerformed(evt);
  199. }
  200. });
  201. getContentPane().add(btnLast);
  202.  
  203. // MODIFY
  204. btnModify.setText("Modify");
  205. btnModify.addActionListener(new ActionListener()
  206. {
  207. public void actionPerformed(ActionEvent evt)
  208. {
  209. btnAddActionPerformed(evt);
  210. }
  211. });
  212. getContentPane().add(btnModify);
  213.  
  214. // SAVE
  215. btnSave.setText("Save");
  216. btnSave.addActionListener(new ActionListener()
  217. {
  218. public void actionPerformed(ActionEvent evt)
  219. {
  220. btnAddActionPerformed(evt);
  221. }
  222. });
  223. getContentPane().add(btnSave);
  224.  
  225. // DELETE
  226. btnDel.setText("Delete");
  227. btnDel.addActionListener(new ActionListener()
  228. {
  229. public void actionPerformed(ActionEvent evt)
  230. {
  231. btnAddActionPerformed(evt);
  232. }
  233. });
  234. getContentPane().add(btnDel);
  235.  
  236.  
  237. // new Jlist model
  238. listModel = new DefaultListModel();
  239. Inventorylist.setModel(listModel);
  240.  
  241. jScrollPanel.setViewportView(Inventorylist);
  242.  
  243. getContentPane().add(jScrollPanel);
  244.  
  245. pack();
  246. }// close
  247.  
  248.  
  249. private void btnAddActionPerformed(ActionEvent evt)
  250. {
  251. // Create cd to add
  252. CdwArtist newCD = new CdwArtist();
  253. newCD.setArtist(artistField.getText());
  254. newCD.setName(cdNameField.getText());
  255. newCD.setItemno(Integer.parseInt(itemField.getText()));
  256. newCD.setNstock(Integer.parseInt(nstockField.getText()));
  257. newCD.setPrice(Float.parseFloat(priceField.getText()));
  258.  
  259. // Add cd to list
  260. listModel.addElement(newCD);
  261. currCD = listModel.size()-1; // sets currCD to added index
  262.  
  263.  
  264. // Clear the text fields after add
  265. artistField.setText(null);
  266. cdNameField.setText(null);
  267. itemField.setText(null);
  268. nstockField.setText(null);
  269. priceField.setText(null);
  270.  
  271. }// end ADD
  272.  
  273. private void btnPrevActionPerformed(ActionEvent evt)
  274. {
  275. // Grab Previous cd
  276. CdwArtist newCD = (CdwArtist) listModel.get( currCD-- );
  277. newCD.setArtist(artistField.getText());
  278. newCD.setName(cdNameField.getText());
  279. newCD.setItemno(Integer.parseInt(itemField.getText()));
  280. newCD.setNstock(Integer.parseInt(nstockField.getText()));
  281. newCD.setPrice(Float.parseFloat(priceField.getText()));
  282.  
  283. }// end PREV
  284.  
  285. // run it
  286. public static void main(String args[])
  287. {
  288. java.awt.EventQueue.invokeLater(new Runnable()
  289. {
  290. public void run()
  291. {
  292. new Inventory2().setVisible(true);
  293. }
  294. });
  295. }
  296.  
  297. } // close class
Last edited by no1zson; Jul 29th, 2007 at 11:07 pm.
Similar Threads
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Jul 30th, 2007
1

Re: Getting my data back from my JList

Can you please provide remining classes for this application. So far I do no see any problems here so copaling whole thing would be better
Last edited by peter_budo; Jul 30th, 2007 at 4:53 am.
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 873
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004
Jul 30th, 2007
1

Re: Getting my data back from my JList

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...
Reputation Points: 56
Solved Threads: 11
Junior Poster
PoovenM is offline Offline
147 posts
since Aug 2006
Jul 30th, 2007
0

Re: Getting my data back from my JList

Peter, here is my first class
Java Syntax (Toggle Plain Text)
  1. import java.util.*;
  2. import java.text.*;
  3. import java.lang.Comparable;
  4.  
  5.  
  6. public class Compactdisk implements Comparable
  7. {// begin class
  8.  
  9. //InventoryCD class has 5 fields
  10. public String name; // Name of cd
  11. public float price; // price of cd
  12. public int itemno; // item number of cd
  13. public int nstock; // how many units in stock
  14. private int i; // cd counter for array
  15. public float value; // value for single cd inventory
  16.  
  17.  
  18.  
  19. //Compact disk class constructor
  20. public Compactdisk()
  21.  
  22. // 4 fields need to be set up
  23. {
  24. name = "";
  25. price = .00f;
  26. itemno = 0;
  27. nstock = 0;
  28. i = 0;
  29. value = .00f;
  30. }
  31.  
  32. // set values
  33. public void setName(String diskName)
  34. {
  35. name = diskName;
  36. }
  37. public void setPrice(float cdPrice)
  38. {
  39. price = cdPrice;
  40. }
  41. public void setItemno(int cdItemno)
  42. {
  43. itemno = cdItemno;
  44. }
  45. public void setNstock(int cdStock)
  46. {
  47. nstock = cdStock;
  48. }
  49. public void setValue(float cdValue)
  50. {
  51. value = cdValue;
  52. }
  53. public void seti(int Count)
  54. {
  55. i = Count;
  56. }
  57.  
  58.  
  59. // return values
  60. public String getName()
  61. {
  62. return (name);
  63. }
  64. public float getPrice()
  65. {
  66. return (price);
  67. }
  68. public int getItemno()
  69. {
  70. return (itemno);
  71. }
  72. public int getNstock()
  73. {
  74. return (nstock);
  75. }
  76. public int compareTo(Object in)
  77. {
  78. return ((Comparable)name.toLowerCase()).compareTo((Comparable)((Compactdisk)in).getName().toLowerCase());
  79. }
  80.  
  81. // returns indivudual inventory value for a disk
  82. public float getValue()
  83. {
  84. return (price * nstock);
  85. }
  86.  
  87.  
  88.  
  89.  
  90.  
  91. }// end class
And the class that extends it
Java Syntax (Toggle Plain Text)
  1. import java.util.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. import javax.swing.event.*;
  6. import java.text.*;
  7. import java.lang.*;
  8.  
  9. public class CdwArtist extends Compactdisk
  10. {
  11.  
  12. private String artist; // artist performing on cd
  13. private float restock; // restocking percentage
  14. private float total; // inventory stock after restock
  15. private float invtotal; // total inventory value of all cds
  16.  
  17. DecimalFormat formatter = new DecimalFormat("$0.00");
  18.  
  19. // Artist constructor
  20. public CdwArtist()
  21. {
  22. artist = "";
  23. restock = .05f;
  24. total = .00f;
  25. invtotal = .00f;
  26. }
  27.  
  28. public CdwArtist(String in)
  29. {
  30. artist=in;
  31. }
  32.  
  33. // get values
  34. public void setArtist(String in)
  35. {
  36. artist=in;
  37. }
  38. public void setRestock(float cdRestock)
  39. {
  40. restock = cdRestock;
  41. }
  42. public void setTotal(float cdtotal)
  43. {
  44. total = cdtotal;
  45. }
  46. public void setInvtotal(float cdinvtotal)
  47. {
  48. invtotal = cdinvtotal;
  49. }
  50.  
  51. // return value
  52. public String getArtist()
  53. {
  54. return (artist);
  55. }
  56.  
  57. // returns indivudual inventory value for a disk
  58. public float getTotal()
  59. {
  60. return(price * nstock)+((price * nstock)* restock);
  61. }
  62.  
  63.  
  64.  
  65. //new toString
  66. public String toString()
  67. {
  68. return "<html>Artist: " + artist +
  69. " CD Name: " + name +
  70. "<br> Item Number: " + itemno +
  71. " Price: " + formatter.format(getPrice()) +
  72. "<br> In Stock: " + nstock +
  73. " Stock Value: " + formatter.format(getValue()) +
  74. "<br> Restocking Fee: $" + restock +
  75. "<br> Total Inventory Value For Item: " + formatter.format(getTotal())+ "<hr></html>";
  76. }
  77. } //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?
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Jul 30th, 2007
1

Re: Getting my data back from my JList

Quote ...
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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Jul 30th, 2007
0

Re: Getting my data back from my JList

Your parsing problem is clear from the exception message:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""

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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Jul 30th, 2007
0

Re: Getting my data back from my JList

Click to Expand / Collapse  Quote originally posted by Ezzaral ...
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!
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Jul 30th, 2007
0

Re: Getting my data back from my JList

Java Syntax (Toggle Plain Text)
  1. if (--currCD<0) currCD = listModel.size()-1;
and then listModel.get(currCD); would work just fine.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Jul 30th, 2007
0

Re: Getting my data back from my JList

Click to Expand / Collapse  Quote originally posted by Ezzaral ...
Your parsing problem is clear from the exception message:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""

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.
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?
Java Syntax (Toggle Plain Text)
  1. 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?
Java Syntax (Toggle Plain Text)
  1. newCD.setText(Integer.parseInt(getItemno()));
Is what my limited experience tells me, but of course that is wrong.
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Jul 30th, 2007
0

Re: Getting my data back from my JList

You simply call setText() on the textfield with the value from the cd object.
Java Syntax (Toggle Plain Text)
  1. itemField.setText( newCD.getItemno());
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: compiler from microjava to java....NEED HELP
Next Thread in Java Forum Timeline: how to find the version details?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC