listModle.modify

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #16
    Originally posted by no1zson
    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?
    Code:
    CdwArtist newCD = (CdwArtist) listModel.lastElement();
    			currCD = listModel.size();
    			
    
    			artistField.setText(newCD.getArtist());
    			cdNameField.setText(newCD.getName());	
    			itemField.setText(String.valueOf(newCD.getItemno()));
    			nstockField.setText(String.valueOf(newCD.getNstock()));
    			priceField.setText(formatter.format(newCD.getPrice()));
    					
    			
    			}// 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

    Comment

    • no1zson
      New Member
      • Jul 2007
      • 38

      #17
      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.
      Code:
      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 
      			if (--currCD<0) currCD = listModel.size()-1;
      			CdwArtist newCD = (CdwArtist) listModel.get( currCD );
      			
      
      			artistField.setText(newCD.getArtist());
      			cdNameField.setText(newCD.getName());	
      			itemField.setText(String.valueOf(newCD.getItemno()));
      			nstockField.setText(String.valueOf(newCD.getNstock()));
      			priceField.setText(formatter.format(newCD.getPrice()));
      					
      			
      			}// end PREV
      			
      				private void btnNextActionPerformed(ActionEvent evt)
      			{
      			// Grab Next cd 
      			if (++currCD >= listModel.size()) currCD= 0;
      			CdwArtist newCD = (CdwArtist) listModel.get( currCD );
      			
      
      			artistField.setText(newCD.getArtist());
      			cdNameField.setText(newCD.getName());	
      			itemField.setText(String.valueOf(newCD.getItemno()));
      			nstockField.setText(String.valueOf(newCD.getNstock()));
      			priceField.setText(formatter.format(newCD.getPrice()));
      					
      			
      			}// end NEXT
      			
      			
      				private void btnFirstActionPerformed(ActionEvent evt)
      			{
      			// Grab First cd 
      			CdwArtist newCD = (CdwArtist) listModel.get(0);
      			currCD = 0;
      				
      			artistField.setText(newCD.getArtist());
      			cdNameField.setText(newCD.getName());	
      			itemField.setText(String.valueOf(newCD.getItemno()));
      			nstockField.setText(String.valueOf(newCD.getNstock()));
      			priceField.setText(formatter.format(newCD.getPrice()));
      					
      			
      			}// end FIRST
      			
      			
      				private void btnLastActionPerformed(ActionEvent evt)
      			{
      			// Grab Last cd 
      			CdwArtist newCD = (CdwArtist) listModel.lastElement();
      			currCD = listModel.size()-1;
      			
      
      			artistField.setText(newCD.getArtist());
      			cdNameField.setText(newCD.getName());	
      			itemField.setText(String.valueOf(newCD.getItemno()));
      			nstockField.setText(String.valueOf(newCD.getNstock()));
      			priceField.setText(formatter.format(newCD.getPrice()));
      					
      			
      			}// end LAST
      			
      				private void btnDeleteActionPerformed(ActionEvent evt)
      			{
      			// Delete cd 
      			listModel.remove(currCD);
      				
      			
      			// Clear the text fields after delete
      			artistField.setText(null);
      			cdNameField.setText(null);	
      			itemField.setText(null);
      			nstockField.setText(null);
               priceField.setText(null);
      					
      			
      			}// end DELETE
      			
      				private void btnModifyActionPerformed(ActionEvent evt)
      			{
      			// Modify cd
      			listModel.remove(currCD);
      			
      			// 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 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.Numbe rFormatExceptio n: For input string: "$2.00"
      at sun.misc.Floati ngDecimal.readJ avaFormatString (FloatingDecima l.java:1224)
      at java.lang.Float .parseFloat(Flo at.java:422)
      at Inventory2.btnM odifyActionPerf ormed(Inventory 2.java:374)
      at Inventory2.acce ss$500(Inventor y2.java:11)
      at Inventory2$7.ac tionPerformed(I nventory2.java: 218)
      at javax.swing.Abs tractButton.fir eActionPerforme d(AbstractButto n.java:1995)
      at javax.swing.Abs tractButton$Han dler.actionPerf ormed(AbstractB utton.java:2318 )
      at javax.swing.Def aultButtonModel .fireActionPerf ormed(DefaultBu ttonModel.java: 387)
      at javax.swing.Def aultButtonModel .setPressed(Def aultButtonModel .java:242)
      at javax.swing.pla f.basic.BasicBu ttonListener.mo useReleased(Bas icButtonListene r.java:236)
      at java.awt.Compon ent.processMous eEvent(Componen t.java:6038)
      at javax.swing.JCo mponent.process MouseEvent(JCom ponent.java:326 0)
      at java.awt.Compon ent.processEven t(Component.jav a:5803)
      at java.awt.Contai ner.processEven t(Container.jav a:2058)
      at java.awt.Compon ent.dispatchEve ntImpl(Componen t.java:4410)
      at java.awt.Contai ner.dispatchEve ntImpl(Containe r.java:2116)
      at java.awt.Compon ent.dispatchEve nt(Component.ja va:4240)
      at java.awt.Lightw eightDispatcher .retargetMouseE vent(Container. java:4322)
      at java.awt.Lightw eightDispatcher .processMouseEv ent(Container.j ava:3986)
      at java.awt.Lightw eightDispatcher .dispatchEvent( Container.java: 3916)
      at java.awt.Contai ner.dispatchEve ntImpl(Containe r.java:2102)
      at java.awt.Window .dispatchEventI mpl(Window.java :2429)
      at java.awt.Compon ent.dispatchEve nt(Component.ja va:4240)
      at java.awt.EventQ ueue.dispatchEv ent(EventQueue. java:599)
      at java.awt.EventD ispatchThread.p umpOneEventForF ilters(EventDis patchThread.jav a:273)
      at java.awt.EventD ispatchThread.p umpEventsForFil ter(EventDispat chThread.java:1 83)
      at java.awt.EventD ispatchThread.p umpEventsForHie rarchy(EventDis patchThread.jav a:173)
      at java.awt.EventD ispatchThread.p umpEvents(Event DispatchThread. java:168)
      at java.awt.EventD ispatchThread.p umpEvents(Event DispatchThread. java:160)
      at java.awt.EventD ispatchThread.r un(EventDispatc hThread.java:12 1)

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #18
        I've already said something about that one.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #19
          Originally posted by no1zson
          java.lang.Numbe rFormatExceptio n: For input string: "$2.00"
          Where does that dollar sign come from? Do you anticipate for that?

          kind regards,

          Jos

          Comment

          • no1zson
            New Member
            • Jul 2007
            • 38

            #20
            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.

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #21
              Originally posted by no1zson
              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 btnSoAndSoActio nPerformed
              method. (Same goes for clearing the fields in your add and update methods).
              You copy and paste too much ;-)

              kind regards,

              Jos

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #22
                Originally posted by no1zson
                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.

                Comment

                • no1zson
                  New Member
                  • Jul 2007
                  • 38

                  #23
                  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.

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #24
                    Originally posted by no1zson
                    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

                    Comment

                    Working...