Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * ============================================================================================
- * A3.java : Book Searching program
- * YOUR UPI: Hkau513
- * ============================================================================================
- */
- 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 = "hkau513";
- static final String PASS = "6df92758";
- static final String URL = "jdbc:mysql://studdb-mysql.fos.auckland.ac.nz:3306/stu_hkau513_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 JPanel contentPane;
- private JTable bookListTextArea;
- private JTextArea bookDetailsTextArea;
- public A3() {
- Container pane = getContentPane();
- pane.setLayout(new BorderLayout());
- JPanel topPanel = new JPanel();
- topPanel.setLayout(new FlowLayout());
- JLabel label = new JLabel("Enter title");
- //complete the top panel
- topPanel.add(label);
- titleTextField = new JTextField();
- topPanel.add(titleTextField);
- titleTextField.setColumns(10);
- 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(new JScrollPane(bookTable), BorderLayout.CENTER);
- bookTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
- titleTextField.addActionListener(new ActionListener(){
- // This method is invoked when the user hits ENTER in this feild
- public void actionPerformed(ActionEvent enter ){
- String searchText = titleTextField.getText();
- bookTableMethod.populateTable(searchText);
- bookTableMethod.fireTableDataChanged();
- }
- });
- titleTextField.addActionListener(new ActionListener() {
- // This method is invoked when the user hits ENTER in the field
- public void actionPerformed(ActionEvent enter) {
- String searchText = titleTextField.getText();
- bookTableMethod.popluateTable(searchText);
- bookTableMethod.fireTableDataChanged();
- }
- });
- searchButton.addActionListener(new ActionListener() {
- // This method is invoked when the user hits ENTER in the field
- public void actionPerformed(ActionEvent enter) {
- String searchText = titleTextField.getText();
- bookTableMethod.popluateTable(searchText);
- bookTableMethod.fireTableDataChanged();
- }
- });
- //topPanel.add(label);
- //complete this
- pane.add(topPanel, BorderLayout.PAGE_START);
- bookTable.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
- public void valueChanged(ListSelectionEvent event) {
- if(event.getValueIsAdjusting()){
- return;
- }
- if(bookTable.getSelectedRow()<0){
- return;
- }
- }
- });
- }
- /**
- * 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