Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Font;
- import java.awt.ScrollPane;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.FocusEvent;
- import java.awt.event.FocusListener;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JScrollPane;
- import javax.swing.JTable;
- import javax.swing.JTextArea;
- import javax.swing.JTextField;
- import javax.swing.ListSelectionModel;
- import javax.swing.SwingConstants;
- import javax.swing.border.LineBorder;
- import javax.swing.table.DefaultTableModel;
- import javax.swing.table.TableCellRenderer;
- import javax.swing.table.TableColumn;
- /** UI class
- *
- * User interface to collect data and search.
- *
- * Uses swing
- *
- */
- public class UI extends JFrame {
- //input panel
- private JPanel inputPane;
- private JTextField nameTextField;
- private JTextField priceTextField;
- private JButton enterDataButton;
- // output panel
- private JPanel contentPane;
- private JTable searchTable;
- private JButton displayButton;
- // search function
- private JTextField searchTextField;
- private JButton searchButton;
- private JTextField matchTextField;
- // data collection
- private GroceryList gl;
- /** UI - manages interaction with user
- *
- * Collects data when button is pressed and stores data in array
- * Displays all data collected in text area
- * Searches for matching name and displays associated data
- */
- public UI() {
- // Add title to window
- super("Grocery List Management System");
- // Initialize data structure
- gl = new GroceryList();
- setResizable(false);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 600, 450);
- contentPane = new JPanel();
- contentPane.setBorder(new LineBorder(new Color(112, 12, 102), 4, true));
- setContentPane(contentPane);
- contentPane.setLayout(null);
- // Default font for the user interface
- Font defaultFont = new Font("Arial", Font.PLAIN, 12);
- // Input related fields for add/updating a GrocItem
- inputPane = new JPanel();
- inputPane.setBorder(new LineBorder(new Color(112, 12, 102), 1, true));
- inputPane.setBounds(10, 32, 574, 125);
- contentPane.add(inputPane);
- inputPane.setLayout(null);
- // labels for input fields
- JLabel lblFoodName = new JLabel("Name");
- lblFoodName.setFont(defaultFont);
- lblFoodName.setHorizontalAlignment(SwingConstants.RIGHT);
- lblFoodName.setBounds(16, 11, 60, 14);
- inputPane.add(lblFoodName);
- JLabel lblPrice = new JLabel("Price");
- lblPrice.setFont(defaultFont);
- lblPrice.setHorizontalAlignment(SwingConstants.RIGHT);
- lblPrice.setBounds(16, 36, 60, 14);
- inputPane.add(lblPrice);
- // input field for name of grocery item
- nameTextField = new JTextField();
- nameTextField.setFont(defaultFont);
- nameTextField.setBounds(80, 8, 220, 20);
- inputPane.add(nameTextField);
- nameTextField.setColumns(25);
- // input field for price
- priceTextField = new JTextField();
- priceTextField.setFont(defaultFont);
- priceTextField.setBounds(80, 33, 220, 20);
- inputPane.add(priceTextField);
- priceTextField.setColumns(15);
- enterDataButton = new JButton("Add");
- // Event trigger for the Add button click
- enterDataButton.addMouseListener(new MouseAdapter() {
- public void mouseClicked(MouseEvent e) {
- addGrocItemToList();
- }
- });
- enterDataButton.setFont(defaultFont);
- enterDataButton.setBounds(360, 11, 90, 28);
- inputPane.add(enterDataButton);
- // Search function
- // label for search field
- JLabel lblSearchFoodName = new JLabel("Name");
- lblSearchFoodName.setFont(defaultFont);
- lblSearchFoodName.setHorizontalAlignment(SwingConstants.RIGHT);
- lblSearchFoodName.setBounds(16, 71, 60, 14);
- inputPane.add(lblSearchFoodName);
- // input field for name of grocery item to match
- searchTextField = new JTextField();
- searchTextField.setFont(defaultFont);
- searchTextField.setBounds(80, 71, 220, 20);
- inputPane.add(searchTextField);
- searchTextField.setColumns(25);
- inputPane.add(searchTextField);
- JLabel lblDisplayPrice = new JLabel("Price");
- lblDisplayPrice.setFont(defaultFont);
- lblDisplayPrice.setHorizontalAlignment(SwingConstants.RIGHT);
- lblDisplayPrice.setBounds(16, 96, 60, 14);
- inputPane.add(lblDisplayPrice);
- // display field for matching data
- matchTextField = new JTextField();
- matchTextField.setFont(defaultFont);
- matchTextField.setBounds(80, 96, 220, 20);
- matchTextField.setEditable(false); // display matching data
- inputPane.add(matchTextField);
- searchTextField.setColumns(25);
- searchButton = new JButton("Search");
- // Event trigger for the Search button click
- searchButton.addMouseListener(new MouseAdapter() {
- public void mouseClicked(MouseEvent e) {
- matchTextField.setText (searchForGrocItem(searchTextField.getText()));
- }
- });
- searchButton.setFont(defaultFont);
- searchButton.setBounds(360, 71, 90, 28);
- inputPane.add(searchButton);
- // output
- JScrollPane scrollPane = new JScrollPane();
- scrollPane.setBounds(10, 208, 574, 203);
- contentPane.add(scrollPane);
- // output table
- searchTable = new JTable();
- searchTable.setFont(defaultFont);
- searchTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
- // Set the model of the search results table that defines the layout of the data
- searchTable.setModel(new DefaultTableModel(new String[] {"Name", "Price"}, 0) {
- boolean[] columnEditables = new boolean[] {
- false, false
- };
- public boolean isCellEditable(int row, int column) {
- return columnEditables[column];
- }
- });
- searchTable.getTableHeader().setFont(defaultFont);
- // display grocery list on button click
- displayButton = new JButton("Display");
- displayButton.addMouseListener(new MouseAdapter() {
- // Event trigger for the search button click
- public void mouseClicked(MouseEvent e) {
- displayList();
- }
- });
- displayButton.setFont(defaultFont);
- displayButton.setBounds(370, 168, 90, 28);
- contentPane.add(displayButton);
- scrollPane.setViewportView(searchTable);
- setLocationRelativeTo(null);
- setVisible(true);
- }
- /** displayList()
- *
- * Clears display table before retrieving and displaying collection of grocery items
- *
- */
- public void displayList() {
- // Clear the search results table
- DefaultTableModel model = (DefaultTableModel)searchTable.getModel();
- for(int i = model.getRowCount() - 1; i >= 0; i--)
- model.removeRow(i);
- // add grocery items to display table, one by one
- GrocItem [] list = gl.getList();
- for(int i = 0; i < gl.listSize(); i++) {
- model.addRow(new Object[] {
- list[i].getName(),
- list[i].getPrice()
- });
- }
- }
- /** addGrocItemToList ()
- *
- * Collects data from UI, instantiates a new GrocItem and adds it to the grocery list
- */
- public void addGrocItemToList () {
- gl.add( new GrocItem (nameTextField.getText(), Double.parseDouble (priceTextField.getText())));
- // reset input fields to empty
- nameTextField.setText("");
- priceTextField.setText("");
- }
- /** searchForGrocItem (String n)
- *
- * @param n Name of grocery item to find
- * @return String matching price information
- */
- public String searchForGrocItem (String name) {
- GrocItem gi = gl.searchForGrocItem (name);
- // display matching price, if matching item found
- if (gi==null)
- return "Not found";
- else
- return (Double.toString (gi.getPrice()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement