-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBookSearch.java
More file actions
131 lines (102 loc) · 3.8 KB
/
Copy pathBookSearch.java
File metadata and controls
131 lines (102 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package lms;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
/**
* Creates GUI (window) for searching for books by criterion. Designs window elements and actions.
*/
public class BookSearch extends JFrame {
//***Data fields***
private JButton search = new JButton("Search!");
private JButton seeAll = new JButton("See All Books");
private String keywordOption = "Title"; // default
JTextField criterionEntry = new JTextField();
/**
* Creates GUI (window) for searching for books by criterion. Designs window elements and actions.
*/
public BookSearch(){
JPanel display = new JPanel();
display.setLayout(new BorderLayout(5,5));
JPanel north = new JPanel();
north.setLayout(new GridLayout(2,1,10,5));
JLabel header = new JLabel("SEARCH FOR BOOKS");
header.setHorizontalAlignment(SwingConstants.CENTER);
header.setFont(new Font("Arial", Font.BOLD, 15));
north.add(header);
north.add(seeAll);
display.add(north, BorderLayout.NORTH);
JPanel west = new JPanel();
west.setLayout(new GridLayout(2,1,10,5));
west.add(new JLabel("Search by:"));
String [] keywords = {"Title", "Author", "Genre"};
JComboBox<String> searchDropDown = new JComboBox<String>(keywords);
searchDropDown.setSelectedIndex(0);
searchDropDown.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
// set value of selectedOption
// https://stackoverflow.com/questions/4962416/preferred-way-of-getting-the-selected-item-of-a-jcombobox
keywordOption = String.valueOf(searchDropDown.getSelectedItem());
}
});
west.add(searchDropDown);
display.add(west, BorderLayout.WEST);
JPanel center = new JPanel();
center.setLayout(new GridLayout(2,1,10,5));
center.add(new JPanel()); // spacer
center.add(criterionEntry);
display.add(center, BorderLayout.CENTER);
JPanel south = new JPanel();
south.setLayout(new GridLayout(1,3,25,5));
south.add(new JLabel()); //spacer
south.add(search); //spacer
south.add(new JLabel()); //spacer
display.add(south, BorderLayout.SOUTH);
add(display);
//Create and register listeners
UpdateListener buttonPress = new UpdateListener();
search.addActionListener(buttonPress);
//https://stackoverflow.com/a/31776595
search.addActionListener(e -> this.dispose()); // closes search box
seeAll.addActionListener(buttonPress);
seeAll.addActionListener(e -> this.dispose());
}
/**
* Main method for BookSearch class; used to finalize display of BookSearch window/GUI
* @param args: String array; default is null
*/
public static void main(String[] args){
BookSearch frame = new BookSearch();
frame.setDefaultCloseOperation(HIDE_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setTitle("SEARCH");
frame.setVisible(true);
}
//***Inner Class***
/**
* Implements ActionListener for buttons in BookSearch window/GUI.
*/
class UpdateListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==search){
// get info from text entry field
String criterion = criterionEntry.getText().trim(); // remove excess white space on either side of text
Patron.bookSearch(null, keywordOption.toLowerCase(), criterion, "window");
}
if(e.getSource()==seeAll){
Patron.bookSearch(null, null, null, "window");
}
}
}
}