GUI buttons Inventory Part 5

Reply

Join Date: Jan 2007
Posts: 7
Reputation: bigbluesky is an unknown quantity at this point 
Solved Threads: 0
bigbluesky bigbluesky is offline Offline
Newbie Poster

GUI buttons Inventory Part 5

 
0
  #1
Aug 19th, 2007
I can only get 2 of the buttons to work - what am I doing wrong?
I have been working on this for 5 days.
I also cannot get my icon to work - do I need more of an address?


  1. import java.util.ArrayList; //For ArrayList
  2.  
  3. public class Product implements Comparable<Product>
  4. {
  5.  
  6. //=======================================Private Variables===============================
  7.  
  8. private String name; //Product name
  9. private String type; //Product type
  10. private int identificationNumber; //Product identification number
  11. private int unitsInStock; //Product number units in stock
  12. private double unitPriceInDollars; //Product price of each per unit in dollars
  13.  
  14.  
  15.  
  16. //=========================================Constructors==================================
  17.  
  18. //Default constructor
  19. public Product()
  20. {
  21. //Do nothing
  22. //NOTE: Strings get set to null, int to 0, and double to 0.0
  23.  
  24. }//End Product default constructor
  25.  
  26.  
  27. //Initialization constructor with type
  28. public Product(
  29. String nameIn, String typeIn, int identificationNumberIn,
  30. int unitsInStockIn, double unitPriceInDollarsIn
  31. )
  32. {
  33. //Use the set methods to enforce bounds checking
  34. setName( nameIn );
  35. setType( typeIn );
  36. setIdentificationNumber( identificationNumberIn );
  37. setUnitsInStock( unitsInStockIn );
  38. setUnitPriceInDollars( unitPriceInDollarsIn );
  39.  
  40. }//End Product initialization constructor
  41.  
  42.  
  43. //Copy Constructor
  44. public Product( Product productIn )
  45. {
  46. //Use the set methods to enforce bounds checking
  47. setName( productIn.getName() );
  48. setType( productIn.getType() );
  49. setIdentificationNumber( productIn.getIdentificationNumber() );
  50. setUnitsInStock( productIn.getUnitsInStock() );
  51. setUnitPriceInDollars( productIn.getUnitPriceInDollars() );
  52.  
  53. }//End copy constructor Product
  54.  
  55.  
  56.  
  57.  
  58.  
  59. //=======================================Public Methods==================================
  60.  
  61.  
  62.  
  63.  
  64.  
  65. //----------------------------------------Set Methods------------------------------------
  66.  
  67.  
  68. //Stores the item name
  69. public void setName( String nameIn )
  70. {
  71. name = nameIn;
  72.  
  73. }//End method setName
  74.  
  75.  
  76. //Stores the Product type
  77. public void setType( String typeIn )
  78. {
  79. type = typeIn;
  80.  
  81. }//End method setType
  82.  
  83.  
  84. //Stores the Product identification number
  85. public void setIdentificationNumber( int identificationNumberIn )
  86. {
  87. // If the value is negative, then store 0
  88. identificationNumber = ( ( identificationNumberIn > 0 ) ? identificationNumberIn : 0 );
  89.  
  90. }//End method setIdentificationNumber
  91.  
  92.  
  93. //Stores the Product number of units in stock
  94. public void setUnitsInStock( int unitsInStockIn )
  95. {
  96. // If the value is negative, then store 0
  97. unitsInStock = ( ( unitsInStockIn > 0 ) ? unitsInStockIn : 0 );
  98.  
  99. }//End methodsetUnitsInStock
  100.  
  101.  
  102. //Stores the unit price in dollars (the price per unit) of each Product
  103. public void setUnitPriceInDollars( double unitPriceInDollarsIn )
  104. {
  105. // If the value is negative, then store 0.0
  106. unitPriceInDollars = ( ( unitPriceInDollarsIn > 0.0 ) ? unitPriceInDollarsIn : 0.0 );
  107.  
  108. }//End method setUnitPriceInDollars
  109.  
  110.  
  111. //A method to overwrite this Product with another
  112. public void set( Product productIn )
  113. {
  114. //Set the Product name--use the set methods to enforce bounds checking
  115. setName( productIn.getName() );
  116.  
  117. //Set the Product type--use the set methods to enforce bounds checking
  118. setType( productIn.getType() );
  119.  
  120. //Set the Product identification number
  121. setIdentificationNumber( productIn.getIdentificationNumber() );
  122.  
  123. //Set the Product number of units in stock
  124. setUnitsInStock( productIn.getUnitsInStock() );
  125.  
  126. //Set the Product unit price in dollars
  127. setUnitPriceInDollars( productIn.getUnitPriceInDollars() );
  128.  
  129. }//End method set
  130.  
  131.  
  132.  
  133.  
  134. //----------------------------------------Get Methods------------------------------------
  135.  
  136. //Returns the Product name
  137. public String getName()
  138. {
  139. return ( name );
  140.  
  141. }//End method getName
  142.  
  143.  
  144. //Returns the Product type
  145. public String getType()
  146. {
  147. return ( type );
  148.  
  149. }//End method getType
  150.  
  151.  
  152. //Returns the Product identification number
  153. public int getIdentificationNumber()
  154. {
  155. return ( identificationNumber );
  156.  
  157. }//End getIdentificationNumber
  158.  
  159.  
  160. //Returns the Product number of units in stock
  161. public int getUnitsInStock()
  162. {
  163. return( unitsInStock );
  164.  
  165. }//End method getUnitsInStock
  166.  
  167.  
  168. //Returns the Product unit price (price of one unit)
  169. public double getUnitPriceInDollars()
  170. {
  171. return( unitPriceInDollars );
  172.  
  173. }//End method getUnitPriceInDollars
  174.  
  175.  
  176.  
  177. //----------------------------------------Calculations-----------------------------------
  178.  
  179. //Calculates the stock value for one (this) Product in dollars
  180. // NOTE: I don't call this a 'get' method because I reserve that name for methods that
  181. // retrieve private variables
  182. public double stockValueInDollars()
  183. {
  184. return ( unitsInStock * unitPriceInDollars );
  185.  
  186. }//End method stockValueInDollars
  187.  
  188.  
  189. //Calculates and returns the total value of an array of Products
  190. public static double totalInventoryValue( Product inventory[] )
  191. {
  192. double inventoryValue = 0.0;
  193.  
  194. for ( Product product : inventory )
  195. {
  196. //Add the total value of this product to the total value of the inventory
  197. inventoryValue += product.stockValueInDollars();
  198.  
  199. }//End enhanced for
  200.  
  201. return ( inventoryValue ); //Return the value of the inventory
  202.  
  203. }//End method totalValue
  204.  
  205.  
  206. //Calculates and returns the total value of an ArrayList of Products
  207. // I just include this for possible future versions that use an ArrayList
  208. // instead of an array
  209. // You can safely leave this out of your program
  210. public static double totalInventoryValue( ArrayList<Product> inventory )
  211. {
  212. double inventoryValue = 0.0;
  213.  
  214. for ( Product product : inventory )
  215. {
  216. //Add the stock value of this Product to the total
  217. inventoryValue += product.stockValueInDollars();
  218.  
  219. }//End enhanced for
  220.  
  221. return ( inventoryValue ); //Return the value of the inventory
  222.  
  223. }//End method totalInventoryValue
  224.  
  225.  
  226.  
  227.  
  228.  
  229. //----------------------------------------Sorting----------------------------------
  230.  
  231. //Overloads the compareTo() method so Products can be compared and
  232. // potentially sorted by name using Arrays.sort
  233. public int compareTo( Product anObject )
  234. {
  235. String s2 = anObject.name.toUpperCase();
  236. String s1 = this.name.toUpperCase();
  237.  
  238. return ( s1.compareTo(s2) );
  239.  
  240. }//End method compareTo
  241.  
  242.  
  243. //Sorts the Product array by name
  244. // This is an example of how to sort an array using a Bubble Sort Algorithm
  245. public static void sortAlphabetical( Product inventory[] )
  246. {
  247. Product temp; //For swapping
  248. int bottom;
  249. int i;
  250.  
  251. //This is called a bubble sort
  252. for(bottom = inventory.length-1;bottom > 0;bottom--)
  253. {
  254. for(i = 0; i < bottom; i++)
  255. {
  256. //If inventory[i]'s name is lexographically greater than
  257. // inventory[i+1]'s name, then swap the two
  258. if ( ( inventory[i].getName() ).compareTo( inventory[i+1].getName() ) > 0 )
  259. {
  260. //Swap inventory[i] with inventory[i+1]
  261. temp = inventory[i];
  262. inventory[i] = inventory[i+1];
  263. inventory[i+1] = temp;
  264.  
  265. }//End if
  266.  
  267. }//End for
  268.  
  269. }//End for
  270.  
  271. }//End method sortAlphabetical
  272.  
  273.  
  274. //-----------------------------------------Display----------------------------------
  275.  
  276. //Returns a formatted String that can be used to print to screen or file
  277. public String toString()
  278. {
  279. //NOTE: The numbers before a decimal point set the field width and the minus
  280. // sign left justifies the output in the field. The numbers after the
  281. // decimal point set the number of digits to display after the decimal point.
  282. String formatString = "%3d %8d %9.2f %9.2f %-8s %-20s";
  283.  
  284. return (
  285. String.format(
  286. formatString, identificationNumber, unitsInStock,
  287. unitPriceInDollars, stockValueInDollars(),type, name
  288. )
  289. );
  290.  
  291. }//End method toString()
  292.  
  293.  
  294. //Produce a nice display for a Product array
  295. public static void listArray( Product inventory[] )
  296. {
  297. String headings;
  298.  
  299. System.out.println( "--------------------------------------------------------------" );
  300.  
  301. //Create the column headings
  302. headings = String.format(
  303. "%-3s %-8s %-20s %8s %9s %9s",
  304. "ID#",
  305. "TYPE",
  306. "NAME",
  307. "AMOUNT",
  308. "PRICE($)",
  309. "TOTAL($)"
  310. );
  311.  
  312. //Create the column headings
  313. headings = String.format(
  314. "%3s %8s %9s %9s %-8s %-20s",
  315. "ID#",
  316. "AMOUNT",
  317. "PRICE($)",
  318. "VALUE($)",
  319. "TYPE",
  320. "NAME"
  321. );
  322.  
  323. //Print the column headings to the screen
  324. System.out.println( headings );
  325.  
  326. System.out.println( "--------------------------------------------------------------" );
  327.  
  328. //Use an enhanced for loop to print the toString() for each Product p in
  329. // the Product array
  330. for( Product p : inventory )
  331. {
  332. System.out.println( p.toString() );
  333.  
  334. }//End for
  335.  
  336. System.out.println( "--------------------------------------------------------------" );
  337.  
  338. //System.out.printf("Total Inventory Value: %39.2f\n", totalInventoryValue( inventory ) );
  339. System.out.printf("Total Inventory Value: %9.2f\n", totalInventoryValue( inventory ) );
  340.  
  341. System.out.println( "--------------------------------------------------------------" );
  342.  
  343. }//End method listArray
  344.  
  345.  
  346. //Returns a formatted String for output purposes
  347. //NOTE: This is my old way of displaying things and can be disregarded
  348. public String display()
  349. {
  350. String formatString = "Identification Number : %d\n";
  351. formatString += "Product Name : %s\n";
  352. formatString += "Product Type : %s\n";
  353. formatString += "Units In Stock : %d\n";
  354. formatString += "Unit Price : $%.2f\n";
  355. formatString += "Stock Value : $%.2f\n\n";
  356.  
  357. return (
  358. String.format(
  359. formatString, identificationNumber, name, unitsInStock,
  360. unitPriceInDollars, stockValueInDollars()
  361. )
  362. );
  363.  
  364. }//End toString()
  365.  
  366.  
  367.  
  368.  
  369. }//End class Product

  1. Criterion
  2. Points
  3.  
  4. The application compiles and runs.
  5. 10
  6.  
  7. The application uses buttons to allow the user to move to the first item in the inventory, the previous item,
  8. the next item, and the last item in the inventory.
  9. 10
  10.  
  11. The application properly allows the user to move to the last item when the first item is displayed and the previous button
  12. is selected, and to move to the first item when the last item is displayed and the next button is selected.
  13. 10
  14.  
  15. The GUI includes a company logo.
  16. 10
  17.  
  18. The source code is readable and well documented.
  19. 10
  20.  
  21. Total
  22. 50
  23.  
  24. */
  25.  
  26. import javax.swing.*;
  27. import java.awt.*;
  28. import java.awt.event.*; //For java.awt.
  29. import java.util.Arrays;
  30.  
  31. public class Inventory5 extends JFrame
  32. {
  33.  
  34. //=======================================Private Variables===============================
  35.  
  36. //The private data
  37. private DVD [] dvd; //The inventory -- an array of DVDs
  38. private int index; //An index for keep track of the DVD to display
  39.  
  40. //Panels
  41. private JPanel logoPanel; //Panel for placing the logo label
  42. private JPanel fieldPanel; //Panel for placing the JTextFields and their JLabels
  43. private JPanel mainPanel; //Panel for placing the main JButtons
  44.  
  45. //Labels
  46. private JLabel logoLabel; //Label for displaying the logo
  47. private JLabel nameLabel; //Label for the product name
  48. private JLabel idLabel; //Label for the product id
  49. private JLabel ratingLabel; //Label for the DVD rating
  50. private JLabel unitsLabel; //Label for the units in stock
  51. private JLabel priceLabel; //Label for the price
  52. private JLabel valueLabel; //Label for the stock value
  53. private JLabel feeLabel; //Label for the restocking fee
  54. private JLabel totalLabel; //Label for the total value of the inventory
  55.  
  56. //Text Fields
  57. private JTextField nameText; //Field for displaying/editing the product name
  58. private JTextField idText; //Field for displaying/editing the product id
  59. private JTextField ratingText; //Field for displaying/editing the DVD rating
  60. private JTextField unitsText; //Field for displaying/editing the units in stock
  61. private JTextField priceText; //Field for displaying/editing the DVD price
  62. private JTextField valueText; //Field for displaying/editing the DVD stock value
  63. private JTextField feeText; //Field for displaying/editing the DVD restocking fee
  64. private JTextField totalText; //Field for displaying/editing the total inventory value
  65.  
  66. //Navigation Buttons
  67. private JButton firstButton; //Button for moving to the first element in the array
  68. private JButton nextButton; //Button for moving to the next element in the array
  69. private JButton previousButton; //Button for moving to the next element in the array
  70. private JButton lastButton; //Button for moving to the first element in the array
  71.  
  72. //Constatnts
  73. private final static int FRAME_GRID_ROWS = 3;
  74. private final static int FRAME_GRID_COLS = 1;
  75.  
  76. private final static int FIELD_PANEL_ROWS = 10;
  77. private final static int FIELD_PANEL_COLS = 2;
  78.  
  79. private final static int MAIN_PANEL_ROWS = 1;
  80. private final static int MAIN_PANEL_COLS = 4;
  81.  
  82.  
  83. private final static String EMPTY_ARRAY_MESSAGE = "Hit ADD to add a new DVD";
  84.  
  85. private final static int FRAME_WIDTH = 350;
  86. private final static int FRAME_LENGTH = 300;
  87. private final static int FRAME_XLOC = 250;
  88. private final static int FRAME_YLOC = 100;
  89.  
  90. //=========================================Constructors==================================
  91.  
  92.  
  93. //Initialization constructor
  94. // Starts the GUI with a DVD array passed by the user, but ensures unique identification numbers
  95. public Inventory5( DVD dvdIn[] )
  96. {
  97. //Pass the frame title to JFrame and set the IconImage as well
  98. //Joe's DVD Inventory
  99. super( "Joe's DVD Inventory" );
  100.  
  101. //Set the input array equal to the private array variable
  102. dvd = dvdIn;
  103.  
  104. //Sort the array
  105. Arrays.sort( dvd );
  106.  
  107. //Always start the display at the first element of the array
  108. index = 0;
  109.  
  110. //Build the GUI
  111. buildGUI();
  112.  
  113. //Give it some values
  114. updateAllTextFields();
  115. }
  116.  
  117. //=================================Set/Update/Other Methods================================
  118.  
  119.  
  120. //A method for updating each of the GUI fields
  121. private void updateAllTextFields()
  122. {
  123. if ( dvd.length > 0 ) //The update the TextField display
  124. {
  125. //Update the product name text field
  126. nameText.setText( dvd[index].getName() );
  127.  
  128. //Update the product id text field
  129. idText.setText( String.format( "%d", dvd[index].getIdentificationNumber() ) );
  130.  
  131. //Update the rating text field
  132. ratingText.setText( dvd[index].getRating() );
  133.  
  134. //Update the units in stock text field
  135. unitsText.setText( String.format( "%d", dvd[index].getUnitsInStock() ) );
  136.  
  137. //Update the price text field
  138. priceText.setText( String.format( "$%.2f" , dvd[index].getUnitPriceInDollars() ));
  139.  
  140. //Update the stock value text field
  141. valueText.setText( String.format( "$%.2f" , dvd[index].stockValueInDollars() ));
  142.  
  143. //Update the restocking fee text field
  144. feeText.setText( String.format( "$%.2f" , dvd[index].restockingFee() ));
  145.  
  146. //Update the total value text field
  147. totalText.setText( String.format( "$%.2f" , Product.totalInventoryValue( dvd ) ));
  148.  
  149. }//End if
  150. else //Put a special message in the fields
  151. {
  152. //Update the product name text field
  153. nameText.setText( EMPTY_ARRAY_MESSAGE );
  154.  
  155. //Update the product id text field
  156. idText.setText( EMPTY_ARRAY_MESSAGE );
  157.  
  158. //Update the rating text field
  159. ratingText.setText( EMPTY_ARRAY_MESSAGE );
  160.  
  161. //Update the units in stock text field
  162. unitsText.setText( EMPTY_ARRAY_MESSAGE );
  163.  
  164. //Update the price text field
  165. priceText.setText( EMPTY_ARRAY_MESSAGE );
  166.  
  167. //Update the stock value text field
  168. valueText.setText( EMPTY_ARRAY_MESSAGE );
  169.  
  170. //Update the restocking fee text field
  171. feeText.setText( EMPTY_ARRAY_MESSAGE );
  172.  
  173. //Update the total value text field
  174. totalText.setText( EMPTY_ARRAY_MESSAGE );
  175.  
  176. }//End else
  177.  
  178. }//End updateAllTextFields
  179.  
  180.  
  181. //Update the display for the calculated fields
  182. private void updateCalculatedFields()
  183. {
  184. //Update the stock value text field
  185. valueText.setText( String.format( "$%.2f" , dvd[index].stockValueInDollars() ));
  186.  
  187. //Update the restocking fee text field
  188. feeText.setText( String.format( "$%.2f" , dvd[index].restockingFee() ));
  189.  
  190. //Update the total value text field
  191. totalText.setText( String.format( "$%.2f" , Product.totalInventoryValue( dvd ) ));
  192.  
  193. }//End updateCalculatedFields
  194.  
  195.  
  196. //Set the appropriate fields editable or uneditable
  197. private void setModifiableTextFieldsEnabled( Boolean state )
  198. {
  199. //The DVD name, ID, rating, units in stock, and price can all be set editable or uneditable
  200. nameText.setEditable( state );
  201. ratingText.setEditable( state );
  202. unitsText.setEditable( state );
  203. priceText.setEditable( state );
  204.  
  205. }//End setModifiableTextFieldsEnabled
  206.  
  207.  
  208.  
  209. //==========================Button and Text Handlers and Methods===========================
  210.  
  211. //-------------------------Button Handler Class and Handling Methods-----------------------
  212.  
  213. //The handler for handling the events for the buttons
  214. private class ButtonHandler implements ActionListener
  215. {
  216. public void actionPerformed(ActionEvent event)
  217. {
  218. if( event.getSource() == firstButton ) //|<<| pressed
  219. {
  220. handleFirstButton();
  221.  
  222. }//End if
  223.  
  224. else if( event.getSource() == previousButton ) //|< | pressed
  225. {
  226. handlePreviousButton();
  227.  
  228. }//End else if
  229. else if( event.getSource() == nextButton ) //| >| pressed
  230. {
  231. handleNextButton();
  232.  
  233. }//End else if
  234. else if( event.getSource() == lastButton ) //|>>| pressed
  235. {
  236. handleLastButton();
  237.  
  238. }//End else if
  239.  
  240. }//End method actionPerformed
  241.  
  242. }//End class ButtonHandler
  243.  
  244.  
  245.  
  246. //Display the first element of the DVD array
  247. private void handleFirstButton()
  248. {
  249. //Set the index to the first element in the array
  250. index = 0;
  251.  
  252. //Update and disable modification
  253. updateAllTextFields();
  254. setModifiableTextFieldsEnabled( false );
  255.  
  256. }//End method handleFirstButton
  257.  
  258.  
  259. //Display the previous element of the DVD array or wrap to the last
  260. private void handlePreviousButton()
  261. {
  262. //Decrement the index
  263. index--;
  264.  
  265. //If index is less than 0, wrap around to the last element of the array
  266. if ( index < 0 )
  267. {
  268. index = dvd.length - 1;
  269.  
  270. }//End if
  271.  
  272. //Update and disable modification
  273. updateAllTextFields();
  274. setModifiableTextFieldsEnabled( false );
  275.  
  276. }//End method handlePreviousButton
  277.  
  278. //Display the previous element of the DVD array or wrap to the last
  279. private void handleNextButton()
  280. {
  281. //Decrement the index
  282. index--;
  283.  
  284. //If index is less than 0, wrap around to the last element of the array
  285. if ( index < 0)
  286. {
  287. index = dvd.length + 1;
  288.  
  289. }//End if
  290.  
  291. //Update and disable modification
  292. updateAllTextFields();
  293. setModifiableTextFieldsEnabled( false );
  294.  
  295. }//End method handleNextButton
  296.  
  297.  
  298. //Display the previous element of the DVD array or wrap to the last
  299. private void handleLastButton()
  300. {
  301. //Decrement the index
  302. index--;
  303.  
  304. //If index is less than 0, wrap around to the last element of the array
  305. if ( index < 0)
  306. {
  307. index = dvd.length - 1;
  308.  
  309. }//End if
  310.  
  311. //Update and disable modification
  312. updateAllTextFields();
  313. setModifiableTextFieldsEnabled( false );
  314.  
  315. }//End method handleLastButton
  316.  
  317. //===================================GUI Building Methods==================================
  318.  
  319. //Build the GUI
  320. private void buildGUI()
  321. {
  322. //Make a grid for the panels
  323. setLayout( new GridLayout( FRAME_GRID_ROWS, FRAME_GRID_COLS ) );
  324.  
  325. //Add the logo
  326. buildLogo();
  327.  
  328. //Add the text fields, their labels, and the SEARCH line
  329. buildFields();
  330.  
  331. //Add the navigation and other buttons
  332. buildMainButtons();
  333.  
  334. //Set some of the frame properties
  335. setSize( 350 , 570 );
  336. setLocation( FRAME_XLOC , FRAME_YLOC );
  337. pack();
  338. setResizable( false );
  339. setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  340. setVisible( true );
  341.  
  342. }//End buildGUI()
  343.  
  344.  
  345. //Add the logo to the JFrame
  346. private void buildLogo()
  347. {
  348. //Create the panel on which to place the logo
  349. logoPanel = new JPanel();
  350. logoPanel.setLayout( new GridLayout( 1, 1 ) );
  351.  
  352. //Create the logo
  353. logoLabel = new JLabel( "travelbug.gif" );
  354.  
  355. //Add the logo to the panel
  356. logoPanel.add( logoLabel );
  357.  
  358. //Add the panel to the frame
  359. add( logoPanel );
  360.  
  361. }//End method buildLogo
  362.  
  363. //Add the text fields, their labels, and the SEARCH line
  364. private void buildFields()
  365. {
  366. //Create the panel on which to place the labels and text fields
  367. fieldPanel = new JPanel();
  368. fieldPanel.setLayout( new GridLayout( FIELD_PANEL_ROWS, FIELD_PANEL_COLS ) );
  369.  
  370. //Declare a handler for the buttons
  371. ButtonHandler buttonHandler = new ButtonHandler();
  372.  
  373. //Create the name label
  374. nameLabel = new JLabel( "Name: " );
  375. nameLabel.setLabelFor( nameText );
  376. fieldPanel.add( nameLabel );
  377.  
  378. //Create the name text field
  379. nameText = new JTextField( "NONE" );
  380. nameText.setEditable( false );
  381. fieldPanel.add( nameText );
  382.  
  383. //Create the id label
  384. idLabel = new JLabel( "ID: " );
  385. idLabel.setLabelFor( idText );
  386. fieldPanel.add( idLabel );
  387.  
  388. //Create the id text field
  389. idText = new JTextField( "0" );
  390. idText.setEditable( false );
  391. fieldPanel.add( idText );
  392.  
  393. //Create the rating label
  394. ratingLabel = new JLabel( "Rating: " );
  395. ratingLabel.setLabelFor( ratingText );
  396. fieldPanel.add( ratingLabel );
  397.  
  398. //Create the rating text field
  399. ratingText = new JTextField( "" );
  400. ratingText.setEditable( false );
  401. fieldPanel.add( ratingText );
  402.  
  403. //Create the units in stock label
  404. unitsLabel = new JLabel( "Units in Stock: " );
  405. unitsLabel.setLabelFor( unitsText );
  406. fieldPanel.add( unitsLabel );
  407.  
  408. //Create the units in stock text field
  409. unitsText = new JTextField( "" );
  410. unitsText.setEditable( false );
  411. fieldPanel.add( unitsText );
  412.  
  413. //Create the unit price label
  414. priceLabel = new JLabel( "Unit Price: " );
  415. priceLabel.setLabelFor( priceText );
  416. fieldPanel.add( priceLabel );
  417.  
  418. //Create the unit price text field
  419. priceText = new JTextField( "" );
  420. priceText.setEditable( false );
  421. fieldPanel.add( priceText );
  422.  
  423. //Create the stock value label
  424. valueLabel = new JLabel( "Unit Stock Value: " );
  425. valueLabel.setLabelFor( valueText );
  426. fieldPanel.add( valueLabel );
  427.  
  428. //Create the stock value text field
  429. valueText = new JTextField( "" );
  430. valueText.setEditable( false );
  431. fieldPanel.add( valueText );
  432.  
  433. //Create the restocking fee label
  434. feeLabel = new JLabel( "Restocking Fee: " );
  435. feeLabel.setLabelFor( feeText );
  436. fieldPanel.add( feeLabel );
  437.  
  438. //Create the restocking fee text field
  439. feeText = new JTextField( "" );
  440. feeText.setEditable( false );
  441. fieldPanel.add( feeText );
  442.  
  443. //Add two labels that create a space between the other fields and the total inventory value field
  444. fieldPanel.add( new JLabel( " " ) );
  445. fieldPanel.add( new JLabel( " " ) );
  446.  
  447. //Create the total inventory value label
  448. totalLabel = new JLabel( "Total Inventory Value: " );
  449. totalLabel.setLabelFor( totalText );
  450. fieldPanel.add( totalLabel );
  451.  
  452. //Create the total inventory value text field
  453. totalText = new JTextField( "" );
  454. totalText.setEditable( false );
  455. fieldPanel.add( totalText );
  456.  
  457. //Add two labels that create a space between the total inventory value field and the search line
  458. fieldPanel.add( new JLabel( " " ) );
  459. fieldPanel.add( new JLabel( " " ) );
  460.  
  461. //Add the panel to the frame
  462. add( fieldPanel );
  463.  
  464. }//End buildFields
  465.  
  466. //Add the main buttons to the frame
  467. private void buildMainButtons()
  468. {
  469.  
  470. //Create the JPanel for the main buttons
  471. mainPanel = new JPanel();
  472. mainPanel.setLayout( new GridLayout( MAIN_PANEL_ROWS, MAIN_PANEL_COLS ) );
  473.  
  474. //Create the FIRST (<<) button
  475. firstButton = new JButton( "<<" );
  476. firstButton.addActionListener( new ButtonHandler() );
  477. mainPanel.add( firstButton );
  478.  
  479. //Create the PREVIOUS (<) button
  480. previousButton = new JButton( "< " );
  481. previousButton.addActionListener( new ButtonHandler() );
  482. mainPanel.add( previousButton );
  483.  
  484. //Create the NEXT (>) button
  485. nextButton = new JButton( ">" );
  486. nextButton.setEnabled( false );
  487. nextButton.addActionListener( new ButtonHandler() );
  488. mainPanel.add( nextButton );
  489.  
  490. //Create the LAST (>>) button
  491. lastButton = new JButton( ">>" );
  492. lastButton.setEnabled( false );
  493. lastButton.addActionListener( new ButtonHandler() );
  494. mainPanel.add( lastButton );
  495.  
  496. //Add the main button panel to the frame
  497. add( mainPanel );
  498.  
  499. }//End method buildMainButtons
  500.  
  501.  
  502.  
  503. //===================================Main Method==================================
  504.  
  505. //Provide a main method for generating the GUI
  506. public static void main( String args[] )
  507. {
  508.  
  509. //Create some sample DVDs
  510. DVD myDVD[] = new DVD[4];
  511.  
  512. myDVD[0] = new DVD( "Matrix, The" , "R" , 1, 8, 9.99 );
  513. myDVD[1] = new DVD( "Princess Bride, The", "PG" , 2, 12, 9.99 );
  514. myDVD[2] = new DVD( "Ghandi", "PG" , 3, 3, 19.99 );
  515. myDVD[3] = new DVD( "Cool Hand Luke", "NR" , 4, 4, 6.49 );
  516.  
  517. Inventory5 gui = new Inventory5( myDVD );
  518. }
  519.  
  520. } //End class Inventory5

  1. public class DVD extends Product
  2. {
  3. //=======================================Private Variables===============================
  4.  
  5. private String rating;
  6.  
  7. //=========================================Constructors==================================
  8.  
  9. //Initialization constructor
  10. public DVD(String nameIn, String ratingIn, int identificationNumberIn, int unitsInStockIn,
  11. double unitPriceInDollarsIn )
  12. {
  13. //Call the super class constructor
  14. //NOTE: The Product type remains the same for all DVD sub-class objects: DVD
  15. super(nameIn, "DVD", identificationNumberIn, unitsInStockIn, unitPriceInDollarsIn);
  16.  
  17. //Call the set method to ensure proper initialization
  18. setRating( ratingIn );
  19.  
  20. }//End DVD constructor
  21.  
  22.  
  23. //Copy constructor
  24. public DVD( DVD anotherDVD )
  25. {
  26. //Call the super class constructor
  27. super(
  28. anotherDVD.getName(), anotherDVD.getType(), anotherDVD.getIdentificationNumber(),
  29. anotherDVD.getUnitsInStock(), anotherDVD.getUnitPriceInDollars()
  30. );
  31.  
  32. //Call the set method to ensure proper initialization
  33. setRating( anotherDVD.getRating() );
  34.  
  35. }//End DVD constructor
  36.  
  37.  
  38. //=======================================Public Methods==================================
  39.  
  40. //----------------------------------------Set Methods------------------------------------
  41.  
  42.  
  43. //Store the DVD rating
  44. public void setRating( String ratingIn )
  45. {
  46. rating = ratingIn;
  47.  
  48. }//End method setRating
  49.  
  50.  
  51. //A method to overwrite this DVD with another
  52. public void set( DVD dvdIn )
  53. {
  54. //Set the item name--use the set methods to enforce bounds checking
  55. super.setName( dvdIn.getName() );
  56.  
  57. //Set the item identification number
  58. super.setIdentificationNumber( dvdIn.getIdentificationNumber() );
  59.  
  60. //Set the number of units in stock
  61. super.setUnitsInStock( dvdIn.getUnitsInStock() );
  62.  
  63. //Set the unit price in dollars
  64. super.setUnitPriceInDollars( dvdIn.getUnitPriceInDollars() );
  65.  
  66. //Set the rating
  67. setRating( dvdIn.getRating() );
  68.  
  69. }//End method set
  70.  
  71.  
  72. //----------------------------------------Get Methods------------------------------------
  73.  
  74. //Return the DVD rating
  75. public String getRating()
  76. {
  77. return ( rating ); //Return the DVD rating
  78.  
  79. }//End method getRating
  80.  
  81.  
  82.  
  83. //----------------------------------------Other Methods----------------------------------
  84.  
  85. //--------------------------------Calculate the stock value--------------------------------
  86.  
  87. //Returns the total value of product in stock for one product, including a 5% mark-up
  88. // fee
  89. public double stockValueInDollars()
  90. {
  91. //Add and return a 5% mark-up fee beyond the product value determined by the
  92. // super class
  93. return ( super.stockValueInDollars()*(1.0 + 0.05) );
  94.  
  95. } // End valueInDollars
  96.  
  97.  
  98. //Returns the restocking fee
  99. //Added 10/01/06
  100. public double restockingFee()
  101. {
  102. return( super.stockValueInDollars()*0.05 );
  103. }
  104.  
  105. //------------------------------------------Display-----------------------------------------
  106.  
  107.  
  108. //Returns a formatted String for output purposes
  109. public String toString()
  110. {
  111. String formatString;
  112. //Create a formatted string
  113. formatString = String.format(
  114. " %-7s %-9.2f",
  115. rating,
  116. restockingFee()
  117. );
  118.  
  119. //Return the string for use in display
  120. return( super.toString() + formatString );
  121.  
  122. }//End toString()
  123.  
  124.  
  125. //Produce a nice display for a Product array
  126. public static void listArray( DVD inventory[] )
  127. {
  128. String headings;
  129.  
  130. System.out.println( "-------------------------------------------------------------------------------" );
  131.  
  132.  
  133. //Create the column headings
  134. headings = String.format(
  135. "%3s %8s %9s %9s %-8s %-20s %-7s %-9s",
  136. "ID#",
  137. "AMOUNT",
  138. "PRICE($)",
  139. "VALUE($)",
  140. "TYPE",
  141. "NAME",
  142. "RATING",
  143. "MARK-UP"
  144. );
  145.  
  146. //Print the column headings to the screen
  147. System.out.println( headings );
  148.  
  149. System.out.println( "-------------------------------------------------------------------------------" );
  150.  
  151. //Use an enhanced for loop to print the toString() for each Product p in
  152. // the Product array
  153. for( DVD p : inventory )
  154. {
  155. System.out.print( p.toString() );
  156.  
  157. }//End for
  158.  
  159. System.out.println( "-------------------------------------------------------------------------------" );
  160.  
  161. System.out.printf("Total Inventory Value: %9.2f\n", totalInventoryValue( inventory ) );
  162.  
  163. System.out.println( "-------------------------------------------------------------------------------" );
  164.  
  165. }//End method listArray
  166.  
  167.  
  168. }//End class DVD



any help wouild be greatly appreciated!

-bigbluesky
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 787
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 109
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: GUI buttons Inventory Part 5

 
0
  #2
Aug 19th, 2007
Wow, that's a lot of code to wade through. Perhaps you could repost with the section that contains the buttons and icon that don't work so it is easier to find?
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 7
Reputation: bigbluesky is an unknown quantity at this point 
Solved Threads: 0
bigbluesky bigbluesky is offline Offline
Newbie Poster

Re: GUI buttons Inventory Part 5

 
0
  #3
Aug 19th, 2007
yes - that is a lot of code. sorry.

I think the problem is in the Inventory5.java file in the button handler part of the code.


  1. Button Handler Class and Handling Methods
  2.  
  3. //The handler for handling the events for the buttons
  4. private class ButtonHandler implements ActionListener
  5. {
  6. public void actionPerformed(ActionEvent event)
  7. {
  8. if( event.getSource() == firstButton ) //|<<| pressed
  9. {
  10. handleFirstButton();
  11.  
  12. }//End if
  13.  
  14. else if( event.getSource() == previousButton ) //|< | pressed
  15. {
  16. handlePreviousButton();
  17.  
  18. }//End else if
  19. else if( event.getSource() == nextButton ) //| >| pressed
  20. {
  21. handleNextButton();
  22.  
  23. }//End else if
  24. else if( event.getSource() == lastButton ) //|>>| pressed
  25. {
  26. handleLastButton();
  27.  
  28. }//End else if
  29.  
  30. }//End method actionPerformed
  31.  
  32. }//End class ButtonHandler


Or perhaps in this part of the code - Or both:

  1. //Display the previous element of the DVD array or wrap to the last
  2. private void handlePreviousButton()
  3. {
  4. //Decrement the index
  5. index--;
  6.  
  7. //If index is less than 0, wrap around to the last element of the array
  8. if ( index < 0 )
  9. {
  10. index = dvd.length - 1;
  11.  
  12. }//End if
  13.  
  14. //Update and disable modification
  15. updateAllTextFields();
  16. setModifiableTextFieldsEnabled( false );
  17.  
  18. }//End method handlePreviousButton
  19.  
  20. //Display the previous element of the DVD array or wrap to the last
  21. private void handleNextButton()
  22. {
  23. //Decrement the index
  24. index--;
  25.  
  26. //If index is less than 0, wrap around to the last element of the array
  27. if ( index < 0)
  28. {
  29. index = dvd.length + 1;
  30.  
  31. }//End if
  32.  
  33. //Update and disable modification
  34. updateAllTextFields();
  35. setModifiableTextFieldsEnabled( false );
  36.  
  37. }//End method handleNextButton
  38.  
  39.  
  40. //Display the previous element of the DVD array or wrap to the last
  41. private void handleLastButton()
  42. {
  43. //Decrement the index
  44. index--;
  45.  
  46. //If index is less than 0, wrap around to the last element of the array
  47. if ( index < 0)
  48. {
  49. index = dvd.length - 1;
  50.  
  51. }//End if
  52.  
  53. //Update and disable modification
  54. updateAllTextFields();
  55. setModifiableTextFieldsEnabled( false );
  56.  
  57. }//End method handleLastButton



the icon that won't work is here:

  1. //Add the logo to the JFrame
  2. private void buildLogo()
  3. {
  4. //Create the panel on which to place the logo
  5. logoPanel = new JPanel();
  6. logoPanel.setLayout( new GridLayout( 1, 1 ) );
  7.  
  8. //Create the logo
  9. logoLabel = new JLabel( "travelbug.gif" );
  10.  
  11. //Add the logo to the panel
  12. logoPanel.add( logoLabel );
  13.  
  14. //Add the panel to the frame
  15. add( logoPanel );
  16.  
  17. }//End method buildLogo


Thank you!

-bigbluesky
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 787
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 109
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: GUI buttons Inventory Part 5

 
0
  #4
Aug 19th, 2007
  1. private void handleNextButton()
  2. {
  3. //Decrement the index
  4. index--;
  5.  
  6. //If index is less than 0, wrap around to the last element of the array
  7. if ( index < 0)
  8. {
  9. index = dvd.length + 1;
  10.  
  11. }//End if
First, I think
  1. index--;
should be
  1. index++;
and the if statement should read
  1. if (index>=dvd.length)
  2. {
  3. index = 0;
  4. }
I think a similar problem is occurring for the
  1. handleLastButton()
method.

For the icon, try an absolute address and see what happens. If that doesn't work, repost and we'll try to think of something else...

Hope this helps,
darkagn
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 7
Reputation: bigbluesky is an unknown quantity at this point 
Solved Threads: 0
bigbluesky bigbluesky is offline Offline
Newbie Poster

Re: GUI buttons Inventory Part 5

 
0
  #5
Aug 20th, 2007
well I tried that - changed things and the address - nothing worked . . .
thanks anyway.


-bigbluesky
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 787
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 109
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: GUI buttons Inventory Part 5

 
0
  #6
Aug 21st, 2007
Does your program compile and the buttons don't do what they're supposed to or does the program not compile at all?
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 7
Reputation: bigbluesky is an unknown quantity at this point 
Solved Threads: 0
bigbluesky bigbluesky is offline Offline
Newbie Poster

Re: GUI buttons Inventory Part 5

 
0
  #7
Aug 26th, 2007
Hey - sorry geting back so late . . . I got it to work - by saving the icon in the directory with my files - that worked and by changing the othe code a bit the buttons all work now - maybe I will post the fix here later for others to see.

Thanks for your help!

-bigbluesky
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: GUI buttons Inventory Part 5

 
0
  #8
Aug 30th, 2007
You should post the fix for others that are having problems, thats what the forum is for.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 16
Reputation: plasmafire is an unknown quantity at this point 
Solved Threads: 0
plasmafire plasmafire is offline Offline
Newbie Poster

Re: GUI buttons Inventory Part 5

 
0
  #9
Sep 4th, 2007
Still posting the fix?
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