Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * ============================================================================================
- * A3.java : Book Searching program
- * YOUR UPI: mmit352
- * ============================================================================================
- */
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.swing.event.ListSelectionListener;
- import javax.swing.event.ListSelectionEvent;
- public class A3 extends JFrame {
- static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
- static final String USER = "mmit352";
- static final String PASS = "74722d07";
- static final String URL = "jdbc:mysql://studdb-mysql.fos.auckland.ac.nz:3306/stu_mmit352_COMPSCI_280_C_S2_2017";
- //declare and create GUI components
- private JTextField titleTextField;
- private JButton searchButton;
- private JTable bookTable;
- private BookTableModel bookTableModel;
- //complete this
- private JTable bookListTextArea;
- private JPanel contentPane;
- private JTextArea bookDeatilsTextArea;
- public A3() {
- Container pane = getContentPane();
- pane.setLayout(new BorderLayout());
- JPanel topPanel = new JPanel();
- topPanel.setLayout(new FlowLayout());
- JLabel label = new JLabel("Enter title");
- topPanel.add(label);
- titleTextField = new JTextField();
- topPanel.add(titleTextField);
- titleTextField.setColumns(13);
- //complete the top panel
- searchButton = new JButton("Search");
- topPanel.add(searchButton);
- bookDetailsTextArea = new JTextArea();
- pane.add(bookDetailsTextArea, BorderLayout.SOUTH):
- bookTableModel = new bookTableModel(JDBC_DRIVER, URL, USER, PASS):
- bookTable = new JTable(bookTableModel);
- JScrollPane newtable = new JScrollPane(bookTable);
- pane.add(newtable,BorderLayout.CENTER );
- bookTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
- titleTextField.addActionListener(new ActionListener() {
- // This method is invoked when the user hits ENTER in the field
- public void actionPerformed(ActionEvent e) {
- //complete this
- String searchText = titleTextField.getText();
- bookTableModel.populateTable(searchText);
- bookTableModel.fireTableDataChanged();
- }
- });
- searchButton.addActionListener(new ActionListener() {
- // This method is invoked when the user hits ENTER in the field
- public void actionPerformed(ActionEvent e) {
- //complete this
- string.searchText = titleTextField.getText();
- bookTableModel.populateTable(searchText);
- bookTableModel.fireTableDataChanged();
- }
- });
- //complete this
- pane.add(topPanel, BorderLayout.PAGE_START);
- bookTable.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
- public void valueChanged(ListSelectionEvent event) {
- //complete this
- if (event.getValueIsAdjusting()){
- return;
- }
- if (bookTable.getSelectedRow()<0){
- return;
- }
- int newRow = bookTable.getSelectedRow():
- Book bookinfo = bookTableModel.getCurrentBook(newRow);
- bookDetailsTextArea.setText(bookInfo.toString());
- }
- });
- }
- /**
- * Create the GUI and show it. For thread safety, this method should be
- * invoked from the event-dispatching thread.
- */
- private static void createAndShowGUI() {
- //Make sure we have nice window decorations.
- JFrame.setDefaultLookAndFeelDecorated(true);
- //Create and set up the window.
- A3 frame = new A3();
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- //Display the window.
- frame.pack();
- frame.setVisible(true);
- }
- public static void main(String[] args) {
- //Schedule a job for the event-dispatching thread:
- //creating and showing this application's GUI.
- javax.swing.SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- createAndShowGUI();
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement