Inventory program part 5 help

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2007
Posts: 25
Reputation: dkdeleon68 is an unknown quantity at this point 
Solved Threads: 0
dkdeleon68 dkdeleon68 is offline Offline
Light Poster

Re: Inventory program part 5 help

 
0
  #11
Sep 11th, 2007
I'm sorry I am still new to this and do not understand the language yet, so I did not understand what you mean?
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,200
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 486
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Inventory program part 5 help

 
0
  #12
Sep 11th, 2007
What Ezzaral is sugesting is create a variable which will hold value/position of currently selected item. On the start it will be zero as you wish to display first value from your array. When you press Next this value get increased by one. When you press Previous get decreased. Last it will change to length of your array minus one. If First selected the value will set to zero.
When this done you should thing what you want to happens when you reach last position of array. Do you want to start automaticaly from first array variable or you want to disable next move. Then apply same for first position in the array
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,483
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 515
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Inventory program part 5 help

 
0
  #13
Sep 11th, 2007
Yes, peter_budo's explanation is exactly what I was suggesting.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 25
Reputation: dkdeleon68 is an unknown quantity at this point 
Solved Threads: 0
dkdeleon68 dkdeleon68 is offline Offline
Light Poster

Re: Inventory program part 5 help

 
0
  #14
Sep 12th, 2007
i want to thank both of you for helping me.

ok, this is what I have done but the buttons still do not change the inventory to the next, previous, or the others.

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.text.NumberFormat; // used to format currency
  4. import javax.swing.*;
  5.  
  6. import javax.swing.Icon;
  7. import javax.swing.ImageIcon;
  8. import javax.swing.JButton;
  9. import javax.swing.JFrame;
  10. import javax.swing.JLabel;
  11. import javax.swing.JComboBox;
  12. import javax.swing.JPanel;
  13. import javax.swing.JScrollPane;
  14. import javax.swing.JTextArea;
  15.  
  16. class InventoryMain extends Frame implements ActionListener
  17. {
  18. CD[] product;
  19. JTextField[] fields;
  20. NumberFormat nf;
  21. JButton jbtOk, jbtFirst, jbtPrev, jbtNext, jbtLast;
  22. static int dispProd = 0; // variable for actionEvents
  23. // main method begins execution of java application
  24.  
  25. static JTextArea textArea;
  26.  
  27. public static void main(String[] args)
  28. {
  29. int i; // varialbe for looping
  30. double total = 0; // variable for total inventory
  31. try
  32. {
  33. UIManager.setLookAndFeel(
  34. "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
  35. }
  36. catch (Exception e)
  37. {
  38. System.err.println(e.getClass().getName() + ": " + e.getMessage());
  39. }
  40. InventoryMain test = new InventoryMain();
  41. test.initCD();
  42. test.showGUI();
  43. test.populateFields(0);
  44. }
  45.  
  46. public void actionPerformed(ActionEvent ae)
  47. {
  48. if(ae.getSource() == jbtOk)
  49. {
  50. // add OK button functionality
  51. System.out.println("OK pressed");
  52. }
  53. if(ae.getSource() == jbtFirst)
  54. {
  55. // add First button functionality
  56. System.out.println("FIRST pressed");
  57. }
  58. if(ae.getSource() == jbtPrev)
  59. {
  60. // add Previous button functionality
  61. System.out.println("PREVIOUS pressed");
  62. }
  63. if(ae.getSource() == jbtNext)
  64. {
  65. // add Next button functionality
  66. System.out.println("NEXT pressed");
  67. }
  68. if(ae.getSource() == jbtLast)
  69. {
  70. // add Last button functionality
  71. System.out.println("LAST pressed");
  72. }
  73. }
  74.  
  75. public void initCD() {
  76. //Create an array
  77. product = new CD[10];
  78.  
  79. //fill in the classes
  80. product[0] = new CD( 1, "Crowbar" , 20.00, 10, "00722");
  81. product[1] = new CD( 2, "Pantera" , 20.00, 10, "00263");
  82. product[2] = new CD( 3, "Ozzy" , 15.00, 10, "00142");
  83. product[3] = new CD( 4, "Soulfly" , 18.00, 10, "00553");
  84. product[4] = new CD( 5, "Down", 24.00, 10, "00789");
  85. product[5] = new CD( 6, "God Forbid" , 10.00, 10, "00712");
  86. product[6] = new CD( 7, "Black Label Society" , 16.00, 10, "00458" );
  87. product[7] = new CD( 8, "Saint Vitus" , 15.00, 10, "00889");
  88. product[8] = new CD( 9, "Clearlight" , 15.00, 10, "00897");
  89. product[9] = new CD( 10, "Testament" , 15.00, 10, "00656");
  90.  
  91. //Sort elements in array in alphabetical order by product name
  92. // product[0].sortItems(product);
  93.  
  94.  
  95.  
  96. }


  1. private void showGUI() {
  2. JLabel l;
  3. JButton b;
  4. fields = new JTextField[9];
  5. JComboBox combo = new JComboBox();
  6. for(int j = 0; j < product.length; j++)
  7. combo.addItem(product[j].getName());
  8. combo.addActionListener(this);
  9. JFrame f = new JFrame("InventoryGUI");
  10. Container cp = f.getContentPane();
  11. cp.setLayout(new GridBagLayout());
  12. cp.setBackground(UIManager.getColor("control"));
  13. GridBagConstraints c = new GridBagConstraints();
  14. c.gridx = 0;
  15. c.gridy = GridBagConstraints.RELATIVE;
  16. c.gridwidth = 1;
  17. c.gridheight = 1;
  18. c.insets = new Insets(2, 2, 2, 2);
  19. c.anchor = GridBagConstraints.EAST;
  20. cp.add(l = new JLabel("Item Number:", SwingConstants.CENTER), c); l.setDisplayedMnemonic('a');
  21. cp.add(l = new JLabel("Item Name:", SwingConstants.CENTER), c); l.setDisplayedMnemonic('b');
  22. cp.add(l = new JLabel("Number of Units in Stock:", SwingConstants.CENTER), c); l.setDisplayedMnemonic('c');
  23. cp.add(l = new JLabel("Price per Unit: $", SwingConstants.CENTER), c); l.setDisplayedMnemonic('d');
  24. cp.add(l = new JLabel("Total cost of This Item: $", SwingConstants.CENTER), c); l.setDisplayedMnemonic('e');
  25. cp.add(l = new JLabel("Total Value of All Merchandise in Inventory: $",
  26. SwingConstants.CENTER), c); l.setDisplayedMnemonic('f');
  27. cp.add(l = new JLabel("Item Code:", SwingConstants.CENTER), c); l.setDisplayedMnemonic('g');
  28. cp.add(l = new JLabel("Product Restocking Fee: $", SwingConstants.CENTER), c); l.setDisplayedMnemonic('h');
  29. cp.add(combo, c);
  30. c.gridx = 1;
  31. c.gridy = 0;
  32. c.weightx = 1.0;
  33. c.fill = GridBagConstraints.HORIZONTAL;
  34. c.anchor = GridBagConstraints.CENTER;
  35. cp.add(fields[0] = new JTextField(), c);
  36. fields[0].setFocusAccelerator('a');
  37. c.gridx = 1;
  38. c.gridy = GridBagConstraints.RELATIVE;
  39. cp.add(fields[1] = new JTextField(), c); fields[1].setFocusAccelerator('b');
  40. cp.add(fields[2] = new JTextField(), c); fields[2].setFocusAccelerator('c');
  41. cp.add(fields[3] = new JTextField(), c); fields[3].setFocusAccelerator('d');
  42. cp.add(fields[4] = new JTextField(), c); fields[4].setFocusAccelerator('e');
  43. cp.add(fields[5] = new JTextField(), c); fields[5].setFocusAccelerator('f');
  44. cp.add(fields[6] = new JTextField(), c); fields[6].setFocusAccelerator('g');
  45. cp.add(fields[7] = new JTextField(), c); fields[7].setFocusAccelerator('h');
  46. cp.add(fields[8] = new JTextField(), c); fields[8].setFocusAccelerator('i');
  47. c.weightx = 0.0;
  48. c.fill = GridBagConstraints.NONE;
  49. jbtOk = new JButton("OK");
  50. jbtOk.addActionListener(this);
  51. jbtFirst = new JButton("First");
  52. jbtFirst.addActionListener(this);
  53. jbtPrev = new JButton("Previous");
  54. jbtPrev.addActionListener(this);
  55. jbtNext = new JButton("Next");
  56. jbtNext.addActionListener(this);
  57. jbtLast = new JButton("Last");
  58. jbtLast.addActionListener(this);
  59.  
  60. cp.add(jbtOk, c);
  61. cp.add(jbtFirst, c);
  62. cp.add(jbtPrev, c);
  63. cp.add(jbtNext, c);
  64. cp.add(jbtLast, c);
  65. f.pack();
  66. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  67. f.setVisible(true);
  68. // assign actionListener and actionEvent for each button
  69. jbtFirst.addActionListener(new ActionListener()
  70. {
  71. public void actionPerformed(ActionEvent ae)
  72. {
  73. dispProd = 0;
  74.  
  75.  
  76. } // end firstBtn actionEvent
  77.  
  78. }); // end firstBtn actionListener
  79.  
  80.  
  81. jbtPrev.addActionListener(new ActionListener()
  82.  
  83. {
  84. public void actionPerformed(ActionEvent ae)
  85. {
  86. dispProd--;
  87. if (dispProd < 0)
  88. {
  89. dispProd = 0;
  90. }
  91. dispProd = (product.length+dispProd-1) % product.length;
  92.  
  93. } // end prevBtn actionEvent
  94. }); // end prevBtn actionListener
  95.  
  96. jbtLast.addActionListener(new ActionListener()
  97. {
  98. public void actionPerformed(ActionEvent ae)
  99. {
  100. dispProd = product.length - 1;
  101.  
  102.  
  103. } // end lastBtn actionEvent
  104.  
  105. }); // end lastBtn actionListener
  106.  
  107. jbtNext.addActionListener(new ActionListener()
  108.  
  109. {
  110. public void actionPerformed(ActionEvent ae)
  111. {
  112. dispProd++;
  113. if (dispProd >= product.length)
  114. {
  115. dispProd = product.length - 1;
  116. }
  117.  
  118. } // end nextBtn actionEvent
  119. }); // end nextBtn actionListener
  120. }
  121.  
  122. private void populateFields(int index) {
  123. CD cd = product[index];
  124. fields[0].setText(Long.toString(cd.getNumberCode()));
  125. fields[1].setText(cd.getName());
  126. fields[2].setText(Long.toString(cd.getUnits()));
  127. fields[3].setText(Double.toString(cd.getPrice()));
  128. fields[4].setText(Double.toString(cd.getSum()));
  129. fields[5].setText(Double.toString(cd.totalAllInventory(product)));
  130. fields[6].setText(cd.getCode());
  131. fields[7].setText(Double.toString(cd.getSum()*.05));
  132. }
  133. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,200
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 486
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Inventory program part 5 help

 
0
  #15
Sep 12th, 2007
It is still working, you just do not see output on command line don't you? Or you do not know where this is comming from? Why you not getting action from "your" ActionListeners?
Well answer is very simple. You have 2 ActionListeners. One is top level which I created
  1. public void actionPerformed(ActionEvent ae)
  2. {
  3. if(ae.getSource() == jbtOk)
  4. {
  5. // add OK button functionality
  6. System.out.println("OK pressed");
  7. }
  8. if(ae.getSource() == jbtFirst)
  9. {
  10. // add First button functionality
  11. System.out.println("FIRST pressed");
  12. }
  13. if(ae.getSource() == jbtPrev)
  14. {
  15. // add Previous button functionality
  16. System.out.println("PREVIOUS pressed");
  17. }
  18. if(ae.getSource() == jbtNext)
  19. {
  20. // add Next button functionality
  21. System.out.println("NEXT pressed");
  22. }
  23. if(ae.getSource() == jbtLast)
  24. {
  25. // add Last button functionality
  26. System.out.println("LAST pressed");
  27. }
  28. }
and then there are yours anonymous inner class listeners like this one
  1. jbtFirst.addActionListener(new ActionListener()
  2. {
  3. public void actionPerformed(ActionEvent ae)
  4. {
  5. dispProd = 0;
  6.  
  7.  
  8. } // end firstBtn actionEvent
  9.  
  10. }); // end firstBtn actionListener
So all action events by buttons get catch by my event handle as it is top level instead of yours. Simply comment out content of my actionPerformed() method /* */, just content not whole or you get error for JComboBox to which you assign event but not handle anywhere
  1. combo.addActionListener(this);
and do not forget to call a method for updating content of the fields, the one in red
jbtNext.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent ae)
            {
                dispProd++;
                if (dispProd >= product.length)
                {                	
                    dispProd = product.length - 1;
                }
                populateFields(dispProd);
            } // end nextBtn actionEvent
        }); // end nextBtn actionListener

PS: You may complitely remove my event handler once you implement anonymous inner class event handler for that combo box.
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 25
Reputation: dkdeleon68 is an unknown quantity at this point 
Solved Threads: 0
dkdeleon68 dkdeleon68 is offline Offline
Light Poster

Re: Inventory program part 5 help

 
0
  #16
Sep 12th, 2007
the next button needs to navagate to the next item when pushed and the previous button needs to navagate to the previous item and so on. The buttons just print out that they have been pushed and thats it. I am having a hard time understanding.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,200
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 486
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Inventory program part 5 help

 
0
  #17
Sep 12th, 2007
Just delete event handle created by me, create event handler for JComboBox and call following method
  1. populateFields(dispProd);
to update your fields...
Last edited by peter_budo; Sep 12th, 2007 at 7:26 am. Reason: speling mistake
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 25
Reputation: dkdeleon68 is an unknown quantity at this point 
Solved Threads: 0
dkdeleon68 dkdeleon68 is offline Offline
Light Poster

Re: Inventory program part 5 help

 
0
  #18
Sep 12th, 2007
I did what you suggested and now I get these errors:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JButton cannot be cast to javax.swing.JComboBox
at inventoryi.InventoryMain.actionPerformed(InventoryI.java:48)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6038)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
at java.awt.Component.processEvent(Component.java:5803)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)


This is my code that I get errors

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.text.NumberFormat; // used to format currency
  4. import javax.swing.*;
  5.  
  6. import javax.swing.Icon;
  7. import javax.swing.ImageIcon;
  8. import javax.swing.JButton;
  9. import javax.swing.JFrame;
  10. import javax.swing.JLabel;
  11. import javax.swing.JComboBox;
  12. import javax.swing.JPanel;
  13. import javax.swing.JScrollPane;
  14. import javax.swing.JTextArea;
  15.  
  16. class InventoryMain extends Frame implements ActionListener
  17. {
  18. CD[] product;
  19. JTextField[] fields;
  20. NumberFormat nf;
  21. JButton jbtOk, jbtFirst, jbtPrev, jbtNext, jbtLast;
  22. JLabel label; // logo
  23. JPanel buttonJPanel; // panel to hold buttons
  24. String nameText;
  25. private int index;
  26. int i; // varialbe for looping
  27. double total = 0; // variable for total inventory
  28. static int dispProd = 0; // variable for actionEvents
  29. static JTextArea textArea;
  30.  
  31. public void actionPerformed(ActionEvent e)
  32. {
  33. int index = ((JComboBox)e.getSource()).getSelectedIndex();
  34. populateFields(dispProd);
  35. }
  36.  
  37. public static void main(String[] args)
  38. {
  39.  
  40.  
  41. try
  42. {
  43. UIManager.setLookAndFeel(
  44. "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
  45. }
  46. catch (Exception e)
  47. {
  48. System.err.println(e.getClass().getName() + ": " + e.getMessage());
  49. }
  50. InventoryMain test = new InventoryMain();
  51. test.initCD();
  52. test.showGUI();
  53. test.populateFields(0);
  54. }
  55.  
  56.  
  57.  
  58. public void initCD() {
  59. //Create an array
  60. product = new CD[10];
  61.  
  62. //fill in the classes
  63. product[0] = new CD( 1, "Crowbar" , 20.00, 10, "00722");
  64. product[1] = new CD( 2, "Pantera" , 20.00, 10, "00263");
  65. product[2] = new CD( 3, "Ozzy" , 15.00, 10, "00142");
  66. product[3] = new CD( 4, "Soulfly" , 18.00, 10, "00553");
  67. product[4] = new CD( 5, "Down", 24.00, 10, "00789");
  68. product[5] = new CD( 6, "God Forbid" , 10.00, 10, "00712");
  69. product[6] = new CD( 7, "Black Label Society" , 16.00, 10, "00458" );
  70. product[7] = new CD( 8, "Saint Vitus" , 15.00, 10, "00889");
  71. product[8] = new CD( 9, "Clearlight" , 15.00, 10, "00897");
  72. product[9] = new CD( 10, "Testament" , 15.00, 10, "00656");
  73.  
  74. //Sort elements in array in alphabetical order by product name
  75. // product[0].sortItems(product);
  76.  
  77.  
  78. }
  79.  
  80.  
  81.  
  82. private void showGUI() {
  83. JLabel l;
  84. JButton b;
  85. fields = new JTextField[9];
  86. JComboBox combo = new JComboBox();
  87. for(int j = 0; j < product.length; j++)
  88. combo.addItem(product[j].getName());
  89. combo.addActionListener(this);
  90. Icon logo = new ImageIcon("C:/logo.jpg"); // load logo
  91. label = new JLabel(logo); // create logo label
  92. label.setToolTipText("Company Logo"); // create tooltip
  93. textArea = new JTextArea(product[3] + "\n"); // create textArea for
  94. // product display
  95. JFrame f = new JFrame("CD Inventory");
  96. Container cp = f.getContentPane();
  97. cp.setLayout(new GridBagLayout());
  98. cp.setBackground(UIManager.getColor("control"));
  99. GridBagConstraints c = new GridBagConstraints();
  100. c.gridx = 0;
  101. c.gridy = GridBagConstraints.RELATIVE;
  102. c.gridwidth = 1;
  103. c.gridheight = 1;
  104. c.insets = new Insets(2, 2, 2, 2);
  105. c.anchor = GridBagConstraints.EAST;
  106. cp.add(l = new JLabel("Item Number:", SwingConstants.CENTER), c); l.setDisplayedMnemonic('a');
  107. cp.add(l = new JLabel("Item Name:", SwingConstants.CENTER), c); l.setDisplayedMnemonic('b');
  108. cp.add(l = new JLabel("Number of Units in Stock:", SwingConstants.CENTER), c); l.setDisplayedMnemonic('c');
  109. cp.add(l = new JLabel("Price per Unit: $", SwingConstants.CENTER), c); l.setDisplayedMnemonic('d');
  110. cp.add(l = new JLabel("Total cost of This Item: $", SwingConstants.CENTER), c); l.setDisplayedMnemonic('e');
  111. cp.add(l = new JLabel("Total Value of All Merchandise in Inventory: $",
  112. SwingConstants.CENTER), c); l.setDisplayedMnemonic('f');
  113. cp.add(l = new JLabel("Item Code:", SwingConstants.CENTER), c); l.setDisplayedMnemonic('g');
  114. cp.add(l = new JLabel("Product Restocking Fee: $", SwingConstants.CENTER), c); l.setDisplayedMnemonic('h');
  115. cp.add(combo, c);
  116. c.gridx = 1;
  117. c.gridy = 0;
  118. c.weightx = 1.0;
  119. c.fill = GridBagConstraints.HORIZONTAL;
  120. c.anchor = GridBagConstraints.CENTER;
  121. cp.add(fields[0] = new JTextField(), c);
  122. fields[0].setFocusAccelerator('a');
  123. c.gridx = 1;
  124. c.gridy = GridBagConstraints.RELATIVE;
  125. cp.add(fields[1] = new JTextField(), c); fields[1].setFocusAccelerator('b');
  126. cp.add(fields[2] = new JTextField(), c); fields[2].setFocusAccelerator('c');
  127. cp.add(fields[3] = new JTextField(), c); fields[3].setFocusAccelerator('d');
  128. cp.add(fields[4] = new JTextField(), c); fields[4].setFocusAccelerator('e');
  129. cp.add(fields[5] = new JTextField(), c); fields[5].setFocusAccelerator('f');
  130. cp.add(fields[6] = new JTextField(), c); fields[6].setFocusAccelerator('g');
  131. cp.add(fields[7] = new JTextField(), c); fields[7].setFocusAccelerator('h');
  132. cp.add(fields[8] = new JTextField(), c); fields[8].setFocusAccelerator('i');
  133. c.weightx = 0.0;
  134. c.fill = GridBagConstraints.NONE;
  135.  
  136. jbtOk = new JButton("OK");
  137. jbtOk.addActionListener(this);
  138. jbtFirst = new JButton("First");
  139. jbtFirst.addActionListener(this);
  140. jbtPrev = new JButton("Previous");
  141. jbtPrev.addActionListener(this);
  142. jbtNext = new JButton("Next");
  143. jbtNext.addActionListener(this);
  144. jbtLast = new JButton("Last");
  145. jbtLast.addActionListener(this);
  146.  
  147. cp.add(jbtOk, c);
  148. cp.add(jbtFirst, c);
  149. cp.add(jbtPrev, c);
  150. cp.add(jbtNext, c);
  151. cp.add(jbtLast, c);
  152. f.pack();
  153. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  154. f.setVisible(true);
  155.  
  156. // assign actionListener and actionEvent for each button
  157. jbtFirst.addActionListener(new ActionListener()
  158. {
  159. public void actionPerformed(ActionEvent ae)
  160. {
  161. dispProd = 0;
  162.  
  163.  
  164. } // end firstBtn actionEvent
  165.  
  166. }); // end firstBtn actionListener
  167.  
  168.  
  169. jbtPrev.addActionListener(new ActionListener()
  170.  
  171. {
  172. public void actionPerformed(ActionEvent ae)
  173. {
  174. dispProd--;
  175. if (dispProd < 0)
  176. {
  177. dispProd = product.length - 1;
  178. }
  179. dispProd = (product.length+dispProd-1) % product.length;
  180.  
  181. } // end prevBtn actionEvent
  182. }); // end prevBtn actionListener
  183.  
  184. jbtLast.addActionListener(new ActionListener()
  185. {
  186. public void actionPerformed(ActionEvent ae)
  187. {
  188. dispProd = product.length - 1;
  189.  
  190.  
  191. } // end lastBtn actionEvent
  192.  
  193. }); // end lastBtn actionListener
  194.  
  195. jbtNext.addActionListener(new ActionListener()
  196.  
  197. {
  198. public void actionPerformed(ActionEvent ae)
  199. {
  200. dispProd++;
  201. if (dispProd >= product.length - 1)
  202. {
  203. dispProd = 0;
  204. }
  205.  
  206. } // end nextBtn actionEvent
  207. }); // end nextBtn actionListener
  208. }
  209.  
  210. private void populateFields(int index) {
  211. CD cd = product[index];
  212. fields[0].setText(Long.toString(cd.getNumberCode()));
  213. fields[1].setText(cd.getName());
  214. fields[2].setText(Long.toString(cd.getUnits()));
  215. fields[3].setText(Double.toString(cd.getPrice()));
  216. fields[4].setText(Double.toString(cd.getSum()));
  217. fields[5].setText(Double.toString(cd.totalAllInventory(product)));
  218. fields[6].setText(cd.getCode());
  219. fields[7].setText(Double.toString(cd.getSum()*.05));
  220. }



  1. class CD {
  2.  
  3. int itemNumber;
  4. String name;
  5. int units;
  6. double price;
  7. String itemCode;
  8. String command;
  9. public CD(int n, String name, double price, int units, String itemCo) {
  10. itemNumber = n;
  11. this.name = name;
  12. this.price = price;
  13. this.units = units;
  14. itemCode = itemCo;
  15. }
  16.  
  17. public int getNumberCode() { return itemNumber; }
  18. public String getName() { return name; }
  19. public int getUnits() { return units; }
  20. public double getPrice() { return price; }
  21. public double getSum() { return units*price; }
  22. public String getCode() { return itemCode; }
  23. public String getActionnCommand() {return command; }
  24. public double totalAllInventory(CD[] cds) {
  25. double total = 0;
  26. for(int j = 0; j < cds.length; j++)
  27. total += cds[j].getSum();
  28. return total;
  29. }
  30. }
  31.  
  32. public class InventoryI {
  33.  
  34. String productnumber;
  35.  
  36. String name;
  37.  
  38. String Text;
  39.  
  40. int numberofunits;
  41.  
  42. double priceperunit;
  43.  
  44. String itemcode;
  45.  
  46. String command;
  47.  
  48.  
  49. // Create a new instance of Inventory
  50. // main constructor for the class
  51. public InventoryI(String Item_Number, String Item_Name, int Items_in_Stock,
  52. double Item_Price, String Item_Code) {
  53. productnumber = Item_Number;
  54. name = Item_Name;
  55. numberofunits = Items_in_Stock;
  56. priceperunit = Item_Price;
  57. itemcode = Item_Code;
  58. }
  59.  
  60. public void setItemName(String Item_Name) {
  61. // sets the items name
  62.  
  63. name = Item_Name;
  64. }
  65.  
  66. public void setItemCode(String Item_Code) {
  67. // sets the items name
  68.  
  69. itemcode = Item_Code;
  70. }
  71. public void setItemNumber(String Item_Number) { // Sets the Product =Number
  72. // for the item
  73.  
  74. productnumber = Item_Number;
  75. }
  76.  
  77. public void setItemsInStock(int Items_in_Stock) { // sets the =number of
  78. // units in stock
  79.  
  80. numberofunits = Items_in_Stock;
  81. }
  82.  
  83. public void setItemPrice(double Item_Price) { // sets the price of =the
  84. // item
  85. priceperunit = Item_Price;
  86. }
  87.  
  88. public String getItemName() { // returns the Product Name of this item
  89. return name;
  90. }
  91.  
  92. public String getItemCode() { // returns the Product Name of this item
  93. return itemcode;
  94. }
  95.  
  96. public String getItemNumber() { // returns the Product Number of the =item
  97.  
  98. return productnumber;
  99. }
  100.  
  101. public int getItemsInStock() { // returns how many units are in stock
  102. return numberofunits;
  103. }
  104.  
  105. public double getItemPrice() { // returns the price of the item
  106. return priceperunit;
  107. }
  108.  
  109. public double getInventoryIValue() { // returns the total value of =the stock
  110. // for this item
  111. return priceperunit * numberofunits;
  112. }
  113.  
  114. public static double getTotalValueOfAllInventory(InventoryI [] inv) {
  115.  
  116. double tot = 0.0;
  117.  
  118. for(int i = 0; i < inv.length; i++)
  119.  
  120. tot += inv[i].getInventoryIValue();
  121.  
  122. return tot;
  123.  
  124. }
  125.  
  126. public static InventoryI[] sort(InventoryI [] inventory)
  127. {
  128. InventoryI temp[] = new InventoryI[1];
  129.  
  130. //sorting the array using Bubble Sort
  131. for(int j = 0; j < inventory.length - 1; j++)
  132. {
  133.  
  134. for(int k = 0; k < inventory.length - 1; k++)
  135. {
  136.  
  137. if(inventory[k].getItemName().compareToIgnoreCase(inventory[k+1].getItemName()) > 0)
  138. {
  139.  
  140. temp[0] = inventory[k];
  141. inventory[k] = inventory[k+1];
  142. inventory[k+1] = temp[0];
  143.  
  144. }//end if
  145.  
  146. }//end for loop
  147.  
  148. }//end for loop
  149.  
  150.  
  151. return inventory;
  152.  
  153. }
  154.  
  155. public String toString()
  156. {
  157. StringBuffer sb = new StringBuffer();
  158.  
  159.  
  160. sb.append("CD Title: \t").append(name).append("\n");
  161. sb.append("Item Code: \t").append(itemcode).append("\n");
  162. sb.append("Item #: \t").append(productnumber).append("\n");
  163. sb.append("Number in stock:\t").append(numberofunits).append("\n");
  164. sb.append("Price: \t").append(String.format("$%.2f%n", priceperunit));
  165. sb.append("Inventory Value:\t").append(String.format("$%.2f%n", this.getInventoryIValue()));
  166.  
  167. return sb.toString();
  168.  
  169.  
  170. }
  171.  
  172.  
  173. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,200
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 486
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Inventory program part 5 help

 
0
  #19
Sep 12th, 2007
I don't think this is best way to implement event handler for JComboBox
  1. public void actionPerformed(ActionEvent e)
  2. {
  3. int index = ((JComboBox)e.getSource()).getSelectedIndex();
  4. populateFields(dispProd);
  5. }
aslo I moved your declaration of JComboBox from ShowGUI() on the start with other variables only initialization is left there. You OK button doesn't do anything as you did not create anonymous event handler for it, like other buttons, and has no action declared in top level event handler with JComboBox
import java.awt.*;
import java.awt.event.*;
import java.text.NumberFormat; // used to format currency
import javax.swing.*;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
 
class InventoryMain extends Frame implements ActionListener
{
    CD[] product;
    JTextField[] fields;
    NumberFormat nf;
    JButton jbtOk, jbtFirst, jbtPrev, jbtNext, jbtLast;
    JLabel label; // logo   
    JPanel buttonJPanel; // panel to hold buttons
    String nameText;
    JComboBox combo;  // New place to declare combo box
    private int index; 
    int i; // varialbe for looping
    double total = 0; // variable for total inventory   
    static int dispProd = 0; // variable for actionEvents   
    static JTextArea textArea;     
    
    public static void main(String[] args) 
    {
           
       
        try 
        {
            UIManager.setLookAndFeel(
                    "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } 
        catch (Exception e) 
        {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
        }
        InventoryMain test = new InventoryMain();
        test.initCD();
        test.showGUI();
        test.populateFields(0);
    }
  
   
    
    public void initCD() {
        //Create an array    
        product = new CD[10];
     
        //fill in the classes
        product[0] = new CD( 1, "Crowbar" , 20.00, 10, "00722");
        product[1] = new CD( 2, "Pantera" , 20.00, 10, "00263");
        product[2] = new CD( 3, "Ozzy" , 15.00, 10, "00142");
        product[3] = new CD( 4, "Soulfly" , 18.00, 10, "00553");
        product[4] = new CD( 5, "Down", 24.00, 10, "00789");
        product[5] = new CD( 6, "God Forbid" , 10.00, 10, "00712");
        product[6] = new CD( 7, "Black Label Society" , 16.00, 10, "00458" );
        product[7] = new CD( 8, "Saint Vitus" , 15.00, 10, "00889");
        product[8] = new CD( 9, "Clearlight" , 15.00, 10, "00897");
        product[9] = new CD( 10, "Testament" , 15.00, 10, "00656");        
    
      //Sort elements in array in alphabetical order by product name
           // product[0].sortItems(product);
      
    
    }
    
    private void showGUI() {
        JLabel l;
        JButton b;
        fields = new JTextField[9];
        combo = new JComboBox(); // combo box initialization left sa was
        for(int j = 0; j < product.length; j++)
        combo.addItem(product[j].getName());
        combo.addActionListener(this);
          Icon logo = new ImageIcon("C:/logo.jpg"); // load logo
        label = new JLabel(logo); // create logo label
        label.setToolTipText("Company Logo"); // create tooltip
            textArea = new JTextArea(product[3] + "\n"); // create textArea for
                                                        // product display
        JFrame f = new JFrame("CD Inventory");
        Container cp = f.getContentPane();
        cp.setLayout(new GridBagLayout());
        cp.setBackground(UIManager.getColor("control"));
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = GridBagConstraints.RELATIVE;
        c.gridwidth = 1;
        c.gridheight = 1;
        c.insets = new Insets(2, 2, 2, 2);
        c.anchor = GridBagConstraints.EAST;
        cp.add(l = new JLabel("Item Number:", SwingConstants.CENTER), c);   l.setDisplayedMnemonic('a');
        cp.add(l = new JLabel("Item Name:", SwingConstants.CENTER), c);    l.setDisplayedMnemonic('b');
        cp.add(l = new JLabel("Number of Units in Stock:", SwingConstants.CENTER), c);   l.setDisplayedMnemonic('c');
        cp.add(l = new JLabel("Price per Unit: $", SwingConstants.CENTER), c);       l.setDisplayedMnemonic('d');
        cp.add(l = new JLabel("Total cost of This Item: $", SwingConstants.CENTER), c);   l.setDisplayedMnemonic('e');
        cp.add(l = new JLabel("Total Value of All Merchandise in Inventory: $",
                               SwingConstants.CENTER), c);   l.setDisplayedMnemonic('f');
        cp.add(l = new JLabel("Item Code:", SwingConstants.CENTER), c);   l.setDisplayedMnemonic('g');
        cp.add(l = new JLabel("Product Restocking Fee: $", SwingConstants.CENTER), c);  l.setDisplayedMnemonic('h');
        cp.add(combo, c);
        c.gridx = 1;
        c.gridy = 0;
        c.weightx = 1.0;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.CENTER;
        cp.add(fields[0] = new JTextField(), c);
        fields[0].setFocusAccelerator('a');
        c.gridx = 1;
        c.gridy = GridBagConstraints.RELATIVE;
        cp.add(fields[1] = new JTextField(), c);         fields[1].setFocusAccelerator('b');
        cp.add(fields[2] = new JTextField(), c);         fields[2].setFocusAccelerator('c');
        cp.add(fields[3] = new JTextField(), c);         fields[3].setFocusAccelerator('d');
        cp.add(fields[4] = new JTextField(), c);         fields[4].setFocusAccelerator('e');
        cp.add(fields[5] = new JTextField(), c);         fields[5].setFocusAccelerator('f');
        cp.add(fields[6] = new JTextField(), c);         fields[6].setFocusAccelerator('g');
        cp.add(fields[7] = new JTextField(), c);         fields[7].setFocusAccelerator('h');
        cp.add(fields[8] = new JTextField(), c);         fields[8].setFocusAccelerator('i');
        c.weightx = 0.0;
        c.fill = GridBagConstraints.NONE;
        
        jbtOk = new JButton("OK");
        jbtOk.addActionListener(this);
        jbtFirst = new JButton("First");
        jbtFirst.addActionListener(this);
        jbtPrev = new JButton("Previous");
        jbtPrev.addActionListener(this);
        jbtNext = new JButton("Next");
        jbtNext.addActionListener(this);
        jbtLast = new JButton("Last");
        jbtLast.addActionListener(this);
        
        cp.add(jbtOk, c);
        cp.add(jbtFirst, c);
        cp.add(jbtPrev, c);
        cp.add(jbtNext, c);
        cp.add(jbtLast, c);
        f.pack();
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
           
// assign actionListener and actionEvent for each button
        jbtFirst.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                dispProd = 0;
                populateFields(dispProd);  // missing method call for update
            } // end firstBtn actionEvent
 
        }); // end firstBtn actionListener
 
 
        jbtPrev.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent ae)
            {
                dispProd--;
                if (dispProd < 0)
                {
                    dispProd = product.length - 1;
                }
                 //WHAT IS THIS FOR??
                 //dispProd = (product.length+dispProd-1) % product.length;

                 populateFields(dispProd);  // missing method call for update
            } // end prevBtn actionEvent
        }); // end prevBtn actionListener
 
        jbtLast.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                dispProd = product.length - 1;
                populateFields(dispProd);  // missing method call for update
            } // end lastBtn actionEvent
 
        }); // end lastBtn actionListener
 
        jbtNext.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent ae)
            {
                dispProd++;
                if (dispProd >= product.length - 1)
                {
                    dispProd = 0;
                }
                populateFields(dispProd);  // Missing method call for update
            } // end nextBtn actionEvent
        }); // end nextBtn actionListener
    }

    // event handler for combo box
    public void actionPerformed(ActionEvent e)
    {        
        if(e.getSource() == combo)
        {
               //System message, I do not know what you wish to do with combo box
            System.out.println("Selected option: " + combo.getSelectedIndex() );
        }
    }
 
    private void populateFields(int index) {
        CD cd = product[index];
        fields[0].setText(Long.toString(cd.getNumberCode()));
        fields[1].setText(cd.getName());
        fields[2].setText(Long.toString(cd.getUnits()));
        fields[3].setText(Double.toString(cd.getPrice()));
        fields[4].setText(Double.toString(cd.getSum()));
        fields[5].setText(Double.toString(cd.totalAllInventory(product)));
        fields[6].setText(cd.getCode());
        fields[7].setText(Double.toString(cd.getSum()*.05));        
    
    }
}
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 25
Reputation: dkdeleon68 is an unknown quantity at this point 
Solved Threads: 0
dkdeleon68 dkdeleon68 is offline Offline
Light Poster

Re: Inventory program part 5 help

 
0
  #20
Sep 12th, 2007
Thank you very much for all your help.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC