Inventory program part 5 help

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

Inventory program part 5 help

 
0
  #1
Sep 6th, 2007
I need to
Modify the Inventory Program by adding a button to the GUI that allows the user to move to the first item, the previous item, the next item, and the last item in the inventory. If the first item is displayed and the user clicks on the Previous button, the last item should display. If the last item is displayed and the user clicks on the Next button, the first item
should display.
Add a company logo to the GUI using Java graphics classes.

This is what I have so far.

  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.JPanel;
  12. import javax.swing.JScrollPane;
  13. import javax.swing.JTextArea;
  14.  
  15. class InventoryMain implements ActionListener
  16. {
  17. CD[] product;
  18. JTextField[] fields;
  19. NumberFormat nf;
  20.  
  21. public void actionPerformed(ActionEvent e)
  22. {
  23. int index = ((JComboBox)e.getSource()).getSelectedIndex();
  24. populateFields(index);
  25. }
  26.  
  27. public static void main(String[] args)
  28. {
  29. try
  30. {
  31. UIManager.setLookAndFeel(
  32. "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
  33. }
  34. catch (Exception e)
  35. {
  36. System.err.println(e.getClass().getName() + ": " + e.getMessage());
  37. }
  38. InventoryMain test = new InventoryMain();
  39. test.initCD();
  40. test.showGUI();
  41. test.populateFields(0);
  42. }
  43.  
  44. /**
  45.   *
  46.   */
  47. public void initCD() {
  48. //Create an array
  49. product = new CD[10];
  50.  
  51. //fill in the classes
  52. product[0] = new CD( 1, "Crowbar" , 20.00, 10, "00722");
  53. product[1] = new CD( 2, "Pantera" , 20.00, 10, "00263");
  54. product[2] = new CD( 3, "Ozzy" , 15.00, 10, "00142");
  55. product[3] = new CD( 4, "Soulfly" , 18.00, 10, "00553");
  56. product[4] = new CD( 5, "Down", 24.00, 10, "00789");
  57. product[5] = new CD( 6, "God Forbid" , 10.00, 10, "00712");
  58. product[6] = new CD( 7, "Black Label Society" , 16.00, 10, "00458" );
  59. product[7] = new CD( 8, "Saint Vitus" , 15.00, 10, "00889");
  60. product[8] = new CD( 9, "Clearlight" , 15.00, 10, "00897");
  61. product[9] = new CD( 10, "Testament" , 15.00, 10, "00656");
  62.  
  63. //Sort elements in array in alphabetical order by product name
  64. // product[0].sortItems(product);
  65. final JButton firstBtn = new JButton("First"); // first button
  66. final JButton prevBtn = new JButton("Previous"); // previous button
  67. final JButton nextBtn = new JButton("Next"); // next button
  68. final JButton lastBtn = new JButton("Last"); // last button
  69. final JLabel label; // logo
  70. final JTextArea textArea; // text area for product list
  71. final JPanel buttonJPanel; // panel to hold buttons
  72. //JLabel constructor for logo
  73. Icon logo = new ImageIcon("C:/logo.jpg"); // load logo
  74. label = new JLabel(logo); // create logo label
  75. label.setToolTipText("Company Logo"); // create tooltip
  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. JFrame f = new JFrame("InventoryGUI");
  91. Container cp = f.getContentPane();
  92. cp.setLayout(new GridBagLayout());
  93. cp.setBackground(UIManager.getColor("control"));
  94. GridBagConstraints c = new GridBagConstraints();
  95. c.gridx = 0;
  96. c.gridy = GridBagConstraints.RELATIVE;
  97. c.gridwidth = 1;
  98. c.gridheight = 1;
  99. c.insets = new Insets(2, 2, 2, 2);
  100. c.anchor = GridBagConstraints.EAST;
  101. cp.add(l = new JLabel("Item Number:", SwingConstants.CENTER), c); l.setDisplayedMnemonic('a');
  102. cp.add(l = new JLabel("Item Name:", SwingConstants.CENTER), c); l.setDisplayedMnemonic('b');
  103. cp.add(l = new JLabel("Number of Units in Stock:", SwingConstants.CENTER), c); l.setDisplayedMnemonic('c');
  104. cp.add(l = new JLabel("Price per Unit: $", SwingConstants.CENTER), c); l.setDisplayedMnemonic('d');
  105. cp.add(l = new JLabel("Total cost of This Item: $", SwingConstants.CENTER), c); l.setDisplayedMnemonic('e');
  106. cp.add(l = new JLabel("Total Value of All Merchandise in Inventory: $",
  107. SwingConstants.CENTER), c); l.setDisplayedMnemonic('f');
  108. cp.add(l = new JLabel("Item Code:", SwingConstants.CENTER), c); l.setDisplayedMnemonic('g');
  109. cp.add(l = new JLabel("Product Restocking Fee: $", SwingConstants.CENTER), c); l.setDisplayedMnemonic('h');
  110. cp.add(combo, c);
  111. c.gridx = 1;
  112. c.gridy = 0;
  113. c.weightx = 1.0;
  114. c.fill = GridBagConstraints.HORIZONTAL;
  115. c.anchor = GridBagConstraints.CENTER;
  116. cp.add(fields[0] = new JTextField(), c);
  117. fields[0].setFocusAccelerator('a');
  118. c.gridx = 1;
  119. c.gridy = GridBagConstraints.RELATIVE;
  120. cp.add(fields[1] = new JTextField(), c); fields[1].setFocusAccelerator('b');
  121. cp.add(fields[2] = new JTextField(), c); fields[2].setFocusAccelerator('c');
  122. cp.add(fields[3] = new JTextField(), c); fields[3].setFocusAccelerator('d');
  123. cp.add(fields[4] = new JTextField(), c); fields[4].setFocusAccelerator('e');
  124. cp.add(fields[5] = new JTextField(), c); fields[5].setFocusAccelerator('f');
  125. cp.add(fields[6] = new JTextField(), c); fields[6].setFocusAccelerator('g');
  126. cp.add(fields[7] = new JTextField(), c); fields[7].setFocusAccelerator('h');
  127. cp.add(fields[8] = new JTextField(), c); fields[8].setFocusAccelerator('i');
  128. c.weightx = 0.0;
  129. c.fill = GridBagConstraints.NONE;
  130. cp.add(b = new JButton("OK"), c);
  131. cp.add(b = new JButton("First"),c); // set up panel
  132. cp.add(b = new JButton("Prev"),c);
  133. cp.add(b = new JButton("Next"),c);
  134. cp.add(b = new JButton("Last"),c);
  135. b.setMnemonic('o');
  136. f.pack();
  137. f.addWindowListener(new WindowAdapter()
  138.  
  139. {
  140. public void windowClosing(WindowEvent evt)
  141. {
  142. System.exit(0);
  143. }
  144. });
  145. f.setVisible(true);
  146.  
  147. }
  148.  
  149. private void populateFields(int index) {
  150. CD cd = product[index];
  151. fields[0].setText(Long.toString(cd.getNumberCode()));
  152. fields[1].setText(cd.getName());
  153. fields[2].setText(Long.toString(cd.getUnits()));
  154. fields[3].setText(Double.toString(cd.getPrice()));
  155. fields[4].setText(Double.toString(cd.getSum()));
  156. fields[5].setText(Double.toString(cd.totalAllInventory(product)));
  157. fields[6].setText(cd.getCode());
  158. fields[7].setText(Double.toString(cd.getSum()*.05));
  159. }
  160. }
  161.  
  162. class CD {
  163.  
  164. int itemNumber;
  165. String name;
  166. int units;
  167. double price;
  168. String itemCode;
  169.  
  170. public CD(int n, String name, double price, int units, String itemCo) {
  171. itemNumber = n;
  172. this.name = name;
  173. this.price = price;
  174. this.units = units;
  175. itemCode = itemCo;
  176. }
  177.  
  178. public int getNumberCode() { return itemNumber; }
  179. public String getName() { return name; }
  180. public int getUnits() { return units; }
  181. public double getPrice() { return price; }
  182. public double getSum() { return units*price; }
  183. public String getCode() { return itemCode; }
  184.  
  185. public double totalAllInventory(CD[] cds) {
  186. double total = 0;
  187. for(int j = 0; j < cds.length; j++)
  188. total += cds[j].getSum();
  189. return total;
  190. }
  191. }
  192.  
  193. public class InventoryI {
  194.  
  195. String productnumber;
  196.  
  197. String name;
  198.  
  199. int numberofunits;
  200.  
  201. double priceperunit;
  202.  
  203. String itemcode;
  204.  
  205.  
  206. // Create a new instance of Inventory
  207. // main constructor for the class
  208. public InventoryI(String Item_Number, String Item_Name, int Items_in_Stock,
  209. double Item_Price, String Item_Code) {
  210. productnumber = Item_Number;
  211. name = Item_Name;
  212. numberofunits = Items_in_Stock;
  213. priceperunit = Item_Price;
  214. itemcode = Item_Code;
  215. }
  216.  
  217. public void setItemName(String Item_Name) {
  218. // sets the items name
  219.  
  220. name = Item_Name;
  221. }
  222.  
  223. public void setItemCode(String Item_Code) {
  224. // sets the items name
  225.  
  226. itemcode = Item_Code;
  227. }
  228. public void setItemNumber(String Item_Number) { // Sets the Product =Number
  229. // for the item
  230.  
  231. productnumber = Item_Number;
  232. }
  233.  
  234. public void setItemsInStock(int Items_in_Stock) { // sets the =number of
  235. // units in stock
  236.  
  237. numberofunits = Items_in_Stock;
  238. }
  239.  
  240. public void setItemPrice(double Item_Price) { // sets the price of =the
  241. // item
  242. priceperunit = Item_Price;
  243. }
  244.  
  245. public String getItemName() { // returns the Product Name of this item
  246. return name;
  247. }
  248.  
  249. public String getItemCode() { // returns the Product Name of this item
  250. return itemcode;
  251. }
  252.  
  253. public String getItemNumber() { // returns the Product Number of the =item
  254.  
  255. return productnumber;
  256. }
  257.  
  258. public int getItemsInStock() { // returns how many units are in stock
  259. return numberofunits;
  260. }
  261.  
  262. public double getItemPrice() { // returns the price of the item
  263. return priceperunit;
  264. }
  265.  
  266. public double getInventoryIValue() { // returns the total value of =the stock
  267. // for this item
  268. return priceperunit * numberofunits;
  269. }
  270.  
  271. public static double getTotalValueOfAllInventory(InventoryI [] inv) {
  272.  
  273. double tot = 0.0;
  274.  
  275. for(int i = 0; i < inv.length; i++)
  276.  
  277. tot += inv[i].getInventoryIValue();
  278.  
  279. return tot;
  280.  
  281. }
  282.  
  283. public static InventoryI[] sort(InventoryI [] inventory)
  284. {
  285. InventoryI temp[] = new InventoryI[1];
  286.  
  287. //sorting the array using Bubble Sort
  288. for(int j = 0; j < inventory.length - 1; j++)
  289. {
  290.  
  291. for(int k = 0; k < inventory.length - 1; k++)
  292. {
  293.  
  294. if(inventory[k].getItemName().compareToIgnoreCase(inventory[k+1].getItemName()) > 0)
  295. {
  296.  
  297. temp[0] = inventory[k];
  298. inventory[k] = inventory[k+1];
  299. inventory[k+1] = temp[0];
  300.  
  301. }//end if
  302.  
  303. }//end for loop
  304.  
  305. }//end for loop
  306.  
  307.  
  308. return inventory;
  309. }
  310.  
  311. public String toString()
  312. {
  313. StringBuffer sb = new StringBuffer();
  314.  
  315.  
  316. sb.append("CD Title: \t").append(name).append("\n");
  317. sb.append("Item Code: \t").append(itemcode).append("\n");
  318. sb.append("Item #: \t").append(productnumber).append("\n");
  319. sb.append("Number in stock:\t").append(numberofunits).append("\n");
  320. sb.append("Price: \t").append(String.format("$%.2f%n", priceperunit));
  321. sb.append("Inventory Value:\t").append(String.format("$%.2f%n", this.getInventoryIValue()));
  322.  
  323. return sb.toString();
  324. }
  325.  
  326.  
  327. }
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
  #2
Sep 6th, 2007
I forgot to say that my code compiles and runs showing the buttons but the buttons do nothing.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,346
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: 498
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Inventory program part 5 help

 
0
  #3
Sep 6th, 2007
You actually define the buttons twice, once in the initCD() method and then again in showGUI(), but you do not add any ActionListeners (or Actions, both are valid) to be executed when the buttons are pressed.
How To Use Actions
Last edited by Ezzaral; Sep 6th, 2007 at 5:07 pm. Reason: added link
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
  #4
Sep 6th, 2007
here is the code with changes,

  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.JPanel;
  12. import javax.swing.JScrollPane;
  13. import javax.swing.JTextArea;
  14.  
  15. class InventoryMain extends Frame implements ActionListener
  16. {
  17. CD[] product;
  18. JTextField[] fields;
  19. NumberFormat nf;
  20. JButton b;
  21.  
  22. public void actionPerformed(ActionEvent e)
  23. {
  24. int index = ((JComboBox)e.getSource()).getSelectedIndex();
  25. populateFields(index);
  26. }
  27.  
  28. public static void main(String[] args)
  29. {
  30.  
  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. /**
  47.   *
  48.   */
  49. public void initCD() {
  50. //Create an array
  51. product = new CD[10];
  52.  
  53. //fill in the classes
  54. product[0] = new CD( 1, "Crowbar" , 20.00, 10, "00722");
  55. product[1] = new CD( 2, "Pantera" , 20.00, 10, "00263");
  56. product[2] = new CD( 3, "Ozzy" , 15.00, 10, "00142");
  57. product[3] = new CD( 4, "Soulfly" , 18.00, 10, "00553");
  58. product[4] = new CD( 5, "Down", 24.00, 10, "00789");
  59. product[5] = new CD( 6, "God Forbid" , 10.00, 10, "00712");
  60. product[6] = new CD( 7, "Black Label Society" , 16.00, 10, "00458" );
  61. product[7] = new CD( 8, "Saint Vitus" , 15.00, 10, "00889");
  62. product[8] = new CD( 9, "Clearlight" , 15.00, 10, "00897");
  63. product[9] = new CD( 10, "Testament" , 15.00, 10, "00656");
  64.  
  65. //Sort elements in array in alphabetical order by product name
  66. // product[0].sortItems(product);
  67. b = new JButton("First"); // first button
  68. b.addActionListener(this);
  69. b = new JButton("Previous"); // previous button
  70. b.addActionListener(this);
  71. b = new JButton("Next"); // next button
  72. b.addActionListener(this);
  73. b = new JButton("Last"); // last button
  74.  
  75. }
  76.  
  77. // inner class for button event handling
  78. private class ButtonHandler implements ActionListener
  79. {
  80.  
  81. // handle button event
  82. public void actionPerformed( ActionEvent event )
  83.  
  84. {
  85.  
  86.  
  87. // System.out.println(event.getActionCommand());
  88.  
  89.  
  90. // See which button was pressed
  91. if (event.getActionCommand()== "Next"){
  92.  
  93.  
  94. }
  95.  
  96. else if (event.getActionCommand()== "Prev"){
  97.  
  98.  
  99. }
  100. else if (event.getActionCommand()== "Last"){
  101.  
  102.  
  103. }
  104. else if (event.getActionCommand()== "First"){
  105.  
  106.  
  107. }
  108.  
  109.  
  110. } // end method actionPerformed
  111.  
  112. } // end private inner class ButtonHandler
  113.  
  114. private void showGUI() {
  115. JLabel l;
  116. JButton b;
  117. fields = new JTextField[9];
  118. JComboBox combo = new JComboBox();
  119. for(int j = 0; j < product.length; j++)
  120. combo.addItem(product[j].getName());
  121. combo.addActionListener(this);
  122. JFrame f = new JFrame("InventoryGUI");
  123. Container cp = f.getContentPane();
  124. cp.setLayout(new GridBagLayout());
  125. cp.setBackground(UIManager.getColor("control"));
  126. GridBagConstraints c = new GridBagConstraints();
  127. c.gridx = 0;
  128. c.gridy = GridBagConstraints.RELATIVE;
  129. c.gridwidth = 1;
  130. c.gridheight = 1;
  131. c.insets = new Insets(2, 2, 2, 2);
  132. c.anchor = GridBagConstraints.EAST;
  133. cp.add(l = new JLabel("Item Number:", SwingConstants.CENTER), c); l.setDisplayedMnemonic('a');
  134. cp.add(l = new JLabel("Item Name:", SwingConstants.CENTER), c); l.setDisplayedMnemonic('b');
  135. cp.add(l = new JLabel("Number of Units in Stock:", SwingConstants.CENTER), c); l.setDisplayedMnemonic('c');
  136. cp.add(l = new JLabel("Price per Unit: $", SwingConstants.CENTER), c); l.setDisplayedMnemonic('d');
  137. cp.add(l = new JLabel("Total cost of This Item: $", SwingConstants.CENTER), c); l.setDisplayedMnemonic('e');
  138. cp.add(l = new JLabel("Total Value of All Merchandise in Inventory: $",
  139. SwingConstants.CENTER), c); l.setDisplayedMnemonic('f');
  140. cp.add(l = new JLabel("Item Code:", SwingConstants.CENTER), c); l.setDisplayedMnemonic('g');
  141. cp.add(l = new JLabel("Product Restocking Fee: $", SwingConstants.CENTER), c); l.setDisplayedMnemonic('h');
  142. cp.add(combo, c);
  143. c.gridx = 1;
  144. c.gridy = 0;
  145. c.weightx = 1.0;
  146. c.fill = GridBagConstraints.HORIZONTAL;
  147. c.anchor = GridBagConstraints.CENTER;
  148. cp.add(fields[0] = new JTextField(), c);
  149. fields[0].setFocusAccelerator('a');
  150. c.gridx = 1;
  151. c.gridy = GridBagConstraints.RELATIVE;
  152. cp.add(fields[1] = new JTextField(), c); fields[1].setFocusAccelerator('b');
  153. cp.add(fields[2] = new JTextField(), c); fields[2].setFocusAccelerator('c');
  154. cp.add(fields[3] = new JTextField(), c); fields[3].setFocusAccelerator('d');
  155. cp.add(fields[4] = new JTextField(), c); fields[4].setFocusAccelerator('e');
  156. cp.add(fields[5] = new JTextField(), c); fields[5].setFocusAccelerator('f');
  157. cp.add(fields[6] = new JTextField(), c); fields[6].setFocusAccelerator('g');
  158. cp.add(fields[7] = new JTextField(), c); fields[7].setFocusAccelerator('h');
  159. cp.add(fields[8] = new JTextField(), c); fields[8].setFocusAccelerator('i');
  160. c.weightx = 0.0;
  161. c.fill = GridBagConstraints.NONE;
  162. cp.add(b = new JButton("OK"), c);
  163. cp.add(b = new JButton("First"),c);
  164. cp.add(b = new JButton("Prev"),c);
  165. cp.add(b = new JButton("Next"),c);
  166. cp.add(b = new JButton("Last"),c);
  167. b.setMnemonic('o');
  168. f.pack();
  169. f.addWindowListener(new WindowAdapter()
  170.  
  171. {
  172. public void windowClosing(WindowEvent evt)
  173. {
  174. System.exit(0);
  175. }
  176. });
  177. f.setVisible(true);
  178.  
  179. }
  180.  
  181. private void populateFields(int index) {
  182. CD cd = product[index];
  183. fields[0].setText(Long.toString(cd.getNumberCode()));
  184. fields[1].setText(cd.getName());
  185. fields[2].setText(Long.toString(cd.getUnits()));
  186. fields[3].setText(Double.toString(cd.getPrice()));
  187. fields[4].setText(Double.toString(cd.getSum()));
  188. fields[5].setText(Double.toString(cd.totalAllInventory(product)));
  189. fields[6].setText(cd.getCode());
  190. fields[7].setText(Double.toString(cd.getSum()*.05));
  191. }
  192. }
  193.  
  194. class CD {
  195.  
  196. int itemNumber;
  197. String name;
  198. int units;
  199. double price;
  200. String itemCode;
  201. String command;
  202. public CD(int n, String name, double price, int units, String itemCo) {
  203. itemNumber = n;
  204. this.name = name;
  205. this.price = price;
  206. this.units = units;
  207. itemCode = itemCo;
  208. }
  209.  
  210. public int getNumberCode() { return itemNumber; }
  211. public String getName() { return name; }
  212. public int getUnits() { return units; }
  213. public double getPrice() { return price; }
  214. public double getSum() { return units*price; }
  215. public String getCode() { return itemCode; }
  216. public String getActionnCommand() {return command; }
  217. public double totalAllInventory(CD[] cds) {
  218. double total = 0;
  219. for(int j = 0; j < cds.length; j++)
  220. total += cds[j].getSum();
  221. return total;
  222. }
  223. }
  224.  
  225. public class InventoryI {
  226.  
  227. String productnumber;
  228.  
  229. String name;
  230.  
  231. int numberofunits;
  232.  
  233. double priceperunit;
  234.  
  235. String itemcode;
  236.  
  237. String command;
  238.  
  239.  
  240. // Create a new instance of Inventory
  241. // main constructor for the class
  242. public InventoryI(String Item_Number, String Item_Name, int Items_in_Stock,
  243. double Item_Price, String Item_Code) {
  244. productnumber = Item_Number;
  245. name = Item_Name;
  246. numberofunits = Items_in_Stock;
  247. priceperunit = Item_Price;
  248. itemcode = Item_Code;
  249. }
  250.  
  251. public void setItemName(String Item_Name) {
  252. // sets the items name
  253.  
  254. name = Item_Name;
  255. }
  256.  
  257. public void setItemCode(String Item_Code) {
  258. // sets the items name
  259.  
  260. itemcode = Item_Code;
  261. }
  262. public void setItemNumber(String Item_Number) { // Sets the Product =Number
  263. // for the item
  264.  
  265. productnumber = Item_Number;
  266. }
  267.  
  268. public void setItemsInStock(int Items_in_Stock) { // sets the =number of
  269. // units in stock
  270.  
  271. numberofunits = Items_in_Stock;
  272. }
  273.  
  274. public void setItemPrice(double Item_Price) { // sets the price of =the
  275. // item
  276. priceperunit = Item_Price;
  277. }
  278.  
  279. public String getItemName() { // returns the Product Name of this item
  280. return name;
  281. }
  282.  
  283. public String getItemCode() { // returns the Product Name of this item
  284. return itemcode;
  285. }
  286.  
  287. public String getItemNumber() { // returns the Product Number of the =item
  288.  
  289. return productnumber;
  290. }
  291.  
  292. public int getItemsInStock() { // returns how many units are in stock
  293. return numberofunits;
  294. }
  295.  
  296. public double getItemPrice() { // returns the price of the item
  297. return priceperunit;
  298. }
  299.  
  300. public double getInventoryIValue() { // returns the total value of =the stock
  301. // for this item
  302. return priceperunit * numberofunits;
  303. }
  304.  
  305. public static double getTotalValueOfAllInventory(InventoryI [] inv) {
  306.  
  307. double tot = 0.0;
  308.  
  309. for(int i = 0; i < inv.length; i++)
  310.  
  311. tot += inv[i].getInventoryIValue();
  312.  
  313. return tot;
  314.  
  315. }
  316.  
  317. public static InventoryI[] sort(InventoryI [] inventory)
  318. {
  319. InventoryI temp[] = new InventoryI[1];
  320.  
  321. //sorting the array using Bubble Sort
  322. for(int j = 0; j < inventory.length - 1; j++)
  323. {
  324.  
  325. for(int k = 0; k < inventory.length - 1; k++)
  326. {
  327.  
  328. if(inventory[k].getItemName().compareToIgnoreCase(inventory[k+1].getItemName()) > 0)
  329. {
  330.  
  331. temp[0] = inventory[k];
  332. inventory[k] = inventory[k+1];
  333. inventory[k+1] = temp[0];
  334.  
  335. }//end if
  336.  
  337. }//end for loop
  338.  
  339. }//end for loop
  340.  
  341.  
  342. return inventory;
  343.  
  344. }
  345.  
  346. public String toString()
  347. {
  348. StringBuffer sb = new StringBuffer();
  349.  
  350.  
  351. sb.append("CD Title: \t").append(name).append("\n");
  352. sb.append("Item Code: \t").append(itemcode).append("\n");
  353. sb.append("Item #: \t").append(productnumber).append("\n");
  354. sb.append("Number in stock:\t").append(numberofunits).append("\n");
  355. sb.append("Price: \t").append(String.format("$%.2f%n", priceperunit));
  356. sb.append("Inventory Value:\t").append(String.format("$%.2f%n", this.getInventoryIValue()));
  357.  
  358. return sb.toString();
  359.  
  360.  
  361. }
  362.  
  363.  
  364. }
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,346
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: 498
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Inventory program part 5 help

 
0
  #5
Sep 6th, 2007
  1. b = new JButton("First"); // first button
  2. b.addActionListener(this);
  3. b = new JButton("Previous"); // previous button
  4. b.addActionListener(this);
  5. b = new JButton("Next"); // next button
  6. b.addActionListener(this);
Still does not do anything. You are assigning "b" instances of new buttons (replacing the previous each time I might add), but the buttons that are getting added to the panel are being created in showGUI(). You need to add the action listener to the button that is being added to the panel. Also, your current action listener implementation is picking up the combo box index. Each button will need to have its own action to perform.
Last edited by Ezzaral; Sep 6th, 2007 at 6:18 pm.
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
  #6
Sep 6th, 2007
Ok, I changes it so the action listener is added to the buttons that are being added to the panel annd still nothing. by the way, i want to thank you for all your help and advice, they are much appreciated.


  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.JPanel;
  12. import javax.swing.JScrollPane;
  13. import javax.swing.JTextArea;
  14.  
  15. class InventoryMain extends Frame implements ActionListener
  16. {
  17. CD[] product;
  18. JTextField[] fields;
  19. NumberFormat nf;
  20. JButton b;
  21.  
  22. public void actionPerformed(ActionEvent e)
  23. {
  24. int index = ((JComboBox)e.getSource()).getSelectedIndex();
  25. populateFields(index);
  26. }
  27. // inner class for button event handling
  28. private class ButtonHandler implements ActionListener
  29. {
  30.  
  31. // handle button event
  32. public void actionPerformed( ActionEvent event )
  33.  
  34. {
  35.  
  36.  
  37. System.out.println(event.getActionCommand());
  38.  
  39.  
  40. // See which button was pressed
  41. if (event.getActionCommand()== "Next"){
  42.  
  43.  
  44. }
  45.  
  46. else if (event.getActionCommand()== "Prev"){
  47.  
  48.  
  49. }
  50. else if (event.getActionCommand()== "Last"){
  51.  
  52.  
  53. }
  54. else if (event.getActionCommand()== "First"){
  55.  
  56.  
  57. }
  58.  
  59.  
  60. } // end method actionPerformed
  61.  
  62. } // end private inner class ButtonHandler
  63. public static void main(String[] args)
  64. {
  65.  
  66. try
  67. {
  68. UIManager.setLookAndFeel(
  69. "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
  70. }
  71. catch (Exception e)
  72. {
  73. System.err.println(e.getClass().getName() + ": " + e.getMessage());
  74. }
  75. InventoryMain test = new InventoryMain();
  76. test.initCD();
  77. test.showGUI();
  78. test.populateFields(0);
  79. }
  80.  
  81. /**
  82.   *
  83.   */
  84. public void initCD() {
  85. //Create an array
  86. product = new CD[10];
  87.  
  88. //fill in the classes
  89. product[0] = new CD( 1, "Crowbar" , 20.00, 10, "00722");
  90. product[1] = new CD( 2, "Pantera" , 20.00, 10, "00263");
  91. product[2] = new CD( 3, "Ozzy" , 15.00, 10, "00142");
  92. product[3] = new CD( 4, "Soulfly" , 18.00, 10, "00553");
  93. product[4] = new CD( 5, "Down", 24.00, 10, "00789");
  94. product[5] = new CD( 6, "God Forbid" , 10.00, 10, "00712");
  95. product[6] = new CD( 7, "Black Label Society" , 16.00, 10, "00458" );
  96. product[7] = new CD( 8, "Saint Vitus" , 15.00, 10, "00889");
  97. product[8] = new CD( 9, "Clearlight" , 15.00, 10, "00897");
  98. product[9] = new CD( 10, "Testament" , 15.00, 10, "00656");
  99.  
  100. //Sort elements in array in alphabetical order by product name
  101. // product[0].sortItems(product);
  102. b = new JButton("First"); // first button
  103. b = new JButton("Previous"); // previous button
  104. b = new JButton("Next"); // next button
  105. b = new JButton("Last"); // last button
  106.  
  107.  
  108. }
  109.  
  110.  
  111.  
  112. private void showGUI() {
  113. JLabel l;
  114. JButton b;
  115. fields = new JTextField[9];
  116. JComboBox combo = new JComboBox();
  117. for(int j = 0; j < product.length; j++)
  118. combo.addItem(product[j].getName());
  119.  
  120. JFrame f = new JFrame("InventoryGUI");
  121. Container cp = f.getContentPane();
  122. cp.setLayout(new GridBagLayout());
  123. cp.setBackground(UIManager.getColor("control"));
  124. GridBagConstraints c = new GridBagConstraints();
  125. c.gridx = 0;
  126. c.gridy = GridBagConstraints.RELATIVE;
  127. c.gridwidth = 1;
  128. c.gridheight = 1;
  129. c.insets = new Insets(2, 2, 2, 2);
  130. c.anchor = GridBagConstraints.EAST;
  131. cp.add(l = new JLabel("Item Number:", SwingConstants.CENTER), c); l.setDisplayedMnemonic('a');
  132. cp.add(l = new JLabel("Item Name:", SwingConstants.CENTER), c); l.setDisplayedMnemonic('b');
  133. cp.add(l = new JLabel("Number of Units in Stock:", SwingConstants.CENTER), c); l.setDisplayedMnemonic('c');
  134. cp.add(l = new JLabel("Price per Unit: $", SwingConstants.CENTER), c); l.setDisplayedMnemonic('d');
  135. cp.add(l = new JLabel("Total cost of This Item: $", SwingConstants.CENTER), c); l.setDisplayedMnemonic('e');
  136. cp.add(l = new JLabel("Total Value of All Merchandise in Inventory: $",
  137. SwingConstants.CENTER), c); l.setDisplayedMnemonic('f');
  138. cp.add(l = new JLabel("Item Code:", SwingConstants.CENTER), c); l.setDisplayedMnemonic('g');
  139. cp.add(l = new JLabel("Product Restocking Fee: $", SwingConstants.CENTER), c); l.setDisplayedMnemonic('h');
  140. cp.add(combo, c);
  141. c.gridx = 1;
  142. c.gridy = 0;
  143. c.weightx = 1.0;
  144. c.fill = GridBagConstraints.HORIZONTAL;
  145. c.anchor = GridBagConstraints.CENTER;
  146. cp.add(fields[0] = new JTextField(), c);
  147. fields[0].setFocusAccelerator('a');
  148. c.gridx = 1;
  149. c.gridy = GridBagConstraints.RELATIVE;
  150. cp.add(fields[1] = new JTextField(), c); fields[1].setFocusAccelerator('b');
  151. cp.add(fields[2] = new JTextField(), c); fields[2].setFocusAccelerator('c');
  152. cp.add(fields[3] = new JTextField(), c); fields[3].setFocusAccelerator('d');
  153. cp.add(fields[4] = new JTextField(), c); fields[4].setFocusAccelerator('e');
  154. cp.add(fields[5] = new JTextField(), c); fields[5].setFocusAccelerator('f');
  155. cp.add(fields[6] = new JTextField(), c); fields[6].setFocusAccelerator('g');
  156. cp.add(fields[7] = new JTextField(), c); fields[7].setFocusAccelerator('h');
  157. cp.add(fields[8] = new JTextField(), c); fields[8].setFocusAccelerator('i');
  158. c.weightx = 0.0;
  159. c.fill = GridBagConstraints.NONE;
  160. cp.add(b = new JButton("OK"), c);
  161. b.addActionListener(this);
  162. cp.add(b = new JButton("First"),c);
  163. b.addActionListener(this);
  164. cp.add(b = new JButton("Prev"),c);
  165. b.addActionListener(this);
  166. cp.add(b = new JButton("Next"),c);
  167. b.addActionListener(this);
  168. cp.add(b = new JButton("Last"),c);
  169. b.addActionListener(this);
  170. b.setMnemonic('o');
  171. f.pack();
  172. f.addWindowListener(new WindowAdapter()
  173.  
  174. {
  175. public void windowClosing(WindowEvent evt)
  176. {
  177. System.exit(0);
  178. }
  179. });
  180. f.setVisible(true);
  181.  
  182. }
  183.  
  184. private void populateFields(int index) {
  185. CD cd = product[index];
  186. fields[0].setText(Long.toString(cd.getNumberCode()));
  187. fields[1].setText(cd.getName());
  188. fields[2].setText(Long.toString(cd.getUnits()));
  189. fields[3].setText(Double.toString(cd.getPrice()));
  190. fields[4].setText(Double.toString(cd.getSum()));
  191. fields[5].setText(Double.toString(cd.totalAllInventory(product)));
  192. fields[6].setText(cd.getCode());
  193. fields[7].setText(Double.toString(cd.getSum()*.05));
  194. }
  195. }
  196.  
  197. class CD {
  198.  
  199. int itemNumber;
  200. String name;
  201. int units;
  202. double price;
  203. String itemCode;
  204. String command;
  205. public CD(int n, String name, double price, int units, String itemCo) {
  206. itemNumber = n;
  207. this.name = name;
  208. this.price = price;
  209. this.units = units;
  210. itemCode = itemCo;
  211. }
  212.  
  213. public int getNumberCode() { return itemNumber; }
  214. public String getName() { return name; }
  215. public int getUnits() { return units; }
  216. public double getPrice() { return price; }
  217. public double getSum() { return units*price; }
  218. public String getCode() { return itemCode; }
  219. public String getActionnCommand() {return command; }
  220. public double totalAllInventory(CD[] cds) {
  221. double total = 0;
  222. for(int j = 0; j < cds.length; j++)
  223. total += cds[j].getSum();
  224. return total;
  225. }
  226. }
  227.  
  228. public class InventoryI {
  229.  
  230. String productnumber;
  231.  
  232. String name;
  233.  
  234. int numberofunits;
  235.  
  236. double priceperunit;
  237.  
  238. String itemcode;
  239.  
  240. String command;
  241.  
  242.  
  243. // Create a new instance of Inventory
  244. // main constructor for the class
  245. public InventoryI(String Item_Number, String Item_Name, int Items_in_Stock,
  246. double Item_Price, String Item_Code) {
  247. productnumber = Item_Number;
  248. name = Item_Name;
  249. numberofunits = Items_in_Stock;
  250. priceperunit = Item_Price;
  251. itemcode = Item_Code;
  252. }
  253.  
  254. public void setItemName(String Item_Name) {
  255. // sets the items name
  256.  
  257. name = Item_Name;
  258. }
  259.  
  260. public void setItemCode(String Item_Code) {
  261. // sets the items name
  262.  
  263. itemcode = Item_Code;
  264. }
  265. public void setItemNumber(String Item_Number) { // Sets the Product =Number
  266. // for the item
  267.  
  268. productnumber = Item_Number;
  269. }
  270.  
  271. public void setItemsInStock(int Items_in_Stock) { // sets the =number of
  272. // units in stock
  273.  
  274. numberofunits = Items_in_Stock;
  275. }
  276.  
  277. public void setItemPrice(double Item_Price) { // sets the price of =the
  278. // item
  279. priceperunit = Item_Price;
  280. }
  281.  
  282. public String getItemName() { // returns the Product Name of this item
  283. return name;
  284. }
  285.  
  286. public String getItemCode() { // returns the Product Name of this item
  287. return itemcode;
  288. }
  289.  
  290. public String getItemNumber() { // returns the Product Number of the =item
  291.  
  292. return productnumber;
  293. }
  294.  
  295. public int getItemsInStock() { // returns how many units are in stock
  296. return numberofunits;
  297. }
  298.  
  299. public double getItemPrice() { // returns the price of the item
  300. return priceperunit;
  301. }
  302.  
  303. public double getInventoryIValue() { // returns the total value of =the stock
  304. // for this item
  305. return priceperunit * numberofunits;
  306. }
  307.  
  308. public static double getTotalValueOfAllInventory(InventoryI [] inv) {
  309.  
  310. double tot = 0.0;
  311.  
  312. for(int i = 0; i < inv.length; i++)
  313.  
  314. tot += inv[i].getInventoryIValue();
  315.  
  316. return tot;
  317.  
  318. }
  319.  
  320. public static InventoryI[] sort(InventoryI [] inventory)
  321. {
  322. InventoryI temp[] = new InventoryI[1];
  323.  
  324. //sorting the array using Bubble Sort
  325. for(int j = 0; j < inventory.length - 1; j++)
  326. {
  327.  
  328. for(int k = 0; k < inventory.length - 1; k++)
  329. {
  330.  
  331. if(inventory[k].getItemName().compareToIgnoreCase(inventory[k+1].getItemName()) > 0)
  332. {
  333.  
  334. temp[0] = inventory[k];
  335. inventory[k] = inventory[k+1];
  336. inventory[k+1] = temp[0];
  337.  
  338. }//end if
  339.  
  340. }//end for loop
  341.  
  342. }//end for loop
  343.  
  344.  
  345. return inventory;
  346.  
  347. }
  348.  
  349. public String toString()
  350. {
  351. StringBuffer sb = new StringBuffer();
  352.  
  353.  
  354. sb.append("CD Title: \t").append(name).append("\n");
  355. sb.append("Item Code: \t").append(itemcode).append("\n");
  356. sb.append("Item #: \t").append(productnumber).append("\n");
  357. sb.append("Number in stock:\t").append(numberofunits).append("\n");
  358. sb.append("Price: \t").append(String.format("$%.2f%n", priceperunit));
  359. sb.append("Inventory Value:\t").append(String.format("$%.2f%n", this.getInventoryIValue()));
  360.  
  361. return sb.toString();
  362.  
  363.  
  364. }
  365.  
  366.  
  367. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,114
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: 470
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Inventory program part 5 help

 
0
  #7
Sep 7th, 2007
To be fair this program is in mess you should organize it little, good thing is you willing to work on it.
Bellow are some uptades for you, check the red marked parts.

PS: Can you please next time put each java file is separated code tags? Thanx
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.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;
    
    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);
    }
    
    /* DOESN'T DO ANYTHING
     public void actionPerformed(ActionEvent e)
    {
        int index = ((JComboBox)e.getSource()).getSelectedIndex();
        populateFields(index);
    }
     // inner class for button event handling
   private class ButtonHandler implements ActionListener
   {
      // handle button event
      public void actionPerformed( ActionEvent event )
      {
          System.out.println(event.getActionCommand());
         // See which button was pressed
         if (event.getActionCommand()== "Next"){ 
        }

       else if (event.getActionCommand()== "Prev"){
       
        }
         else if (event.getActionCommand()== "Last"){
         
         }
           else if (event.getActionCommand()== "First"){             

           }
       } // end method actionPerformed

   } // end private inner class ButtonHandler*/
    
    public void actionPerformed(ActionEvent ae)
    {
    	if(ae.getSource() == jbtOk)
    	{
    		// add OK button functionality
    		System.out.println("OK pressed");
    	}
    	if(ae.getSource() == jbtFirst)
    	{
    		// add First button functionality
    		System.out.println("FIRST pressed");
    	}
    	if(ae.getSource() == jbtPrev)
    	{
    		// add Previous button functionality
    		System.out.println("PREVIOUS pressed");
    	}
    	if(ae.getSource() == jbtNext)
    	{
    		// add Next button functionality
    		System.out.println("NEXT pressed");
    	}
    	if(ae.getSource() == jbtLast)
    	{
    		// add Last button functionality
    		System.out.println("LAST pressed");
    	}
    }
    
    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);
   		
       /* WHAT IS THIS FOR ? ? ?
       b = new JButton("First"); // first button      
       b = new JButton("Previous"); // previous button
       b = new JButton("Next"); // next button
       b = new JButton("Last"); // last button */      
    }
    
     
        
    private void showGUI() 
    {
        JLabel l;
        /* ONE BUTTON DOESN'T DO FOR ALL ! ! !
           ALSO THIS IS SECOND DECLARATION OF THE BUTTON, ONE ON THE START
         JButton b;*/
        fields = new JTextField[9];
        JComboBox combo = new JComboBox();
        for(int j = 0; j < product.length; j++)
            combo.addItem(product[j].getName());
       
        JFrame f = new JFrame("InventoryGUI");
        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);
        /* INTERESTING SHORTCUT BUT YOU OVERWRITING YOUR BUTTON b
         	THAT IS ONE OF THE REASONS WHY NOTHING IS WORKING FOR YOU ! ! !
        cp.add(b = new JButton("OK"), c);
        b.addActionListener(this);
        cp.add(b = new JButton("First"),c); 
        b.addActionListener(this);
        cp.add(b = new JButton("Prev"),c);
        b.addActionListener(this);
        cp.add(b = new JButton("Next"),c);
        b.addActionListener(this);
        cp.add(b = new JButton("Last"),c);
        b.addActionListener(this);*/
        
        /* ASIGN MNEMONIC TO WHICH OF THE BUTTONS ? ? ?
         b.setMnemonic('o');*/
        f.pack();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        /* ABOVE IS SIMPLER SOLUTION
         f.addWindowListener(new WindowAdapter()

        {
            public void windowClosing(WindowEvent evt) 
            {
                System.exit(0);
            }
        });*/
        f.setVisible(true);
        
    }
 
    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
  #8
Sep 8th, 2007
Thank you for the help, I was up all night last night and got my buttons to print out that they were pushed but I still can not get them to function so that if the list is at the first item and I push previous button the list will show the last item and so on. I have been trying to get my code to do that for the last 32 hours, I even fell asleep with my fingers on the keyboard and woke up from dreaming about java coding. lol lol. if you can point me in the right direction it would be much appreciated, dont get me wrong I'm not asking you to do it for me I'm just asking for a little help.
once again I want to thank you for your help.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 376
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Inventory program part 5 help

 
0
  #9
Sep 8th, 2007
It looks to me like you have written the whole thing in one go and now you're trying to back pedal.

What you should have done is to hit compile and run and check at each stage if it's correct!
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,346
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: 498
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Inventory program part 5 help

 
0
  #10
Sep 10th, 2007
If you keep a pointer to the current recordint currentIndex; then moving within your array is trivial. You just increment the index as needed and populate the fields based on that index.
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