-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAddBookGUI.java
More file actions
163 lines (129 loc) · 5.57 KB
/
Copy pathAddBookGUI.java
File metadata and controls
163 lines (129 loc) · 5.57 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package lms;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
public class AddBookGUI implements ActionListener {
//Add various components. Helps to add them here in case we want to use the information in our ActionListener later
/**
* Text field to input title info
*/
public static JTextField title = new JTextField();
/**
* Text field to input author info
*/
public static JTextField author = new JTextField();
/**
* Text field to input genre info
*/
public static JTextField genre = new JTextField();
/**
* Button used to add book
*/
public static JButton addBook = new JButton("Add Book");
/**
* Constructor for the window.
* Sets sizes, layouts, and action events/listeners for all elements of the page
*/
public AddBookGUI(){
// Creating instance of JFrame
JFrame frame = new JFrame("Welcome to our humble library!");
// Set the width and height of frame
frame.setSize(800, 400);
//Create the panel that contains the "boxes" of content
JPanel boxes = new JPanel();
boxes.setLayout(new GridLayout(3,1,10,0));
//Create the Header for the Sign in section
JPanel box1 = new JPanel();
JLabel signInHeader = new JLabel("Add Book to Database");
signInHeader.setHorizontalAlignment(SwingConstants.CENTER);
signInHeader.setFont(new Font("Arial", Font.BOLD, 18));
//signInHeader.setVerticalAlignment(SwingConstants.TOP);
box1.add(signInHeader);
//Create a box that holds a Patron/Librarian radio button and the sign in text field
JPanel box2 = new JPanel();
box2.setLayout(new BoxLayout(box2, BoxLayout.LINE_AXIS));
//Set a border, mainly to squish the components closer to the center
box2.setBorder(new EmptyBorder(10, 30, 10, 30));
JPanel titlePanel = new JPanel();
titlePanel.setPreferredSize(new Dimension(120,25));
JLabel titleLabel = new JLabel("Title:");
title.setPreferredSize(new Dimension(120,25));
titlePanel.add(titleLabel);
titlePanel.add(title);
box2.add(titlePanel);
JPanel authorPanel = new JPanel();
author.setPreferredSize(new Dimension(120,25));
JLabel authorLabel = new JLabel("Author:");
author.setPreferredSize(new Dimension(120,25));
authorPanel.add(authorLabel);
authorPanel.add(author);
box2.add(authorPanel);
JPanel genrePanel = new JPanel();
genre.setPreferredSize(new Dimension(150,25));
JLabel genreLabel = new JLabel("Genre:");
genre.setPreferredSize(new Dimension(150,25));
genrePanel.add(genreLabel);
genrePanel.add(genre);
box2.add(genrePanel);
//Add addBook Button
JPanel box3 = new JPanel();
addBook.setMnemonic(KeyEvent.VK_C);
addBook.setActionCommand("add");
addBook.addActionListener(this);
box3.add(addBook);
// Use an empty panel to add space between two main sections
//JPanel spacer = new JPanel();
//spacer.add(Box.createRigidArea(new Dimension(10, 50)));
boxes.add(box1);
boxes.add(box2);
boxes.add(box3);
//Give entire window some padding
boxes.setBorder(new EmptyBorder(10, 0, 30, 0));
//Add boxes to frame
frame.add(boxes);
//Setting the frame visibility to true
frame.setVisible(true);
//Close the Window without exiting application on close
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
/**
* Overriding the abstract actionPerformed method
* Writes logic to handle the actions for various buttons/fields in the window
* Detects login info/addBook button clicks to navigate to correct page.
*/
@Override
public void actionPerformed(ActionEvent e) {
if ("add".equals(e.getActionCommand())) {
Connection connection = DatabaseConnection.openDatabase();
//Ensure no fields are blank before adding
if(!title.getText().isBlank() && !author.getText().isBlank() && !genre.getText().isBlank()) {
Librarian.addBook(connection, "books", title.getText(), author.getText(), genre.getText());
//get the book id
String id = DatabaseQueries.getLastID(connection, "books", "book_ID");
//Show a positive message if added, and an error message if not.
JOptionPane.showMessageDialog(null, "Book successfully added! Its id is" + id, "Success!", JOptionPane.INFORMATION_MESSAGE);
}else {
JOptionPane.showMessageDialog(null, "No book was added because one or more fields were left blank.", "Field Blank", JOptionPane.WARNING_MESSAGE);
}
DatabaseConnection.closeDatabase(connection);
}
}
}