By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,983 Members | 1,582 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,983 IT Pros & Developers. It's quick & easy.

listModle.modify

P: 38
I have been adding buttons to my GUI to manipulate list data.
I added a Delete button this morning in case I decide I no longer needed a particular element.
I am now working on a modify button, in case I want to keep the element, but only modify a field or two.
This seemed easy after my Delete, but now I am having issues with it.
Is there a listModel command out there that I am unfamiliar with that will help me with this?
Right now I have modify set up exactly like delete, just to put architecture in place without creating an error.
Any tips on what I should change to make it work correctly? Here is entire class.
Expand|Select|Wrap|Line Numbers
  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. import javax.swing.JFrame;
  9.  
  10. public class Inventory2 extends JFrame
  11. {
  12.     private JLabel cdNameLabel; // name label 
  13.     private JLabel artistLabel; // item number label 
  14.     private JLabel nstockLabel; // units in stock label 
  15.     private JLabel priceLabel; // price each label 
  16.     private JLabel itemLabel; // item number label 
  17.     private JTextField cdNameField; // name display 
  18.     private JTextField artistField; // artist display 
  19.     private JFormattedTextField nstockField; // units in stock display 
  20.     private JFormattedTextField priceField; // price each display 
  21.     private JTextField itemField; // item number display 
  22.     private NumberFormat nstockFormat; // format field and parse numbers 
  23.     private NumberFormat priceFormat; // format field and parse numbers 
  24.     private JButton btnAdd; // first button 
  25.     private JButton btnPrev; // previous button 
  26.     private JButton btnNext; // next button 
  27.     private JButton btnDel; // last button 
  28.     private JButton btnFirst; // first button
  29.     private JButton btnLast; // last button
  30.     private JButton btnModify; // modify button
  31.     private JButton btnSave; // save button
  32.     private JButton btnSearch; // search button
  33.     private JPanel buttonJPanel; // JPanle to hold buttons 
  34.     private JPanel fieldJPanel; // JPanel to hold labels and displays 
  35.     private JPanel fontJPanel; // JPanel to display logo 
  36.     private int currCD; 
  37.     private double total = 0; // variable for total inventory 
  38.     private JList Inventorylist; // JList to take place of old array
  39.     private DefaultListModel listModel;
  40.     private JScrollPane jScrollPanel;  
  41.     private float Invtotal = .00f;
  42.  
  43.      DecimalFormat formatter = new DecimalFormat("$0.00");
  44.  
  45.     public Inventory2() // create class and method to perform GUI build 
  46.     { 
  47.         initComponents(); 
  48.     } 
  49.  
  50.     private void initComponents() 
  51.     { 
  52.  
  53.         // create label names 
  54.         cdNameLabel = new JLabel("CD Name:"); 
  55.         artistLabel = new JLabel("Artist:");
  56.         nstockLabel = new JLabel("In Stock:"); 
  57.         priceLabel = new JLabel("Each Item Cost:$"); 
  58.         itemLabel = new JLabel("Item Number:"); 
  59.  
  60.  
  61.         // initial fields
  62.         cdNameField = new JTextField(25);
  63.         cdNameField.setEditable(true);
  64.         artistField = new JTextField(15);
  65.         artistField.setEditable(true);
  66.         nstockField = new JFormattedTextField(nstockFormat);
  67.         nstockField.setEditable(true);
  68.         nstockField.setColumns(5);
  69.         priceField = new JFormattedTextField(priceFormat);
  70.         priceField.setEditable(true);
  71.         priceField.setColumns(5);
  72.         itemField = new JTextField(4);
  73.         itemField.setEditable(true);
  74.  
  75.         // JList
  76.         jScrollPanel = new JScrollPane();
  77.         Inventorylist = new JList(); 
  78.         currCD = 0;
  79.  
  80.  
  81.         // buttons 
  82.         btnAdd = new JButton(); 
  83.         btnNext = new JButton(); 
  84.         btnPrev = new JButton(); 
  85.         btnDel = new JButton();
  86.         btnLast = new JButton();
  87.         btnFirst = new JButton();
  88.         btnModify = new JButton(); 
  89.         btnSave = new JButton();
  90.         btnSearch = new JButton();
  91.  
  92.         getContentPane().setLayout(new FlowLayout());
  93.         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  94.  
  95.         // add shapesJPanel to frame
  96.         ShapesJPanel shapesJPanel = new ShapesJPanel();    
  97.         getContentPane().add( new ShapesJPanel() );            
  98.  
  99.         // place textFields and labels
  100.  
  101.         //artist
  102.         artistLabel.setText("Artist"); 
  103.         getContentPane().add(artistLabel); 
  104.  
  105.         artistField.setMinimumSize(new Dimension(70,20)); 
  106.         artistField.setPreferredSize(new Dimension(70,20));
  107.         getContentPane().add(artistField); 
  108.  
  109.         // cd name
  110.         cdNameLabel.setText("CD Name"); 
  111.         getContentPane().add(cdNameLabel); 
  112.  
  113.         cdNameField.setMinimumSize(new Dimension(70,20)); 
  114.         cdNameField.setPreferredSize(new Dimension(70,20));
  115.         getContentPane().add(cdNameField);
  116.  
  117.         // copies in stock
  118.         nstockLabel.setText("Copies In Stock"); 
  119.         getContentPane().add(nstockLabel); 
  120.  
  121.         nstockField.setMinimumSize(new Dimension(5,20)); 
  122.         nstockField.setPreferredSize(new Dimension(5,20));
  123.         getContentPane().add(nstockField); 
  124.  
  125.         //price of cd
  126.         priceLabel.setText("Price"); 
  127.         getContentPane().add(priceLabel); 
  128.  
  129.         priceField.setMinimumSize(new Dimension(20,20)); 
  130.         priceField.setPreferredSize(new Dimension(20,20));
  131.         getContentPane().add(priceField); 
  132.  
  133.         //item number of cd
  134.         itemLabel.setText("Item Number"); 
  135.         getContentPane().add(itemLabel); 
  136.  
  137.         itemField.setMinimumSize(new Dimension(5,20)); 
  138.         itemField.setPreferredSize(new Dimension(5,20));
  139.         getContentPane().add(itemField); 
  140.  
  141.  
  142.  
  143.         // add listeners
  144.  
  145.         btnAdd.setText("Add");
  146.         btnAdd.addActionListener(new ActionListener()
  147.         {
  148.             public void actionPerformed(ActionEvent evt)
  149.             {
  150.                 btnAddActionPerformed(evt);
  151.             }
  152.         });
  153.         getContentPane().add(btnAdd);
  154.  
  155.         // PREVIOUS
  156.         btnPrev.setText("Previous");
  157.         btnPrev.addActionListener(new ActionListener()
  158.         {
  159.             public void actionPerformed(ActionEvent evt)
  160.             {
  161.                 btnPrevActionPerformed(evt);
  162.             }
  163.         });
  164.         getContentPane().add(btnPrev);
  165.  
  166.         // NEXT
  167.         btnNext.setText("Next");
  168.         btnNext.addActionListener(new ActionListener()
  169.         {
  170.             public void actionPerformed(ActionEvent evt)
  171.             {
  172.                 btnNextActionPerformed(evt);
  173.             }
  174.         });
  175.         getContentPane().add(btnNext);
  176.  
  177.         // SEARCH
  178.         btnSearch.setText("Search");
  179.         btnSearch.addActionListener(new ActionListener()
  180.         {
  181.             public void actionPerformed(ActionEvent evt)
  182.             {
  183.                 btnAddActionPerformed(evt);
  184.             }
  185.         });
  186.         getContentPane().add(btnSearch);
  187.  
  188.         // FIRST
  189.         btnFirst.setText("First");
  190.         btnFirst.addActionListener(new ActionListener()
  191.         {
  192.             public void actionPerformed(ActionEvent evt)
  193.             {
  194.                 btnFirstActionPerformed(evt);
  195.             }
  196.         });
  197.         getContentPane().add(btnFirst);
  198.  
  199.         // LAST
  200.         btnLast.setText("Last");
  201.         btnLast.addActionListener(new ActionListener()
  202.         {
  203.             public void actionPerformed(ActionEvent evt)
  204.             {
  205.                 btnLastActionPerformed(evt);
  206.             }
  207.         });
  208.         getContentPane().add(btnLast);
  209.  
  210.         // MODIFY
  211.         btnModify.setText("Modify");
  212.         btnModify.addActionListener(new ActionListener()
  213.         {
  214.             public void actionPerformed(ActionEvent evt)
  215.             {
  216.                 btnModifyActionPerformed(evt);
  217.             }
  218.         });
  219.         getContentPane().add(btnModify);
  220.  
  221.         // SAVE
  222.         btnSave.setText("Save");
  223.         btnSave.addActionListener(new ActionListener()
  224.         {
  225.             public void actionPerformed(ActionEvent evt)
  226.             {
  227.                 btnAddActionPerformed(evt);
  228.             }
  229.         });
  230.         getContentPane().add(btnSave);
  231.  
  232.         // DELETE
  233.         btnDel.setText("Delete");
  234.         btnDel.addActionListener(new ActionListener()
  235.         {
  236.             public void actionPerformed(ActionEvent evt)
  237.             {
  238.                 btnDeleteActionPerformed(evt);
  239.             }
  240.         });
  241.         getContentPane().add(btnDel);
  242.  
  243.  
  244.         // new Jlist model
  245.         listModel = new DefaultListModel();
  246.         Inventorylist.setModel(listModel);
  247.  
  248.         jScrollPanel.setViewportView(Inventorylist);
  249.  
  250.         getContentPane().add(jScrollPanel);
  251.  
  252.         pack();
  253.     }// close
  254.  
  255.  
  256.         private void btnAddActionPerformed(ActionEvent evt)
  257.         {
  258.             // Create cd to add
  259.             CdwArtist newCD = new CdwArtist();
  260.             newCD.setArtist(artistField.getText());
  261.             newCD.setName(cdNameField.getText());    
  262.             newCD.setItemno(Integer.parseInt(itemField.getText()));
  263.             newCD.setNstock(Integer.parseInt(nstockField.getText()));
  264.             newCD.setPrice(Float.parseFloat(priceField.getText()));
  265.  
  266.             // Add cd to list
  267.             listModel.addElement(newCD);
  268.             currCD = listModel.size()-1;  // sets currCD to added index
  269.  
  270.  
  271.             // Clear the text fields after add
  272.             artistField.setText(null);
  273.             cdNameField.setText(null);    
  274.             itemField.setText(null);
  275.             nstockField.setText(null);
  276.          priceField.setText(null);
  277.  
  278.             }// end ADD
  279.  
  280.         private void btnPrevActionPerformed(ActionEvent evt)
  281.         {
  282.             // Grab Previous cd 
  283.             if (--currCD<0) currCD = listModel.size()-1;
  284.             CdwArtist newCD = (CdwArtist) listModel.get( currCD );
  285.  
  286.  
  287.             artistField.setText(newCD.getArtist());
  288.             cdNameField.setText(newCD.getName());    
  289.             itemField.setText(String.valueOf(newCD.getItemno()));
  290.             nstockField.setText(String.valueOf(newCD.getNstock()));
  291.             priceField.setText(formatter.format(newCD.getPrice()));
  292.  
  293.  
  294.             }// end PREV
  295.  
  296.                 private void btnNextActionPerformed(ActionEvent evt)
  297.             {
  298.             // Grab Next cd 
  299.             if (++currCD >= listModel.size()) currCD= 0;
  300.             CdwArtist newCD = (CdwArtist) listModel.get( currCD );
  301.  
  302.  
  303.             artistField.setText(newCD.getArtist());
  304.             cdNameField.setText(newCD.getName());    
  305.             itemField.setText(String.valueOf(newCD.getItemno()));
  306.             nstockField.setText(String.valueOf(newCD.getNstock()));
  307.             priceField.setText(formatter.format(newCD.getPrice()));
  308.  
  309.  
  310.             }// end NEXT
  311.  
  312.  
  313.                 private void btnFirstActionPerformed(ActionEvent evt)
  314.             {
  315.             // Grab First cd 
  316.             CdwArtist newCD = (CdwArtist) listModel.get(0);
  317.  
  318.  
  319.             artistField.setText(newCD.getArtist());
  320.             cdNameField.setText(newCD.getName());    
  321.             itemField.setText(String.valueOf(newCD.getItemno()));
  322.             nstockField.setText(String.valueOf(newCD.getNstock()));
  323.             priceField.setText(formatter.format(newCD.getPrice()));
  324.  
  325.  
  326.             }// end FIRST
  327.  
  328.  
  329.                 private void btnLastActionPerformed(ActionEvent evt)
  330.             {
  331.             // Grab Last cd 
  332.             CdwArtist newCD = (CdwArtist) listModel.lastElement();
  333.  
  334.  
  335.             artistField.setText(newCD.getArtist());
  336.             cdNameField.setText(newCD.getName());    
  337.             itemField.setText(String.valueOf(newCD.getItemno()));
  338.             nstockField.setText(String.valueOf(newCD.getNstock()));
  339.             priceField.setText(formatter.format(newCD.getPrice()));
  340.  
  341.  
  342.             }// end LAST
  343.  
  344.                 private void btnDeleteActionPerformed(ActionEvent evt)
  345.             {
  346.             // Delete cd 
  347.             listModel.remove(currCD);
  348.  
  349.  
  350.             // Clear the text fields after delete
  351.             artistField.setText(null);
  352.             cdNameField.setText(null);    
  353.             itemField.setText(null);
  354.             nstockField.setText(null);
  355.          priceField.setText(null);
  356.  
  357.  
  358.             }// end DELETE
  359.  
  360.                 private void btnModifyActionPerformed(ActionEvent evt)
  361.             {
  362.             // Modify cd 
  363.             listModel.remove(currCD);
  364.  
  365.  
  366.             // Clear the text fields after delete
  367.             artistField.setText(null);
  368.             cdNameField.setText(null);    
  369.             itemField.setText(null);
  370.             nstockField.setText(null);
  371.          priceField.setText(null);
  372.  
  373.  
  374.             }// end Modify
  375.  
  376.         // run it
  377.         public static void main(String args[])
  378.         {
  379.         //JFrame frame = new JFrame( "CD Inventory Logo" );
  380.       //frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  381.  
  382.       // create ShapesJPanel
  383.       //ShapesJPanel shapesJPanel = new ShapesJPanel();
  384.       //frame.add( shapesJPanel ); // add shapesJPanel to frame
  385.       //frame.setSize( 125, 200 ); // set frame size
  386.       //frame.setVisible( true ); // display frame
  387.         java.awt.EventQueue.invokeLater(new Runnable()
  388.             {
  389.             public void run()
  390.                 {
  391.                 new Inventory2().setVisible(true);
  392.                }
  393.             });
  394.         }
  395.  
  396. } // close class
Jul 31 '07 #1
Share this Question
Share on Google+
23 Replies


Expert 10K+
P: 11,448
You do keep track of a 'currCD' all the time, so why don't you just delete the
current 'currCD' and insert a new one?

kind regards,

Jos
Jul 31 '07 #2

10K+
P: 13,264
I have not tested your code to see what you're talking about, but you should model your modify around your add rather than your delete. The difference with add should be that with modify you have a product to display first and when a save is done, you don't create a new product but simply override an existing one.
Jul 31 '07 #3

10K+
P: 13,264
You do keep track of a 'currCD' all the time, so why don't you just delete the
current 'currCD' and insert a new one?

kind regards,

Jos
I haven't looked at the code but I would think they have a unique key for each product that they want to mantain after an edit.
Jul 31 '07 #4

Expert 10K+
P: 11,448
I haven't looked at the code but I would think they have a unique key for each product that they want to mantain after an edit.
No he hasn't; check out the btnAddActionPerformed() logic: it just adds a new
CD to the list no matter what. A delete/insert pair would do fine for an update
here.

kind regards,

Jos
Jul 31 '07 #5

10K+
P: 13,264
No he hasn't; check out the btnAddActionPerformed() logic: it just adds a new
CD to the list no matter what. A delete/insert pair would do fine for an update
here.

kind regards,

Jos
I just thought even if he had some key, he could then create the new one using that old key anyway ...
Jul 31 '07 #6

P: 38
That is on the horizon ... sort of.
I will be changing the itemno field to stay in order. For example,
if I have 10 cds, they should be labeled 1-10 in order.
if we delete # 8, the remaining 9 should reorder accordingly ... if we add one, it should automatically take on the next number in line.

I do not think that will affect this current task at all though.
like listmodel.remove, listmodel.add, listmodel.firsst and all that, I was hoping for a quick listmode.modify ... but if it does not exist I am willing to do a quick delete add.

this is what I have now:
Expand|Select|Wrap|Line Numbers
  1. private void btnModifyActionPerformed(ActionEvent evt)
  2.             {
  3.             // Modify cd
  4.             //listModel.addElement(newCD);
  5.             CdwArtist newCD = (CdwArtist) listModel.get( currCD );
  6.             newCD.setArtist(artistField.getText());
  7.             newCD.setName(cdNameField.getText());    
  8.             newCD.setItemno(Integer.parseInt(itemField.getText()));
  9.             newCD.setNstock(Integer.parseInt(nstockField.getText()));
  10.             newCD.setPrice(Float.parseFloat(priceField.getText()));
  11.  
  12.  
  13.             // Clear the text fields after Modify
  14.             artistField.setText(null);
  15.             cdNameField.setText(null);    
  16.             itemField.setText(null);
  17.             nstockField.setText(null);
  18.          priceField.setText(null);
  19.  
  20.  
  21.             }// end Modify
Jul 31 '07 #7

P: 38
I have tried to Delete current, and then Add new cd but it is not working.

Can you see my problem? Logic being, delete whatever cd I am currently looking at, take what is in the fields at that time (modified data) and create a new element. After new element is added, clear fields.
Expand|Select|Wrap|Line Numbers
  1.     // Modify cd
  2.             listModel.remove(currCD);
  3.  
  4.             // Create cd to add
  5.             CdwArtist newCD = new CdwArtist();
  6.             newCD.setArtist(artistField.getText());
  7.             newCD.setName(cdNameField.getText());    
  8.             newCD.setItemno(Integer.parseInt(itemField.getText()));
  9.             newCD.setNstock(Integer.parseInt(nstockField.getText()));
  10.             newCD.setPrice(Float.parseFloat(priceField.getText()));
  11.  
  12.             // Add cd to list
  13.             listModel.addElement(newCD);
  14.             currCD = listModel.size()-1;  // sets currCD to added index
  15.  
  16.  
  17.             // Clear the text fields after add
  18.             artistField.setText(null);
  19.             cdNameField.setText(null);    
  20.             itemField.setText(null);
  21.             nstockField.setText(null);
  22.          priceField.setText(null);
Jul 31 '07 #8

Expert 10K+
P: 11,448
I have tried to Delete current, and then Add new cd but it is not working.
You have to elaborate a bit on what 'not working' means. Is the old CD gone?
Isn't the new CD added? Adre they both there in the list? Are none of the two
present in the list?

kind regards,

Jos
Jul 31 '07 #9

P: 38
You are correct, I am sorry. (new to this)
When I hit Modify ..
The last entered cd into my list is deleted, NOT the one I have in the fields ... then I get these errors
Expand|Select|Wrap|Line Numbers
  1. ---jGRASP exec: java Inventory2
  2.  
  3. Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "$1.00"
  4.     at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1224)
  5.     at java.lang.Float.parseFloat(Float.java:422)
  6.     at Inventory2.btnModifyActionPerformed(Inventory2.java:374)
  7.     at Inventory2.access$500(Inventory2.java:11)
  8.     at Inventory2$7.actionPerformed(Inventory2.java:219)
  9.     at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
  10.     at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
  11.     at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
  12.     at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
  13.     at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
  14.     at java.awt.Component.processMouseEvent(Component.java:6038)
  15.     at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
  16.     at java.awt.Component.processEvent(Component.java:5803)
  17.     at java.awt.Container.processEvent(Container.java:2058)
  18.     at java.awt.Component.dispatchEventImpl(Component.java:4410)
  19.     at java.awt.Container.dispatchEventImpl(Container.java:2116)
  20.     at java.awt.Component.dispatchEvent(Component.java:4240)
  21.     at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
  22.     at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
  23.     at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
  24.     at java.awt.Container.dispatchEventImpl(Container.java:2102)
  25.     at java.awt.Window.dispatchEventImpl(Window.java:2429)
  26.     at java.awt.Component.dispatchEvent(Component.java:4240)
  27.     at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
  28.     at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
  29.     at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
  30.     at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
  31.     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
  32.     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
  33.     at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
Jul 31 '07 #10

Expert 10K+
P: 11,448
You are correct, I am sorry. (new to this)
When I hit Modify ..
The last entered cd into my list is deleted, NOT the one I have in the fields ...
That implies that the variable 'currCD' doesn't point to the CD your text fields
are showing. I'd suggest you always repopulate those text fields with the values
of the CD that 'currCD' is pointing at.

kind regards,

Jos
Jul 31 '07 #11

10K+
P: 13,264
And that exception tells you a line number that you should check in your code. Remember that how you display your data is usually different from how you store it.
Jul 31 '07 #12

P: 38
I see. Playing with my app, trying to debug I have discovered some kind of currCD problem.
My delete button deletes the last cd put in the list, not what I think should be the currCD.
When I hist FIRST for example, the first cd in the list populates my fields ... is that not set up to be my currCD?
The way I read it, it is. Why then would my Delete button not delete that element?

This confuses me.
Jul 31 '07 #13

Expert 10K+
P: 11,448
I see. Playing with my app, trying to debug I have discovered some kind of currCD problem.
My delete button deletes the last cd put in the list, not what I think should be the currCD.
When I hist FIRST for example, the first cd in the list populates my fields ... is that not set up to be my currCD?
Yes it should but you didn't set currrCD= 0 in your 'first' method.

kind regards,

Jos
Aug 1 '07 #14

P: 38
That makes perfect sense now doesn't it!?

Of course I should then have to set it for my LAST button as well, which I thought I did, but same problem.
Is this not the equivilent to the last element? Is listModel.size?
Expand|Select|Wrap|Line Numbers
  1. CdwArtist newCD = (CdwArtist) listModel.lastElement();
  2.             currCD = listModel.size();
  3.  
  4.  
  5.             artistField.setText(newCD.getArtist());
  6.             cdNameField.setText(newCD.getName());    
  7.             itemField.setText(String.valueOf(newCD.getItemno()));
  8.             nstockField.setText(String.valueOf(newCD.getNstock()));
  9.             priceField.setText(formatter.format(newCD.getPrice()));
  10.  
  11.  
  12.             }// end LAST
Aug 1 '07 #15

Expert 10K+
P: 11,448
That makes perfect sense now doesn't it!?

Of course I should then have to set it for my LAST button as well, which I thought I did, but same problem.
Is this not the equivilent to the last element? Is listModel.size?
Expand|Select|Wrap|Line Numbers
  1. CdwArtist newCD = (CdwArtist) listModel.lastElement();
  2.             currCD = listModel.size();
  3.  
  4.  
  5.             artistField.setText(newCD.getArtist());
  6.             cdNameField.setText(newCD.getName());    
  7.             itemField.setText(String.valueOf(newCD.getItemno()));
  8.             nstockField.setText(String.valueOf(newCD.getNstock()));
  9.             priceField.setText(formatter.format(newCD.getPrice()));
  10.  
  11.  
  12.             }// end LAST
I'd make that listmodel.size()-1 if I were you because that's the index of the last
CD in the list.

kind regards,

Jos
Aug 1 '07 #16

P: 38
perfect. I think I should have known that.
Of course, this leaves me at my original problem.
When I pull in a cd to modify, it performs step one and deletes the current element, but goes all crazy without adding the new cd.
It looks as if it does not like the formatting of my fields, but I cannot figure out why.
This is exactly how I ADD cds to begin with, why would it not work this way?
Here are my buttons, so you can see what I have done.
Expand|Select|Wrap|Line Numbers
  1. private void btnAddActionPerformed(ActionEvent evt)
  2.         {
  3.             // Create cd to add
  4.             CdwArtist newCD = new CdwArtist();
  5.             newCD.setArtist(artistField.getText());
  6.             newCD.setName(cdNameField.getText());    
  7.             newCD.setItemno(Integer.parseInt(itemField.getText()));
  8.             newCD.setNstock(Integer.parseInt(nstockField.getText()));
  9.             newCD.setPrice(Float.parseFloat(priceField.getText()));
  10.  
  11.             // Add cd to list
  12.             listModel.addElement(newCD);
  13.             currCD = listModel.size()-1;  // sets currCD to added index
  14.  
  15.  
  16.             // Clear the text fields after add
  17.             artistField.setText(null);
  18.             cdNameField.setText(null);    
  19.             itemField.setText(null);
  20.             nstockField.setText(null);
  21.          priceField.setText(null);
  22.  
  23.             }// end ADD
  24.  
  25.         private void btnPrevActionPerformed(ActionEvent evt)
  26.         {
  27.             // Grab Previous cd 
  28.             if (--currCD<0) currCD = listModel.size()-1;
  29.             CdwArtist newCD = (CdwArtist) listModel.get( currCD );
  30.  
  31.  
  32.             artistField.setText(newCD.getArtist());
  33.             cdNameField.setText(newCD.getName());    
  34.             itemField.setText(String.valueOf(newCD.getItemno()));
  35.             nstockField.setText(String.valueOf(newCD.getNstock()));
  36.             priceField.setText(formatter.format(newCD.getPrice()));
  37.  
  38.  
  39.             }// end PREV
  40.  
  41.                 private void btnNextActionPerformed(ActionEvent evt)
  42.             {
  43.             // Grab Next cd 
  44.             if (++currCD >= listModel.size()) currCD= 0;
  45.             CdwArtist newCD = (CdwArtist) listModel.get( currCD );
  46.  
  47.  
  48.             artistField.setText(newCD.getArtist());
  49.             cdNameField.setText(newCD.getName());    
  50.             itemField.setText(String.valueOf(newCD.getItemno()));
  51.             nstockField.setText(String.valueOf(newCD.getNstock()));
  52.             priceField.setText(formatter.format(newCD.getPrice()));
  53.  
  54.  
  55.             }// end NEXT
  56.  
  57.  
  58.                 private void btnFirstActionPerformed(ActionEvent evt)
  59.             {
  60.             // Grab First cd 
  61.             CdwArtist newCD = (CdwArtist) listModel.get(0);
  62.             currCD = 0;
  63.  
  64.             artistField.setText(newCD.getArtist());
  65.             cdNameField.setText(newCD.getName());    
  66.             itemField.setText(String.valueOf(newCD.getItemno()));
  67.             nstockField.setText(String.valueOf(newCD.getNstock()));
  68.             priceField.setText(formatter.format(newCD.getPrice()));
  69.  
  70.  
  71.             }// end FIRST
  72.  
  73.  
  74.                 private void btnLastActionPerformed(ActionEvent evt)
  75.             {
  76.             // Grab Last cd 
  77.             CdwArtist newCD = (CdwArtist) listModel.lastElement();
  78.             currCD = listModel.size()-1;
  79.  
  80.  
  81.             artistField.setText(newCD.getArtist());
  82.             cdNameField.setText(newCD.getName());    
  83.             itemField.setText(String.valueOf(newCD.getItemno()));
  84.             nstockField.setText(String.valueOf(newCD.getNstock()));
  85.             priceField.setText(formatter.format(newCD.getPrice()));
  86.  
  87.  
  88.             }// end LAST
  89.  
  90.                 private void btnDeleteActionPerformed(ActionEvent evt)
  91.             {
  92.             // Delete cd 
  93.             listModel.remove(currCD);
  94.  
  95.  
  96.             // Clear the text fields after delete
  97.             artistField.setText(null);
  98.             cdNameField.setText(null);    
  99.             itemField.setText(null);
  100.             nstockField.setText(null);
  101.          priceField.setText(null);
  102.  
  103.  
  104.             }// end DELETE
  105.  
  106.                 private void btnModifyActionPerformed(ActionEvent evt)
  107.             {
  108.             // Modify cd
  109.             listModel.remove(currCD);
  110.  
  111.             // Create cd to add
  112.             CdwArtist newCD = new CdwArtist();
  113.             newCD.setArtist(artistField.getText());
  114.             newCD.setName(cdNameField.getText());    
  115.             newCD.setItemno(Integer.parseInt(itemField.getText()));
  116.             newCD.setNstock(Integer.parseInt(nstockField.getText()));
  117.             newCD.setPrice(Float.parseFloat(priceField.getText()));
  118.  
  119.             // Add cd to list
  120.             listModel.addElement(newCD);
  121.             currCD = listModel.size()-1;  // sets currCD to added index
  122.  
  123.  
  124.             // Clear the text fields after add
  125.             artistField.setText(null);
  126.             cdNameField.setText(null);    
  127.             itemField.setText(null);
  128.             nstockField.setText(null);
  129.          priceField.setText(null);
  130.  
  131.  
  132.             }// end Modify
And here are the erros. I think I know what they mean, I just do not understand why, or how to fix it.
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "$2.00"
at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:1224)
at java.lang.Float.parseFloat(Float.java:422)
at Inventory2.btnModifyActionPerformed(Inventory2.jav a:374)
at Inventory2.access$500(Inventory2.java:11)
at Inventory2$7.actionPerformed(Inventory2.java:218)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.jav a:6038)
at javax.swing.JComponent.processMouseEvent(JComponen t.java:3260)
at java.awt.Component.processEvent(Component.java:580 3)
at java.awt.Container.processEvent(Container.java:205 8)
at java.awt.Component.dispatchEventImpl(Component.jav a:4410)
at java.awt.Container.dispatchEventImpl(Container.jav a:2116)
at java.awt.Component.dispatchEvent(Component.java:42 40)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:3916)
at java.awt.Container.dispatchEventImpl(Container.jav a:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429 )
at java.awt.Component.dispatchEvent(Component.java:42 40)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:121)
Aug 1 '07 #17

10K+
P: 13,264
I've already said something about that one.
Aug 1 '07 #18

Expert 10K+
P: 11,448
java.lang.NumberFormatException: For input string: "$2.00"
Where does that dollar sign come from? Do you anticipate for that?

kind regards,

Jos
Aug 1 '07 #19

P: 38
I know it has been commented on earlier in the thread, but I did not understand the solution.
I know the list does not like the information I am trying to pass to it.

The $ comes from the decimal formatter I have put in.
That was the issue. I took the $ out of the formatter and just left it at 0.00, put the $ into the label instead, and all is well.

You guys are great.

Of course that was the simpler of my two problems. I have some debugging left to try on the Search button I am trying to implement.

Thanks again for your help.
Aug 1 '07 #20

Expert 10K+
P: 11,448
Of course that was the simpler of my two problems. I have some debugging left to try on the Search button I am trying to implement.

Thanks again for your help.
You're welcome of course; you also have some refactoring to do because re-
populating all those text fields over and over again deserves a little method of
its own and not to be repeated in every single btnSoAndSoActionPerformed
method. (Same goes for clearing the fields in your add and update methods).
You copy and paste too much ;-)

kind regards,

Jos
Aug 1 '07 #21

10K+
P: 13,264
I know it has been commented on earlier in the thread, but I did not understand the solution.
I know the list does not like the information I am trying to pass to it.

The $ comes from the decimal formatter I have put in.
That was the issue. I took the $ out of the formatter and just left it at 0.00, put the $ into the label instead, and all is well.

You guys are great.

Of course that was the simpler of my two problems. I have some debugging left to try on the Search button I am trying to implement.

Thanks again for your help.
What I was trying to tell you earlier is something like that. You store the price as a double say 2.00. When diplsying to the user you want to show them that it is money by displaying the $ as well, $2.00. A JLabel is just for display, so you use the $2.00 version. A JTextField, however, is for taking in input. So either you force the user to enter it as 2.00 (using a validation) or allow the user to enter the $2.00 version and remove that $ in the background (code) so that you store it correctly as a double.
Aug 1 '07 #22

P: 38
You are right Jos. I have only 2 or 3 more things to do in order to close out this app and get it doing what I want it to.
I will then go back through it and "clean it up" ... this is my first experience with Java and I am just trying to see what is what right now.

Validation is also a concern when I go through on my second sweep. I fear if I try and do everything at once then I may just confuse myself further and do more harm than good.
Aug 1 '07 #23

Expert 10K+
P: 11,448
I will then go back through it and "clean it up" ... this is my first experience with Java and I am just trying to see what is what right now.
Good; do one thing first before you do the next thing. You're on the right track.
Otherwise you end up asking about Collections while File input still doesn't
work and curly brackets still don't match up and being puzzled why A$ is
not considered a valid identifier and a foo String == bar String still won't work ;-)

kind regards,

Jos
Aug 1 '07 #24

Post your reply

Sign in to post your reply or Sign up for a free account.