Searching an array...where to begin?

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

Join Date: May 2008
Posts: 2
Reputation: mstarks67 is an unknown quantity at this point 
Solved Threads: 0
mstarks67 mstarks67 is offline Offline
Newbie Poster

Searching an array...where to begin?

 
0
  #1
May 8th, 2008
I have to somehow add a search function to my code. I have figured out how to add the search button and the text box next to it. The action event for the search button is currently the same as the event for the Next button. I need to change the action to that of a search function for the DVD array that will search for a title in the array and display the results. The JButton is "search", the editable text field is "Search" How do I tie the button action with the text field input with the result being displayed? Below is the code for my GUI.

  1. class ButtonFrame extends JFrame
  2. {
  3. private JButton nextJButton; // button with icons
  4. private JButton prevJButton; // button with icons
  5. private JButton lastJButton; // button with icons
  6. private JButton firstJButton; // button with icons
  7. private JButton addJButton; // button with just text
  8. private JButton deleteJButton; // button with just text
  9. private JButton modifyJButton; // button with just text
  10. private JButton saveJButton; // button with just text
  11. private JLabel logoLabel;
  12. private JButton searchJButton; //Feeble attempt at a search function
  13. //private JTextField space1;
  14. //private JTextField lblSearch; // text field with set size
  15. private JTextField txtSearch; // text field constructed with text
  16. private JTextField lblGenre; // text field with set size
  17. private JTextField txtGenre; // text field constructed with text
  18. private JTextField lblItemNum; // text field with set size
  19. private JTextField txtItemNum; // text field constructed with text
  20. private JTextField lblTitle; // text field with set size
  21. private JTextField txtTitle; // text field constructed with text
  22. private JTextField lblQuantity; // text field with set size
  23. private JTextField txtQuantity; // text field constructed with text
  24. private JTextField lblUnitPrice; // text field with set size
  25. private JTextField txtUnitPrice; // text field constructed with text
  26. private JTextField lblRestockFee; // text field with set size
  27. private JTextField txtRestockFee; // text field constructed with text
  28. private JTextField lblItemValue; // text field with set size
  29. private JTextField txtItemValue; // text field constructed with text
  30. private JTextField lblInventoryValue; // text field with set size
  31. private JTextField txtInventoryValue; // text field constructed with text
  32.  
  33.  
  34. // Just keeping an a class variable to keep count of where I am in the array
  35. // keeping a class variable of the myPlayer array that I passed
  36. Genre[] arrayDVDs;
  37. private int currentArrayCounter;
  38. private int arrayCount;
  39. private String invTotal;
  40.  
  41.  
  42. // ButtonFrame adds JButtons to JFrame
  43. public ButtonFrame(Genre[] inventory, int totalArrayCount, String stringInventoryTotal)
  44. {
  45.  
  46. super( "Movie Inventory" );
  47. arrayDVDs = inventory;
  48. invTotal=stringInventoryTotal;
  49.  
  50. // setting the local passed variable totalArrayCount
  51. // to the class variable arrayCounter so I can see it in the setTextfields method
  52. arrayCount = totalArrayCount;
  53. currentArrayCounter = 0;
  54. // Setting the current array position to 0
  55.  
  56.  
  57. setLayout( new FlowLayout() ); // set frame layout
  58. // Load the next and previous icons
  59. Icon logo = new ImageIcon( getClass().getResource( "logo.jpg" ) );
  60. Icon iconNext = new ImageIcon( getClass().getResource( "forward.gif" ) );
  61. Icon iconPrev = new ImageIcon( getClass().getResource( "back.gif" ) );
  62. Icon iconLast = new ImageIcon( getClass().getResource( "last.gif" ) );
  63. Icon iconFirst = new ImageIcon( getClass().getResource( "first.gif" ) );
  64. logoLabel = new JLabel("",logo,SwingConstants.LEFT);
  65. add(logoLabel);
  66.  
  67. // construct Label Fields with default text and 15 columns
  68. searchJButton = new JButton( "Search" );
  69. //space1.setEditable( false ); // disable editing
  70. add( searchJButton );
  71. txtSearch = new JTextField( "", 15 );
  72. txtSearch.setEditable( true ); // disable editing
  73. add( txtSearch );
  74. lblItemNum = new JTextField( "Item Number", 15 );
  75. lblItemNum.setEditable( false ); // disable editing
  76. add( lblItemNum );
  77. txtItemNum = new JTextField("", 15 );
  78. add( txtItemNum ); // add txtItemNum to JFrame
  79. lblGenre = new JTextField( "Genre", 15 );
  80. lblGenre.setEditable( false ); // disable editing
  81. add( lblGenre );
  82. txtGenre = new JTextField("", 15 );
  83. add( txtGenre ); // add txtGenre to JFrame
  84. lblTitle = new JTextField( "Title", 15 );
  85. lblTitle.setEditable( false ); // disable editing
  86. add( lblTitle );
  87. txtTitle = new JTextField("", 15 );
  88. add( txtTitle ); // add txtTitle to JFrame
  89. lblQuantity = new JTextField( "Quantity", 15 );
  90. lblQuantity.setEditable( false ); // disable editing
  91. add( lblQuantity );
  92. txtQuantity = new JTextField("", 15 );
  93. add( txtQuantity ); // add txtQuantity to JFrame
  94. lblUnitPrice = new JTextField( "Unit Price", 15 );
  95. lblUnitPrice.setEditable( false ); // disable editing
  96. add( lblUnitPrice );
  97. txtUnitPrice = new JTextField("", 15 );
  98. add( txtUnitPrice ); // add txtUnitPrice to JFrame
  99. lblRestockFee = new JTextField( "Restocking Fee", 15 );
  100. lblRestockFee.setEditable( false ); // disable editing
  101. add( lblRestockFee );
  102. txtRestockFee = new JTextField("", 15 );
  103. add( txtRestockFee ); // add txtRestockFee to JFrame
  104. lblItemValue = new JTextField( "Total Item Value", 15 );
  105. lblItemValue.setEditable( false ); // disable editing
  106. add( lblItemValue );
  107. txtItemValue = new JTextField("", 15 );
  108. add( txtItemValue ); // add txtItemValue to JFrame
  109. lblInventoryValue = new JTextField( "Total Inventory Value", 15 );
  110. lblInventoryValue.setEditable( false ); // disable editing
  111. add( lblInventoryValue );
  112. txtInventoryValue = new JTextField(invTotal, 15 );
  113. add( txtInventoryValue ); // add txtInventoryValue to JFrame
  114. // construct textfield with default text
  115.  
  116. // Create the buttons
  117. nextJButton = new JButton( "Next", iconNext ); // button with Next
  118. prevJButton = new JButton( "Prev" , iconPrev ); // button with Next
  119. lastJButton = new JButton( "Last", iconLast ); // button with Next
  120. firstJButton = new JButton( "First" , iconFirst ); // button with Next
  121. addJButton = new JButton( "Add" ); //button with add function
  122. deleteJButton = new JButton( "Delete" ); //button with delete
  123. modifyJButton = new JButton( "Modify" ); //button with modify
  124. saveJButton = new JButton( "Save" ); //button with save
  125. add(firstJButton ); // add iconJButton to JFrame
  126. add(prevJButton); // add iconJButton to JFrame
  127. add(nextJButton ); // add plainJButton to JFrame
  128. add(lastJButton); // add iconJButton to JFrame
  129. add(addJButton); // add textJButton to JFrame
  130. add(deleteJButton); // add textJButton to JFrame
  131. add(modifyJButton); // add textJButton to JFrame
  132. add(saveJButton); // add textJButton to JFrame
  133.  
  134. // create new ButtonHandler for button event handling
  135. ButtonHandler handler = new ButtonHandler();
  136. searchJButton.addActionListener( handler );
  137. firstJButton.addActionListener( handler );
  138. prevJButton.addActionListener( handler );
  139. nextJButton.addActionListener( handler );
  140. lastJButton.addActionListener( handler );
  141. addJButton.addActionListener( handler );
  142. deleteJButton.addActionListener( handler );
  143. modifyJButton.addActionListener( handler );
  144. saveJButton.addActionListener( handler );
  145.  
  146.  
  147. // Now I am going to call SetTextFields to set the text fields
  148. setTextFields();
  149.  
  150. } // end ButtonFrame constructor
  151.  
  152.  
  153. // inner class for button event handling
  154. private class ButtonHandler implements ActionListener
  155. {
  156. // handle button event
  157. public void actionPerformed( ActionEvent event )
  158. {
  159.  
  160. //System.out.println(event.getActionCommand());
  161.  
  162. // See which button was pressed
  163.  
  164. if (event.getActionCommand()== "Search"){
  165. currentArrayCounter++;
  166. }
  167. else if (event.getActionCommand()== "Next"){
  168. currentArrayCounter++;
  169. }
  170. else if (event.getActionCommand()== "Prev"){
  171. currentArrayCounter--;
  172. }
  173. else if (event.getActionCommand()== "Last"){
  174. currentArrayCounter = arrayCount-1;
  175. }
  176. else if (event.getActionCommand()== "First"){
  177. currentArrayCounter = 0;
  178. }
  179. else if (event.getActionCommand()== "Add"){ //Adds 1 to quantity on hand
  180. arrayDVDs[currentArrayCounter].addOneToQuantity();
  181. }
  182. else if (event.getActionCommand()== "Delete"){ //Deletes 1 from quantity on hand
  183. arrayDVDs[currentArrayCounter].removeOneFromQuantity();
  184. }
  185. else if (event.getActionCommand()== "Save") //Writes data to C:/data/inventory.dat
  186. {
  187. FileOutputStream out;
  188. PrintStream p;
  189. try
  190. {
  191. String strDirectory = "c:/data";
  192. new File(strDirectory).mkdir();
  193. out = new FileOutputStream ("C:/data/inventory.dat");
  194. p = new PrintStream(out);
  195. for (int i = 0; i<= 5 - 1; i++)
  196. {
  197. p.println("DVD Genre: " + arrayDVDs[i].getgenre()+"\n");
  198. p.println("DVD Title: " + arrayDVDs[i].gettitle()+"\n");
  199. p.println("Item Number: " + arrayDVDs[i].getitemnumber()+"\n");
  200. p.println("Stock Level: " + arrayDVDs[i].getquantity()+"\n");
  201. p.println("DVD Price: $" + arrayDVDs[i].getunitprice()+"\n");
  202. p.println("");
  203. }
  204. p.close();
  205. }
  206. catch (Exception e)
  207. {
  208. System.out.println("error");
  209. }
  210. }
  211.  
  212. setTextFields();
  213.  
  214. } // end method actionPerformed
  215. } // end private inner class ButtonHandler
  216.  
  217.  
  218.  
  219. private void setTextFields ()
  220. {
  221. // Make sure you havent gone past the end of the array
  222. if (currentArrayCounter == arrayCount)
  223. {
  224. currentArrayCounter = 0;
  225. }
  226.  
  227. // Make sure you havent gone past the first if so, set it to the last
  228. if (currentArrayCounter < 0)
  229. {
  230. currentArrayCounter = arrayCount-1;
  231. }
  232.  
  233.  
  234. NumberFormat n = NumberFormat.getCurrencyInstance(Locale.US); //Provides Locale appropriate currency format
  235.  
  236. txtGenre.setText(arrayDVDs[currentArrayCounter].getgenre());
  237. txtItemNum.setText(arrayDVDs[currentArrayCounter].getitemnumber());
  238. txtTitle.setText(arrayDVDs[currentArrayCounter].gettitle());
  239. txtQuantity.setText(Double.toString(arrayDVDs[currentArrayCounter].getquantity()));
  240. txtUnitPrice.setText(n.format(arrayDVDs[currentArrayCounter].getunitprice()));
  241. String stringRestockFee = n.format(arrayDVDs[currentArrayCounter].CalculateRestockFee());
  242. txtRestockFee.setText(stringRestockFee);
  243. txtItemValue.setText(n.format(arrayDVDs[currentArrayCounter].calculateDVDValue()));
  244.  
  245.  
  246. }
  247.  
  248.  
  249. } // end class ButtonFrame
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