Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- import java.util.LinkedList;
- public class SouthbankLibrary extends JFrame {
- // Method: main
- // Opens the GUI. That's about it...
- public static void main(String[] args) {
- // Create new instance of the GUI Object.
- SouthbankLibrary mainFrame = new SouthbankLibrary();
- mainFrame.setVisible(true);
- mainFrame.setResizable(false);
- }
- //------------------------------------------------------------------------------------------------------------------
- // Constructor of the GUI Object
- public SouthbankLibrary() {
- // Set the frame characteristics
- setTitle( "Macrosoft Staff" );
- setSize(1000, 800);
- // Create a JPanel to hold all the GUI components.
- final JPanel mainPanel = new JPanel();
- mainPanel.setLayout( new FlowLayout(FlowLayout.CENTER) );
- getContentPane().add(mainPanel);
- // Fill the JPanel with the main menu components.
- buildMenuGUI(mainPanel);
- }
- //------------------------------------------------------------------------------------------------------------------
- // Populate the main menu with buttons.
- // Called in the GUI Constructor, and when back to menu button is pressed.
- public static void buildMenuGUI(final JPanel mainPanel) {
- // Clear the mainPanel
- mainPanel.removeAll();
- // Set up buttons, with their dimensions, to fill the whole window.
- JButton listStaffMenuButton = new JButton("LIST ALL STAFF MEMBERS");
- listStaffMenuButton.setPreferredSize(new Dimension(1000, 250));
- JButton searchStaffMenuButton = new JButton("SEARCH STAFF MEMBERS");
- searchStaffMenuButton.setPreferredSize(new Dimension(1000, 250));
- JButton addStaffMenuButton = new JButton("ADD STAFF MEMBER");
- addStaffMenuButton.setPreferredSize(new Dimension(1000, 250));
- // Add them to the GUI.
- mainPanel.add(listStaffMenuButton);
- mainPanel.add(searchStaffMenuButton);
- mainPanel.add(addStaffMenuButton);
- mainPanel.revalidate();
- mainPanel.repaint();
- // Do stuff when each button is pressed...
- // When the list button is pressed
- listStaffMenuButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- buildListGUI(mainPanel);
- }
- });
- // When the search button is pressed
- searchStaffMenuButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- buildSearchGUI(mainPanel);
- }
- });
- // When the add button is pressed
- addStaffMenuButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- buildAddGUI(mainPanel);
- }
- });
- }
- //------------------------------------------------------------------------------------------------------------------
- // Method: buildListGUI by George
- // Clear the current GUI window, and fill with the list of staff members.
- // Called in the buildMenuGUI method.
- public static void buildListGUI(final JPanel mainPanel) {
- // Clear the mainPanel
- mainPanel.removeAll();
- // Load the file every time the List is rebuilt, to ensure it is up-to-date.
- String[][]staffData = loadFile();
- // Create columns names
- String columnNames[] = { "First Name", "Last Name", "Email", "Country", "Gender", "Language" };
- // Create a new table, using staffData as the data, and columnNames as the column names (duh...)
- JTable table = new JTable(staffData, columnNames) {
- // Stop the table from being user-editable.
- public boolean isCellEditable(int row,int column) {
- return false;
- }
- };
- // Add the table to a scrolling pane, and size to fit neatly in the window.
- JScrollPane scrollPane = new JScrollPane(table);
- scrollPane.setPreferredSize(new Dimension(990, 670));
- // Make a button to return to the menu.
- JButton returnToMenuButton = new JButton("RETURN TO MAIN MENU");
- returnToMenuButton.setPreferredSize(new Dimension(1000, 95));
- // Add the table and the button to the GUI window.
- mainPanel.add(returnToMenuButton);
- mainPanel.add(scrollPane);
- mainPanel.revalidate();
- mainPanel.repaint();
- // When the return to menu button is pressed
- returnToMenuButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- buildMenuGUI(mainPanel);
- }
- });
- }
- //------------------------------------------------------------------------------------------------------------------
- // Method: buildSearchGUI by George
- // Clear the current GUI window, and fill with the search box and results table.
- // Called in the buildMenuGUI method.
- public static void buildSearchGUI(final JPanel mainPanel) {
- // Clear the mainPanel
- mainPanel.removeAll();
- // Create the label for the text field.
- final JLabel searchTextFieldLabel = new JLabel("Search by Last Name: ");
- searchTextFieldLabel.setPreferredSize(new Dimension(140, 40));
- // Create the text field for the search term.
- final JTextField searchTextField = new JTextField();
- searchTextField.setPreferredSize(new Dimension(760, 40));
- // Create the search button.
- final JButton searchButton = new JButton("Search");
- searchButton.setPreferredSize(new Dimension(80, 40));
- // Create the table for search results.
- // Load the file every time the List is rebuilt, to ensure it is up-to-date.
- final String[][] staffData = loadFile();
- // Create columns names
- String columnNames[] = { "First Name", "Last Name", "Email", "Country", "Gender", "Language" };
- // Create a new table, using staffData as the data, and columnNames as the column names (duh...)
- final JTable table = new JTable(staffData, columnNames) {
- // Stop the table from being user-editable.
- public boolean isCellEditable(int row,int column) {
- return false;
- }
- };
- // Add the table to a scrolling pane, and size to fit neatly in the window.
- final JScrollPane scrollPane = new JScrollPane(table);
- scrollPane.setPreferredSize(new Dimension(990, 620));
- // Make a button to return to the menu.
- final JButton returnToMenuButton = new JButton("RETURN TO MAIN MENU");
- returnToMenuButton.setPreferredSize(new Dimension(1000, 95));
- // Add the components to the GUI window.
- mainPanel.add(returnToMenuButton);
- mainPanel.add(searchTextFieldLabel);
- mainPanel.add(searchTextField);
- mainPanel.add(searchButton);
- mainPanel.add(scrollPane);
- mainPanel.revalidate();
- mainPanel.repaint();
- // When the search button is pressed.
- searchButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- mainPanel.removeAll();
- String searchTerm = searchTextField.getText().toLowerCase();
- String[][] searchResults = searchArray(staffData, searchTerm);
- // Create columns names
- String columnNames[] = { "First Name", "Last Name", "Email", "Country", "Gender", "Language" };
- // Create a new table, using staffData as the data, and columnNames as the column names (duh...)
- JTable table = new JTable(searchResults, columnNames) {
- // Stop the table from being user-editable.
- public boolean isCellEditable(int row,int column) {
- return false;
- }
- };
- // Add the table to a scrolling pane, and size to fit neatly in the window.
- JScrollPane scrollPane = new JScrollPane(table);
- scrollPane.setPreferredSize(new Dimension(990, 620));
- mainPanel.add(returnToMenuButton);
- mainPanel.add(searchTextFieldLabel);
- mainPanel.add(searchTextField);
- mainPanel.add(searchButton);
- mainPanel.add(scrollPane);
- mainPanel.revalidate();
- mainPanel.repaint();
- }
- });
- // When the return to menu button is pressed.
- returnToMenuButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- buildMenuGUI(mainPanel);
- }
- });
- }
- //------------------------------------------------------------------------------------------------------------------
- // Method: buildAddGUI by George
- // Clear the current GUI window, and fill with the add form.
- // Called in the buildMenuGUI method.
- public static void buildAddGUI (final JPanel mainPanel) {
- // Clear the mainPanel
- mainPanel.removeAll();
- // Make a button to return to the menu.
- JButton returnToMenuButton = new JButton("RETURN TO MAIN MENU");
- returnToMenuButton.setPreferredSize(new Dimension(1000, 95));
- // Set up the labels and corresponding input fields.
- JLabel addFirstNameFieldLabel = new JLabel("First Name: ");
- addFirstNameFieldLabel.setPreferredSize(new Dimension(100, 40));
- final JTextField addFirstNameField = new JTextField();
- addFirstNameField.setPreferredSize(new Dimension(850, 40));
- JLabel addLastNameFieldLabel = new JLabel("Last Name: ");
- addLastNameFieldLabel.setPreferredSize(new Dimension(100, 40));
- final JTextField addLastNameField = new JTextField();
- addLastNameField.setPreferredSize(new Dimension(850, 40));
- JLabel addEmailFieldLabel = new JLabel("Email: ");
- addEmailFieldLabel.setPreferredSize(new Dimension(100, 40));
- final JTextField addEmailField = new JTextField();
- addEmailField.setPreferredSize(new Dimension(850, 40));
- JLabel addCountryFieldLabel = new JLabel("Country: ");
- addCountryFieldLabel.setPreferredSize(new Dimension(100, 40));
- final JTextField addCountryField = new JTextField();
- addCountryField.setPreferredSize(new Dimension(850, 40));
- JLabel addGenderFieldLabel = new JLabel("Gender: ");
- addGenderFieldLabel.setPreferredSize(new Dimension(100, 40));
- String[] genderChoices = {"Male", "Female"};
- final JComboBox addGenderField = new JComboBox(genderChoices);
- addGenderField.setPreferredSize(new Dimension(850, 40));
- JLabel addLanguageFieldLabel = new JLabel("Language: ");
- addLanguageFieldLabel.setPreferredSize(new Dimension(100, 40));
- final JTextField addLanguageField = new JTextField();
- addLanguageField.setPreferredSize(new Dimension(850, 40));
- // Create the add staff button
- JButton addStaffButton = new JButton("ADD STAFF MEMBER");
- addStaffButton.setPreferredSize(new Dimension(1000, 160));
- // Add everything to the GUI window.
- mainPanel.add(returnToMenuButton);
- mainPanel.add(addFirstNameFieldLabel);
- mainPanel.add(addFirstNameField);
- mainPanel.add(addLastNameFieldLabel);
- mainPanel.add(addLastNameField);
- mainPanel.add(addEmailFieldLabel);
- mainPanel.add(addEmailField);
- mainPanel.add(addCountryFieldLabel);
- mainPanel.add(addCountryField);
- mainPanel.add(addGenderFieldLabel);
- mainPanel.add(addGenderField);
- mainPanel.add(addLanguageFieldLabel);
- mainPanel.add(addLanguageField);
- mainPanel.add(addStaffButton);
- mainPanel.revalidate();
- mainPanel.repaint();
- // When the add button is pressed.
- addStaffButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- writeToFile(addFirstNameField.getText(),addLastNameField.getText(),addEmailField.getText(),addCountryField.getText(),addGenderField.getSelectedItem().toString(),addLanguageField.getText());
- }
- });
- // When the return to menu button is pressed.
- returnToMenuButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- buildMenuGUI(mainPanel);
- }
- });
- }
- //------------------------------------------------------------------------------------------------------------------
- // Method: writeToFile by Matt
- // Adds the parameters to the data array and then calls addFile to write this to the csv
- public static void writeToFile (String firstName, String lastName, String email, String country, String gender, String language)
- {
- // Load the old version of the file.
- String[][] staffData = loadFile();
- // Make a new array that is one index larger.
- String[][] newStaffData = new String[staffData.length + 1][6];
- // Copy the old array to the new one.
- for (int i = 0; i < staffData.length; i ++)
- {
- for (int j = 0; j < 6; j++)
- {
- newStaffData[i][j] = staffData[i][j];
- }
- }
- //Add to the new 2D array
- System.out.println(firstName + " " + lastName + " " + email + " " + country + " " + gender + " " +language);
- newStaffData[newStaffData.length - 1][0] = firstName;
- newStaffData[newStaffData.length - 1][1] = lastName;
- newStaffData[newStaffData.length - 1][2] = email;
- newStaffData[newStaffData.length - 1][3] = country;
- newStaffData[newStaffData.length - 1][4] = (gender.charAt(0) + "").toUpperCase();
- newStaffData[newStaffData.length - 1][5] = language;
- //Rewrite the file
- addFile(newStaffData);
- }
- //------------------------------------------------------------------------------------------------------------------
- // Method: addFile by Matt
- // Writes the contents of the parameter 2D array to the STAFF.csv file.
- public static void addFile(String[][] newStaffData)
- {
- try
- {
- PrintWriter pw = new PrintWriter(new FileWriter ("/Users/MatthewClisby/Documents/Comp Sci/Programs/JAVA OUTPUTS/STAFF.csv"));
- // Write each row of the 2d array to the file in a comma separated line.
- for(int i = 0; i < newStaffData.length; i++)
- {
- pw.println(newStaffData[i][0] + "," + newStaffData[i][1] + "," + newStaffData[i][2] + "," + newStaffData[i][3] + "," + newStaffData[i][4] + "," + newStaffData[i][5]);
- }
- pw.close();
- }
- catch (IOException e)
- {
- // output to console if there is a problem with writing
- System.out.println("Something went wrong with writing to the file.");
- }
- }
- //------------------------------------------------------------------------------------------------------------------
- // Method: searchData by Matt
- // Searches the last names of the given array for the search term parameter.
- // Returns the results in a 2D array.
- public static String[][] searchArray (String[][] staffData, String searchTerm)
- {
- // Count the number of results of the search.
- int found = 0;
- for(int i = 0; i < staffData.length; i++)
- {
- if(staffData[i][1].toLowerCase().contains(searchTerm))
- {
- found++;
- }
- }
- // Set up a 2d array with the right number of slots for the results
- String[][] searchArrayResults = new String[found][6];
- int counter = 0;
- for(int i = 0; i < staffData.length; i++)
- {
- if(staffData[i][1].toLowerCase().contains(searchTerm))
- {
- searchArrayResults[counter][0] = staffData[i][0];
- searchArrayResults[counter][1] = staffData[i][1];
- searchArrayResults[counter][2] = staffData[i][2];
- searchArrayResults[counter][3] = staffData[i][3];
- searchArrayResults[counter][4] = staffData[i][4];
- searchArrayResults[counter][5] = staffData[i][5];
- counter++;
- }
- }
- return searchArrayResults;
- }
- //------------------------------------------------------------------------------------------------------------------
- // Method: loadFile by Matt
- // Read in the csv file and split into a 2D array.
- // Returns the data in a 2D array.
- public static String[][] loadFile()
- {
- LinkedList<String> rawData = new LinkedList<String>();
- // try-catch for IO Exceptions.
- try
- {
- // Set up the BufferedReader.
- FileReader reader = new FileReader("/Users/MatthewClisby/Documents/Comp Sci/Programs/JAVA OUTPUTS/STAFF.csv");
- BufferedReader br = new BufferedReader (reader);
- // Put each line of the file into a LinkedList, to allow for varying lengths.
- String line;
- while ((line = br.readLine()) != null)
- {
- rawData.add(line);
- }
- br.close();
- }
- catch (IOException e)
- {
- // Output error message to console if there was a problem
- System.out.println("There was a problem with reading the file.");
- }
- // Move the data from the LinkedList to the 2D array for use with JTable.
- String[][] staffData = new String[rawData.size()][6];
- for (int i = 0; i < rawData.size(); i++)
- {
- String[] splitLine = rawData.get(i).split(",");
- for (int j = 0; j < 6; j++)
- {
- staffData[i][j] = splitLine[j];
- }
- }
- return staffData;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement