473,782 Members | 2,423 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

listModle.modif y

38 New Member
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
23 1985
JosAH
11,448 Recognized Expert MVP
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
Aug 1 '07 #21
r035198x
13,262 MVP
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
no1zson
38 New Member
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
JosAH
11,448 Recognized Expert MVP
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

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

Similar topics

1
4486
by: Franco Fellico' | last post by:
Hi. Suppose to have read and displayed (using PHP) a group of row of a DB table on a dinamyc table on a HTML/PHP page. The number of row displayed could be from 1 to n. Each row contains informations and also a cell with a unique ID of the element. I would like to show at the beginning of each line of the table a couple of cells containing two different icons (one for delete and one
28
12606
by: Charles Sullivan | last post by:
I'm working on a program which has a "tree" of command line arguments, i.e., myprogram level1 ]] such that there can be more than one level2 argument for each level1 argument and more than one level3 argument for each level2 argument, etc. Suppose I code it similar to this fragment: int main( int argc, char *argv ) {
13
2516
by: baumann.Pan | last post by:
when define char *p = " can not modify"; p ='b' ;is not allowed, but if you declare p as char p = "can modify"; p = 'b'; is ok? why?
12
2369
by: Michael B Allen | last post by:
Is it legit to modify static data like the following code? #include <stdlib.h> #include <stdio.h> struct tbl { int i; char *s; };
5
4481
by: Martin Bischoff | last post by:
Hi, is it possible to modify the values of a SqlDataSource's select parameters in the code behind before the select command is executed? Example: I have an SqlDataSource with a ControlParameter <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:XYZ %>"
3
3217
by: Maxwell2006 | last post by:
Hi, When I run a web service project, ASP.NET shows me a default web method invoke page. How can I disable/modify the default test (or method invoke) page of the ASP.NET web services? Thank you, Max
2
8794
by: Bob | last post by:
Hi, I have a list of widgets. I want to iterate through the selected items collection and modify one of the widgets properties. I tried foreach(widget w in lst.SelectedItems) w.MyProperty = something; but I get a message saying "Cannot modify members of w because it is a foreach iteration variable" What is the point of iteration if you can't modify the objects?
6
7875
by: Kiran | last post by:
Hi all, What I am trying to do is to pass a pointer to the first element of an array to a function, modify it in that function, and then print out the values of the array (which has been modified by the function) in main. However, I am getting a segmentation fault. Here is the code: (Please note, the size is fixed in this code, but in my code where I am actually going to use this, the size of the array is not known until you get to the...
1
7443
by: TimEl | last post by:
Hi. Using Perl, I want to modify data in an XML file and print out the entire modified file, not just the elements I modify. In CPAN I have found that XPath allows me to pinpoint the elements that I want to modify. But all of the code examples that I have seen assume that I want to assign the targeted elements to variables, modify each element, and then print only the modified elements out to a file. For example, this code is found at...
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10313
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6735
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.