Advertisement
Guest User

Untitled

a guest
May 23rd, 2015
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.63 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.Color;
  3. import java.awt.Component;
  4. import java.awt.Font;
  5. import java.awt.ScrollPane;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.awt.event.FocusEvent;
  9. import java.awt.event.FocusListener;
  10. import java.awt.event.MouseAdapter;
  11. import java.awt.event.MouseEvent;
  12.  
  13. import javax.swing.JButton;
  14. import javax.swing.JFrame;
  15. import javax.swing.JLabel;
  16. import javax.swing.JPanel;
  17. import javax.swing.JScrollPane;
  18. import javax.swing.JTable;
  19. import javax.swing.JTextArea;
  20. import javax.swing.JTextField;
  21. import javax.swing.ListSelectionModel;
  22. import javax.swing.SwingConstants;
  23. import javax.swing.border.LineBorder;
  24. import javax.swing.table.DefaultTableModel;
  25. import javax.swing.table.TableCellRenderer;
  26. import javax.swing.table.TableColumn;
  27.  
  28. /** UI class
  29. *
  30. * User interface to collect data and search.
  31. *
  32. * Uses swing
  33. *
  34. */
  35.  
  36. public class UI extends JFrame {
  37.  
  38. //input panel
  39. private JPanel inputPane;
  40. private JTextField nameTextField;
  41. private JTextField priceTextField;
  42. private JButton enterDataButton;
  43.  
  44. // output panel
  45. private JPanel contentPane;
  46. private JTable searchTable;
  47. private JButton displayButton;
  48.  
  49. // search function
  50. private JTextField searchTextField;
  51. private JButton searchButton;
  52. private JTextField matchTextField;
  53.  
  54. // data collection
  55. private GroceryList gl;
  56.  
  57. /** UI - manages interaction with user
  58. *
  59. * Collects data when button is pressed and stores data in array
  60. * Displays all data collected in text area
  61. * Searches for matching name and displays associated data
  62. */
  63. public UI() {
  64. // Add title to window
  65. super("Grocery List Management System");
  66.  
  67. // Initialize data structure
  68. gl = new GroceryList();
  69.  
  70. setResizable(false);
  71. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  72. setBounds(100, 100, 600, 450);
  73.  
  74. contentPane = new JPanel();
  75. contentPane.setBorder(new LineBorder(new Color(112, 12, 102), 4, true));
  76. setContentPane(contentPane);
  77. contentPane.setLayout(null);
  78.  
  79. // Default font for the user interface
  80. Font defaultFont = new Font("Arial", Font.PLAIN, 12);
  81.  
  82. // Input related fields for add/updating a GrocItem
  83. inputPane = new JPanel();
  84. inputPane.setBorder(new LineBorder(new Color(112, 12, 102), 1, true));
  85. inputPane.setBounds(10, 32, 574, 125);
  86. contentPane.add(inputPane);
  87. inputPane.setLayout(null);
  88.  
  89. // labels for input fields
  90. JLabel lblFoodName = new JLabel("Name");
  91. lblFoodName.setFont(defaultFont);
  92. lblFoodName.setHorizontalAlignment(SwingConstants.RIGHT);
  93. lblFoodName.setBounds(16, 11, 60, 14);
  94. inputPane.add(lblFoodName);
  95.  
  96. JLabel lblPrice = new JLabel("Price");
  97. lblPrice.setFont(defaultFont);
  98. lblPrice.setHorizontalAlignment(SwingConstants.RIGHT);
  99. lblPrice.setBounds(16, 36, 60, 14);
  100. inputPane.add(lblPrice);
  101.  
  102. // input field for name of grocery item
  103. nameTextField = new JTextField();
  104. nameTextField.setFont(defaultFont);
  105. nameTextField.setBounds(80, 8, 220, 20);
  106. inputPane.add(nameTextField);
  107. nameTextField.setColumns(25);
  108.  
  109. // input field for price
  110. priceTextField = new JTextField();
  111. priceTextField.setFont(defaultFont);
  112. priceTextField.setBounds(80, 33, 220, 20);
  113. inputPane.add(priceTextField);
  114. priceTextField.setColumns(15);
  115.  
  116. enterDataButton = new JButton("Add");
  117. // Event trigger for the Add button click
  118. enterDataButton.addMouseListener(new MouseAdapter() {
  119. public void mouseClicked(MouseEvent e) {
  120. addGrocItemToList();
  121. }
  122. });
  123. enterDataButton.setFont(defaultFont);
  124. enterDataButton.setBounds(360, 11, 90, 28);
  125. inputPane.add(enterDataButton);
  126.  
  127. // Search function
  128.  
  129. // label for search field
  130. JLabel lblSearchFoodName = new JLabel("Name");
  131. lblSearchFoodName.setFont(defaultFont);
  132. lblSearchFoodName.setHorizontalAlignment(SwingConstants.RIGHT);
  133. lblSearchFoodName.setBounds(16, 71, 60, 14);
  134. inputPane.add(lblSearchFoodName);
  135.  
  136. // input field for name of grocery item to match
  137. searchTextField = new JTextField();
  138. searchTextField.setFont(defaultFont);
  139. searchTextField.setBounds(80, 71, 220, 20);
  140. inputPane.add(searchTextField);
  141. searchTextField.setColumns(25);
  142. inputPane.add(searchTextField);
  143.  
  144. JLabel lblDisplayPrice = new JLabel("Price");
  145. lblDisplayPrice.setFont(defaultFont);
  146. lblDisplayPrice.setHorizontalAlignment(SwingConstants.RIGHT);
  147. lblDisplayPrice.setBounds(16, 96, 60, 14);
  148. inputPane.add(lblDisplayPrice);
  149.  
  150. // display field for matching data
  151. matchTextField = new JTextField();
  152. matchTextField.setFont(defaultFont);
  153. matchTextField.setBounds(80, 96, 220, 20);
  154. matchTextField.setEditable(false); // display matching data
  155. inputPane.add(matchTextField);
  156. searchTextField.setColumns(25);
  157.  
  158. searchButton = new JButton("Search");
  159. // Event trigger for the Search button click
  160. searchButton.addMouseListener(new MouseAdapter() {
  161. public void mouseClicked(MouseEvent e) {
  162. matchTextField.setText (searchForGrocItem(searchTextField.getText()));
  163. }
  164. });
  165. searchButton.setFont(defaultFont);
  166. searchButton.setBounds(360, 71, 90, 28);
  167. inputPane.add(searchButton);
  168.  
  169. // output
  170. JScrollPane scrollPane = new JScrollPane();
  171. scrollPane.setBounds(10, 208, 574, 203);
  172. contentPane.add(scrollPane);
  173.  
  174. // output table
  175. searchTable = new JTable();
  176. searchTable.setFont(defaultFont);
  177. searchTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  178. // Set the model of the search results table that defines the layout of the data
  179. searchTable.setModel(new DefaultTableModel(new String[] {"Name", "Price"}, 0) {
  180. boolean[] columnEditables = new boolean[] {
  181. false, false
  182. };
  183. public boolean isCellEditable(int row, int column) {
  184. return columnEditables[column];
  185. }
  186. });
  187. searchTable.getTableHeader().setFont(defaultFont);
  188.  
  189. // display grocery list on button click
  190. displayButton = new JButton("Display");
  191. displayButton.addMouseListener(new MouseAdapter() {
  192. // Event trigger for the search button click
  193. public void mouseClicked(MouseEvent e) {
  194. displayList();
  195. }
  196. });
  197. displayButton.setFont(defaultFont);
  198. displayButton.setBounds(370, 168, 90, 28);
  199. contentPane.add(displayButton);
  200. scrollPane.setViewportView(searchTable);
  201.  
  202. setLocationRelativeTo(null);
  203. setVisible(true);
  204. }
  205.  
  206. /** displayList()
  207. *
  208. * Clears display table before retrieving and displaying collection of grocery items
  209. *
  210. */
  211. public void displayList() {
  212. // Clear the search results table
  213. DefaultTableModel model = (DefaultTableModel)searchTable.getModel();
  214. for(int i = model.getRowCount() - 1; i >= 0; i--)
  215. model.removeRow(i);
  216.  
  217. // add grocery items to display table, one by one
  218. GrocItem [] list = gl.getList();
  219. for(int i = 0; i < gl.listSize(); i++) {
  220. model.addRow(new Object[] {
  221. list[i].getName(),
  222. list[i].getPrice()
  223. });
  224. }
  225. }
  226.  
  227. /** addGrocItemToList ()
  228. *
  229. * Collects data from UI, instantiates a new GrocItem and adds it to the grocery list
  230. */
  231. public void addGrocItemToList () {
  232. gl.add( new GrocItem (nameTextField.getText(), Double.parseDouble (priceTextField.getText())));
  233.  
  234. // reset input fields to empty
  235. nameTextField.setText("");
  236. priceTextField.setText("");
  237.  
  238. }
  239.  
  240. /** searchForGrocItem (String n)
  241. *
  242. * @param n Name of grocery item to find
  243. * @return String matching price information
  244. */
  245. public String searchForGrocItem (String name) {
  246. GrocItem gi = gl.searchForGrocItem (name);
  247.  
  248. // display matching price, if matching item found
  249. if (gi==null)
  250. return "Not found";
  251. else
  252. return (Double.toString (gi.getPrice()));
  253. }
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement