Advertisement
Guest User

Untitled

a guest
Oct 4th, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. /*
  2. * ============================================================================================
  3. * A3.java : Book Searching program
  4. * YOUR UPI: mmit352
  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 = "mmit352";
  16. static final String PASS = "74722d07";
  17. static final String URL = "jdbc:mysql://studdb-mysql.fos.auckland.ac.nz:3306/stu_mmit352_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 JTable bookListTextArea;
  26. private JPanel contentPane;
  27. private JTextArea bookDeatilsTextArea;
  28.  
  29.  
  30. public A3() {
  31. Container pane = getContentPane();
  32. pane.setLayout(new BorderLayout());
  33.  
  34. JPanel topPanel = new JPanel();
  35. topPanel.setLayout(new FlowLayout());
  36. JLabel label = new JLabel("Enter title");
  37. topPanel.add(label);
  38.  
  39. titleTextField = new JTextField();
  40. topPanel.add(titleTextField);
  41. titleTextField.setColumns(13);
  42. //complete the top panel
  43.  
  44. searchButton = new JButton("Search");
  45. topPanel.add(searchButton);
  46.  
  47. bookDetailsTextArea = new JTextArea();
  48. pane.add(bookDetailsTextArea, BorderLayout.SOUTH):
  49.  
  50. bookTableModel = new bookTableModel(JDBC_DRIVER, URL, USER, PASS):
  51. bookTable = new JTable(bookTableModel);
  52.  
  53. JScrollPane newtable = new JScrollPane(bookTable);
  54. pane.add(newtable,BorderLayout.CENTER );
  55.  
  56. bookTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  57.  
  58.  
  59.  
  60. titleTextField.addActionListener(new ActionListener() {
  61. // This method is invoked when the user hits ENTER in the field
  62. public void actionPerformed(ActionEvent e) {
  63. //complete this
  64. String searchText = titleTextField.getText();
  65. bookTableModel.populateTable(searchText);
  66. bookTableModel.fireTableDataChanged();
  67. }
  68. });
  69. searchButton.addActionListener(new ActionListener() {
  70. // This method is invoked when the user hits ENTER in the field
  71. public void actionPerformed(ActionEvent e) {
  72. //complete this
  73. string.searchText = titleTextField.getText();
  74. bookTableModel.populateTable(searchText);
  75. bookTableModel.fireTableDataChanged();
  76. }
  77. });
  78. //complete this
  79.  
  80.  
  81. pane.add(topPanel, BorderLayout.PAGE_START);
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89. bookTable.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
  90. public void valueChanged(ListSelectionEvent event) {
  91. //complete this
  92.  
  93. if (event.getValueIsAdjusting()){
  94. return;
  95. }
  96. if (bookTable.getSelectedRow()<0){
  97. return;
  98. }
  99. int newRow = bookTable.getSelectedRow():
  100. Book bookinfo = bookTableModel.getCurrentBook(newRow);
  101.  
  102. bookDetailsTextArea.setText(bookInfo.toString());
  103.  
  104. }
  105. });
  106. }
  107.  
  108. /**
  109. * Create the GUI and show it. For thread safety, this method should be
  110. * invoked from the event-dispatching thread.
  111. */
  112. private static void createAndShowGUI() {
  113. //Make sure we have nice window decorations.
  114. JFrame.setDefaultLookAndFeelDecorated(true);
  115. //Create and set up the window.
  116. A3 frame = new A3();
  117. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  118. //Display the window.
  119. frame.pack();
  120. frame.setVisible(true);
  121. }
  122.  
  123. public static void main(String[] args) {
  124. //Schedule a job for the event-dispatching thread:
  125. //creating and showing this application's GUI.
  126. javax.swing.SwingUtilities.invokeLater(new Runnable() {
  127. public void run() {
  128. createAndShowGUI();
  129. }
  130. });
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement