SHARE
TWEET

Untitled

a guest Oct 4th, 2017 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  *  ============================================================================================
  3.  *  A3.java : Book Searching program
  4.  *  YOUR UPI: Hkau513
  5.  *  ============================================================================================
  6.  */
  7. import java.awt.*;
  8. import java.awt.event.*;
  9. import javax.swing.*;
  10. import javax.swing.event.ListSelectionListener;
  11. import javax.swing.event.ListSelectionEvent;
  12.  
  13. public class A3 extends JFrame {
  14.     static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  15.     static final String USER = "hkau513";
  16.     static final String PASS = "6df92758";
  17.     static final String URL = "jdbc:mysql://studdb-mysql.fos.auckland.ac.nz:3306/stu_hkau513_COMPSCI_280_C_S2_2017";
  18.  
  19.     //declare and create GUI components
  20.     private JTextField titleTextField;
  21.     private JButton searchButton;
  22.     private JTable bookTable;
  23.     private BookTableModel bookTableModel;
  24.     //complete this
  25.     private JPanel contentPane;
  26.     private JTable bookListTextArea;
  27.     private JTextArea bookDetailsTextArea;
  28.  
  29.   public A3() {
  30.     Container pane = getContentPane();
  31.     pane.setLayout(new BorderLayout());
  32.  
  33.     JPanel topPanel = new JPanel();
  34.     topPanel.setLayout(new FlowLayout());
  35.     JLabel label = new JLabel("Enter title");
  36.     //complete the top panel
  37.     topPanel.add(label);
  38.    
  39.     titleTextField = new JTextField();
  40.     topPanel.add(titleTextField);
  41.     titleTextField.setColumns(10);
  42.    
  43.     searchButton = new JButton("Search");
  44.     topPanel.add(searchButton);
  45.    
  46.     bookDetailsTextArea = new JTextArea();
  47.     pane.add(bookDetailsTextArea, BorderLayout.SOUTH);
  48.    
  49.     bookTableModel = new BookTableModel(JDBC_DRIVER, URL, USER, PASS);
  50.     bookTable = new JTable(bookTableModel);
  51.    
  52.     JScrollPane newtable = new JScrollPane(bookTable);
  53.     pane.add(new JScrollPane(bookTable), BorderLayout.CENTER);
  54.    
  55.     bookTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  56.    
  57.     titleTextField.addActionListener(new ActionListener(){
  58.         // This method is invoked when the user hits ENTER in this feild
  59.         public void actionPerformed(ActionEvent enter ){
  60.             String searchText = titleTextField.getText();
  61.             bookTableMethod.populateTable(searchText);
  62.             bookTableMethod.fireTableDataChanged();
  63.         }
  64.     });
  65.    
  66.     titleTextField.addActionListener(new ActionListener() {
  67.         // This method is invoked when the user hits ENTER in the field
  68.         public void actionPerformed(ActionEvent enter) {
  69.             String searchText = titleTextField.getText();
  70.             bookTableMethod.popluateTable(searchText);
  71.             bookTableMethod.fireTableDataChanged();
  72.         }
  73.     });
  74.    
  75.     searchButton.addActionListener(new ActionListener() {
  76.             // This method is invoked when the user hits ENTER in the field
  77.         public void actionPerformed(ActionEvent enter) {
  78.             String searchText = titleTextField.getText();
  79.             bookTableMethod.popluateTable(searchText);
  80.             bookTableMethod.fireTableDataChanged();
  81.         }
  82.     });
  83.  
  84.     //topPanel.add(label);
  85.     //complete this
  86.  
  87.     pane.add(topPanel, BorderLayout.PAGE_START);
  88.  
  89.     bookTable.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
  90.         public void valueChanged(ListSelectionEvent event) {
  91.             if(event.getValueIsAdjusting()){
  92.                 return;
  93.             }
  94.             if(bookTable.getSelectedRow()<0){
  95.                 return;
  96.             }
  97.         }
  98.     });
  99.   }
  100.  
  101.     /**
  102.    * Create the GUI and show it. For thread safety, this method should be
  103.    * invoked from the event-dispatching thread.
  104.    */
  105.    
  106.   private static void createAndShowGUI() {
  107.     //Make sure we have nice window decorations.
  108.     JFrame.setDefaultLookAndFeelDecorated(true);
  109.     //Create and set up the window.
  110.     A3 frame = new A3();
  111.     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  112.     //Display the window.
  113.     frame.pack();
  114.     frame.setVisible(true);
  115.   }
  116.  
  117.   public static void main(String[] args) {
  118.     //Schedule a job for the event-dispatching thread:
  119.     //creating and showing this application's GUI.
  120.     javax.swing.SwingUtilities.invokeLater(new Runnable() {
  121.       public void run() {
  122.         createAndShowGUI();
  123.     }
  124.     });
  125.   }
  126.  
  127. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top