Check
Check
view;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import javax.swing.Timer;
import javax.swing.table.DefaultTableModel;
import com.hat.controller.InitInformation;
import com.hat.model.BookSearchApp;
import com.hat.model.BorrowedDocument;
import com.hat.model.DatabaseConnection;
import com.hat.model.Document;
import com.hat.model.PublicUser;
import com.hat.model.Student;
import com.hat.model.User;
import com.hat.model.ValidationUtils;
public adminUI() {
String[] columnNames = {"ID", "Name", "Date of Birth", "Address", "Email",
"Phone", "Account", "Password"};
model = new DefaultTableModel(columnNames, 0);
table = new JTable(model);
setColumnWidths(table, USER_COLUMNS);
bookSearchApp = new BookSearchApp();
JPanel mainPanel = new JPanel(new BorderLayout());
setTitle("Custom UI");
setSize(1000, 700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
createHomePanel();
createUserManagement();
createBookManagement();
createNotificationPanel();
createBorrowedBookList();
contentPane.add(homePanel, "home");
contentPane.add(ChatPanel, "ChatPanel");
contentPane.add(bookPanel, "bookManagement");
contentPane.add(userPanel, "userManagement");
contentPane.add(borrowedBookPanel, "borrowedBookList");
createSideBar();
mainPanel.add(sidebar, BorderLayout.WEST);
mainPanel.add(contentPane, BorderLayout.CENTER);
add(mainPanel);
}
searchField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
SwingUtilities.invokeLater(() -> {
// Tạo và thêm loadingPanel vào homePanel trước khi bắt đầu
tìm kiếm
MovingImagePanel loadingPanel = new MovingImagePanel();
// Cập nhật giao diện để hiển thị loadingPanel ngay lập tức
homePanel.revalidate();
homePanel.repaint();
@Override
protected void done() {
try {
// Lấy kết quả từ SwingWorker
JPanel resultPanel = get(); // Nhận JPanel kết
quả tìm kiếm từ doInBackground
};
homePanel.add(searchPanel, BorderLayout.NORTH);
userPanel.add(searchPanel, BorderLayout.NORTH);
}
});
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// Kiểm tra sự kiện nhấn đúp chuột
if (e.getClickCount() == 2) {
int selectedRow = table.getSelectedRow(); // Lấy hàng được chọn
if (selectedRow != -1) { // Kiểm tra nếu hàng hợp lệ
// Lấy dữ liệu từ hàng
try {
Object id = table.getValueAt(selectedRow, 0);
int userId = Integer.parseInt(id.toString());
User user = DatabaseConnection.getUserById(userId);
if (user != null) {
if (detailPanel != null) {
userContentPane.remove(detailPanel);
}
createDetailPanel(user, model);
userContentPane.add(detailPanel, "detail");
userCardLayout.show(userContentPane, "detail");
} else {
System.out.println("Không tìm thấy User với ID: " +
userId);
}
} catch (Exception ex) {
ex.printStackTrace(); // In lỗi chi tiết để xác định
vấn đề
}
}
}
}
});
userListPanel.add(scrollPane);
gbc.gridx = 0;
gbc.gridy = 10;
gbc.insets = new Insets(10, -200, 0, 0);
studentBox = new RoundCheckBox("student");
userListPanel.add(studentBox, gbc);
gbc.insets = new Insets(10, 200, 0, 10);
publicUserBox = new RoundCheckBox("public user");
userListPanel.add(publicUserBox, gbc);
studentBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
publicUserBox.setSelected(false); // Deselect publicUserBox if
studentBox is selected
model.setColumnIdentifiers(STUDENT_COLUMNS); // Update table
columns
loadStudentsToTable(DatabaseConnection.getAllStudents(),
model); // Load students into table
setColumnWidths(table, STUDENT_COLUMNS); // Set column widths
} else if (e.getStateChange() == ItemEvent.DESELECTED) {
model.setColumnIdentifiers(USER_COLUMNS); // Revert to default
user columns
loadUsersToTable(DatabaseConnection.getAllUsers(), model); //
Load users into table
setColumnWidths(table, USER_COLUMNS); // Set column widths
}
userListPanel.revalidate(); // Update UI
userListPanel.repaint();
}
});
publicUserBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
studentBox.setSelected(false); // Deselect studentBox if
publicUserBox is selected
model.setColumnIdentifiers(PUBLIC_USER_COLUMNS); // Update
table columns
loadPublicUsersToTable(DatabaseConnection.getAllPublicUsers(),
model); // Load public users into
// table
setColumnWidths(table, PUBLIC_USER_COLUMNS); // Set column
widths
} else if (e.getStateChange() == ItemEvent.DESELECTED) {
model.setColumnIdentifiers(USER_COLUMNS); // Revert to default
user columns
loadUsersToTable(DatabaseConnection.getAllUsers(), model); //
Load users into table
setColumnWidths(table, USER_COLUMNS); // Set column widths
}
userListPanel.revalidate(); // Update UI
userListPanel.repaint();
}
});
gbc.gridx = 1;
gbc.gridy = 11;
gbc.insets = new Insets(10, -450, 30, 10);
RoundedButton addButton = createButton(30, 17, "Add user", 120, 30,
colorBackground, hoverColor, false);
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
userCardLayout.show(userContentPane, "addUser");
}
});
userListPanel.add(addButton, gbc);
gbc.insets = new Insets(10, -140, 30, 10);
RoundedButton removeUserButton = createButton(30, 17, "Remove user", 150,
30, colorBackground, hoverColor,
false);
removeUserButton.addActionListener(e -> {
int selectedRow = table.getSelectedRow();
if (selectedRow >= 0) {
// Get user ID and name from selected row
int userId = Integer.parseInt(table.getValueAt(selectedRow,
0).toString());
String userName = table.getValueAt(selectedRow, 1).toString();
} else {
JOptionPane.showMessageDialog(null, "Please select a user to
remove.");
}
});
userListPanel.add(removeUserButton, gbc);
gbc.gridy = 2;
JPanel email = createTextField("emailSignUp", null, 30, 15, "Enter user's
email");
addUserPanel.add(email, gbc);
gbc.gridy = 3;
JPanel user = createTextField("userSignUp", null, 30, 15, "Enter user's
username");
addUserPanel.add(user, gbc);
gbc.gridy = 4;
JPanel ID = createTextField("ID", null, 30, 15, "Enter user's ID");
addUserPanel.add(ID, gbc);
gbc.gridy = 5;
gbc.insets = new Insets(0, -150, 5, 0);
RoundedComboBox faculty = new RoundedComboBox(
new String[]{"FIT", "FET", "IAI", "FEMA", "SAE", "FEPN", "FAT",
"FCE"}, "Enter faculty", 15,
130, 35);
faculty.setBackground(colorBackground);
addUserPanel.add(faculty, gbc);
gbc.gridy = 7;
JPanel phoneNumber = createTextField("phoneNumber", null, 30, 15, "Enter
your phone number");
addUserPanel.add(phoneNumber, gbc);
gbc.gridy = 8;
JPanel address = createTextField("address", null, 30, 15, "Enter your
address");
addUserPanel.add(address, gbc);
gbc.gridy = 9;
JPanel dob = createTextField("dob", null, 30, 15, "Enter your date of
birth");
addUserPanel.add(dob, gbc);
gbc.gridy = 10;
gbc.insets = new Insets(5, -50, 5, 10);
checkbox1 = new RoundCheckBox("Student");
addUserPanel.add(checkbox1, gbc);
gbc.gridy = 11;
gbc.insets = new Insets(10, 40, 30, 10);
RoundedButton addUserButton = createButton(30, 17, "Add user", 120, 30,
colorBackground, hoverColor, false);
addUserButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String fullName = getTextFromField("fullNameSignUp");
String email = getTextFromField("emailSignUp");
String username = getTextFromField("userSignUp");
String ID = getTextFromField("ID");
if (checkbox1.isSelected()) {
String newFaculty = faculty.getSelectedItem().toString();
String newMajor = major.getSelectedItem().toString();
Student student = new Student(fullName, dob, address, email,
phoneNumber, username, "123456",
newFaculty, newMajor,
ID);
student.signUp(InitInformation.library);
loadUsersToTable(DatabaseConnection.getAllUsers(), model); //
Load users into table
} else {
String job = getTextFromField("job");
PublicUser publicUser = new PublicUser(fullName, dob, address,
email, phoneNumber, username,
"123456", ID, job);
publicUser.signUp(InitInformation.library);
}
loadUsersToTable(DatabaseConnection.getAllUsers(), model); // Load
users into table
JOptionPane.showMessageDialog(null, "dang ky thanh cong");
userCardLayout.show(userContentPane, "list");
}
});
addUserPanel.add(addUserButton, gbc);
useraddPanel.add(addUserPanel, gbc);
userContentPane.add(userListPanel, "list");
userContentPane.add(useraddPanel, "addUser");
userPanel.add(userContentPane, BorderLayout.CENTER);
detailContentPane.setBackground(Color.WHITE);
gbc.gridy = 1;
gbc.weighty = 1.0;
// gbc1.fill = GridBagConstraints.CENTER
gbc.fill = GridBagConstraints.BOTH;
detailPanel.add(detailContentPane, gbc);
detailContentPane.add(infoPanel, "userPanel");
gbc.gridy = 3;
JPanel user = createTextField("userSignUp", null, 30, 15,
currentUser.getAccount(), "Username: ", false);
userPanel.add(user, gbc);
gbc.gridy = 4;
JPanel ID;
if (student != null) {
ID = createTextField("ID", null, 30, 15, student.getStudentId(), "ID:
", false);
} else {
ID = createTextField("ID", null, 30, 15, publicUser.getIdentityCard(),
"Identify card: ", false);
}
userPanel.add(ID, gbc);
gbc.gridy = 5;
JPanel faculty;
JPanel job;
if (student != null) {
faculty = createTextField("faculty", null, 30, 15,
student.getFaculty(), "Faculty: ", false);
userPanel.add(faculty, gbc);
} else {
job = createTextField("job", null, 30, 15, publicUser.getJob(), "Job:
", false);
userPanel.add(job, gbc);
}
if (student != null) {
gbc.gridy = 6;
JPanel major = createTextField("major", null, 30, 15,
student.getMajor(), "Major: ", false);
major.setBackground(colorBackground);
userPanel.add(major, gbc);
}
gbc.gridy = 7;
JPanel phoneNumber = createTextField("phoneNumber", null, 30, 15,
currentUser.getPhoneNumber(), "Phone number: ",
false);
userPanel.add(phoneNumber, gbc);
gbc.gridy = 8;
JPanel address = createTextField("address", null, 30, 15,
currentUser.getAddress(), "Address: ", false);
userPanel.add(address, gbc);
gbc.gridy = 9;
JPanel dob = createTextField("dob", null, 30, 15,
currentUser.getDateOfBirth(), "Date of birth: ", false);
userPanel.add(dob, gbc);
gbc.gridy = 10;
gbc.anchor = GridBagConstraints.EAST;
gbc.insets = new Insets(5, 300, 5, 0);
JButton editButton = new JButton(edit);
editButton.setBackground(colorBackground);
editButton.setOpaque(false);
editButton.setBorderPainted(false);
editButton.setFocusPainted(false);
editButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
detailCardLayout.show(detailContentPane, "displayInfoPanel");
}
});
userPanel.add(editButton, gbc);
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.HORIZONTAL; // Mở rộng chiều ngang
gbc.gridx = 0;
gbc.gridy = 1;
gbc.weightx = 1.0;
gbc.insets = new Insets(5, 40, 5, 0);
JPanel displayName = createTextField("displayName", null, 30, 15,
currentUser.getName(), "Full name: ", true);
displayUserPanel.add(displayName, gbc);
gbc.gridy = 3;
JPanel displayUser = createTextField("displayUser", null, 30, 15,
currentUser.getAccount(), "Username: ",
false);
displayUserPanel.add(displayUser, gbc);
gbc.gridy = 4;
JPanel displayID;
if (student != null) {
displayID = createTextField("displayID", null, 30, 15,
student.getStudentId(), "ID: ", false);
} else {
displayID = createTextField("displayID", null, 30, 15,
publicUser.getIdentityCard(), "Identify card: ",
true);
}
displayUserPanel.add(displayID, gbc);
gbc.gridy = 5;
JPanel displayFaculty;
JPanel displayJob;
if (student != null) {
displayFaculty = createTextField("displayFaculty", null, 30, 15,
student.getFaculty(), "Faculty: ", true);
displayUserPanel.add(displayFaculty, gbc);
gbc.gridy = 6;
gbc.gridy = 6;
gbc.gridy = 7;
gbc.insets = new Insets(5, 40, 5, 0);
JPanel displayPhoneNumber = createTextField("displayPhoneNumber", null, 30,
15, currentUser.getPhoneNumber(),
"Phone number: ", true);
displayUserPanel.add(displayPhoneNumber, gbc);
gbc.gridy = 8;
JPanel displayAddress = createTextField("displayAddress", null, 30, 15,
currentUser.getAddress(), "Address: ",
true);
displayUserPanel.add(displayAddress, gbc);
gbc.gridy = 9;
JPanel displayDob = createTextField("displayDob", null, 30, 15,
currentUser.getDateOfBirth(),
"Date of birth: ", true);
displayUserPanel.add(displayDob, gbc);
gbc.gridy = 12;
gbc.insets = new Insets(5, 40, 5, 40);
RoundedButton saveButton = createButton(35, 17, "Save", 100, 35,
colorButton, hoverColor, false);
gbc.anchor = GridBagConstraints.EAST;
displayUserPanel.add(saveButton, gbc);
saveButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Retrieve input values from textFieldMap
String email = textFieldMap.get("displayEmail").getText();
if (email.equals("")) {
email = currentUser.getEmail();
}
String phoneNumber =
textFieldMap.get("displayPhoneNumber").getText();
if (phoneNumber.equals("")) {
phoneNumber = currentUser.getPhoneNumber();
}
String id = textFieldMap.get("displayID").getText();
major = textFieldMap.get("displayMajor").getText();
if (major.equals("")) {
major = ((Student) currentUser).getMajor();
}
id = ((Student) currentUser).getStudentId();
} else if (currentUser instanceof PublicUser) {
identityCard =
textFieldMap.get("displayIdentityCard").getText();
if (identityCard.equals("")) {
identityCard = ((PublicUser)
currentUser).getIdentityCard();
}
job = textFieldMap.get("displayJob").getText();
if (job.equals("")) {
job = ((PublicUser) currentUser).getJob();
}
System.out.println("Identity Card: " + identityCard);
}
if (!phoneNumber.isEmpty() && !
ValidationUtils.isValidPhoneNumber(phoneNumber)) {
JOptionPane.showMessageDialog(null, "Invalid phone number
format. Must be 10 digits");
hasError = true;
}
if (hasError) {
return;
}
currentUser.setEmail(email);
currentUser.setPhoneNumber(phoneNumber);
currentUser.setAddress(address);
currentUser.setDateOfBirth(dob);
userPanel.revalidate();
userPanel.repaint();
loadUsersToTable(DatabaseConnection.getAllUsers(), model);
userCardLayout.show(userContentPane, "list");
}
});
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.getColumnModel().getColumn(0).setPreferredWidth(25);
table.getColumnModel().getColumn(1).setPreferredWidth(400);
table.getColumnModel().getColumn(2).setPreferredWidth(100);
table.getColumnModel().getColumn(3).setPreferredWidth(200);
table.getColumnModel().getColumn(4).setPreferredWidth(150);
table.getColumnModel().getColumn(5).setPreferredWidth(50);
table.getColumnModel().getColumn(6).setPreferredWidth(60);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// Kiểm tra sự kiện nhấn đúp chuột
if (e.getClickCount() == 2) {
int selectedRow = table.getSelectedRow(); // Lấy hàng được chọn
if (selectedRow != -1) { // Kiểm tra nếu hàng hợp lệ
// Lấy dữ liệu từ hàng
try {
Object id = table.getValueAt(selectedRow, 0);
int bookId = Integer.parseInt(id.toString());
Document document =
DatabaseConnection.getDocumentById(bookId);
if (document != null) {
if (detailPanel != null) {
bookContentPane.remove(detailPanel);
}
createDetailPanel(document, model);
bookContentPane.add(detailPanel, "detail");
bookCardLayout.show(bookContentPane, "detail");
} else {
System.out.println("Không tìm thấy sách với ID: " +
bookId);
}
} catch (Exception ex) {
ex.printStackTrace(); // In lỗi chi tiết để xác định
vấn đề
}
}
}
}
});
bookListPanel.add(scrollPane);
gbc.gridx = 1;
gbc.gridy = 10;
gbc.insets = new Insets(10, -450, 30, 10);
RoundedButton addButton = createButton(30, 17, "Add book", 120, 30,
colorBackground, hoverColor, false);
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
bookCardLayout.show(bookContentPane, "addBook");
}
});
bookListPanel.add(addButton, gbc);
if (confirm == JOptionPane.YES_OPTION) {
DatabaseConnection.removeDocumentById(bookId);
InitInformation.documents =
DatabaseConnection.getAllDocuments();
loadBooksToTable(InitInformation.documents, model);
JOptionPane.showMessageDialog(null, "Book removed
successfully!");
}
} else {
JOptionPane.showMessageDialog(null, "Please select a book to
remove.");
}
});
bookListPanel.add(removeBookButton, gbc);
gbc.gridy = 2;
JPanel author = createTextField("author", null, 30, 15, "author");
addBookPanel.add(author, gbc);
gbc.gridy = 3;
JPanel category = createTextField("category", null, 30, 15, "faculty");
addBookPanel.add(category, gbc);
gbc.gridy = 4;
JPanel publisher = createTextField("publisher", null, 30, 15, "publisher");
addBookPanel.add(publisher, gbc);
gbc.gridy = 5;
gbc.insets = new Insets(15, -140, 10, 0);
JPanel quantity = createTextField("quantity", null, 13, 15, "quantity");
addBookPanel.add(quantity, gbc);
gbc.gridy = 6;
gbc.insets = new Insets(10, 0, 30, 10);
RoundedButton addBookButton = createButton(30, 17, "Add book", 120, 30,
colorBackground, hoverColor, false);
addBookButton.setActionCommand("addBook");
addBookButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String title = getTextFromField("title");
String author = getTextFromField("author");
String category = getTextFromField("category");
String publisher = getTextFromField("publisher");
String quantity = getTextFromField("quantity");
String price = getTextFromField("price");
try {
Document document = new Document(title, category, author,
publisher, Integer.parseInt(quantity),
Long.parseLong(price));
DatabaseConnection.insertDocument(document);
InitInformation.documents =
DatabaseConnection.getAllDocuments();
loadBooksToTable(InitInformation.documents, model);
JOptionPane.showMessageDialog(null, "Add book successfully",
"Success",
JOptionPane.INFORMATION_MESSAGE);
bookCardLayout.show(bookContentPane, "list");
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Price and quantity must be
numbers", "Error",
JOptionPane.ERROR_MESSAGE);
bookCardLayout.show(bookContentPane, "list");
}
}
});
addBookPanel.add(addBookButton, gbc);
bookAddPanel.add(addBookPanel, gbc);
bookContentPane.add(bookListPanel, "list");
bookContentPane.add(bookAddPanel, "addBook");
bookPanel.add(bookContentPane, BorderLayout.CENTER);
detailContentPane.setBackground(Color.WHITE);
detailContentPane.add(infoPanel, "userPanel");
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.HORIZONTAL; // Mở rộng chiều ngang
gbc.gridx = 0;
gbc.gridy = 1;
gbc.weightx = 1.0;
gbc.insets = new Insets(5, 40, 5, 0);
JPanel fullName = createTextField("Book title", null, 30, 15,
document.getTitle(), "Book title: ", false);
userPanel.add(fullName, gbc);
gbc.gridy = 2;
JPanel ID = createTextField("ID", null, 30, 15,
String.valueOf(document.getId()), "ID: ", false);
userPanel.add(ID, gbc);
gbc.gridy = 3;
JPanel author = createTextField("author", null, 30, 15,
document.getAuthor(), "Author: ", false);
userPanel.add(author, gbc);
gbc.gridy = 4;
JPanel category = createTextField("category", null, 30, 15,
document.getCategory(), "Category: ", false);
userPanel.add(category, gbc);
gbc.gridy = 5;
JPanel publisher = createTextField("publisher", null, 30, 15,
document.getPublisher(), "Publisher: ", false);
userPanel.add(publisher, gbc);
gbc.gridy = 6;
JPanel quantity = createTextField("quantity", null, 30, 15,
String.valueOf(document.getQuantity()), "Quantity: ", false);
userPanel.add(quantity, gbc);
gbc.gridy = 7;
JPanel price = createTextField("price", null, 30, 15, String.valueOf((int)
document.getPrice()) + " VND", "Price: ", false);
userPanel.add(price, gbc);
gbc.gridy = 10;
gbc.anchor = GridBagConstraints.EAST;
gbc.insets = new Insets(5, 300, 5, 0);
JButton editButton = new JButton(edit);
editButton.setBackground(colorBackground);
editButton.setOpaque(false);
editButton.setBorderPainted(false);
editButton.setFocusPainted(false);
editButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
detailCardLayout.show(detailContentPane, "displayInfoPanel");
}
});
userPanel.add(editButton, gbc);
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.HORIZONTAL; // Mở rộng chiều ngang
gbc.gridx = 0;
gbc.gridy = 1;
gbc.weightx = 1.0;
gbc.insets = new Insets(5, 40, 5, 0);
JPanel displayName = createTextField("displayName", null, 30, 15,
document.getTitle(), "Book title: ", false);
displayUserPanel.add(displayName, gbc);
gbc.gridy = 2;
JPanel displayID = createTextField("displayID", null, 30, 15,
String.valueOf(document.getId()), "ID: ", false);
displayUserPanel.add(displayID, gbc);
gbc.gridy = 3;
JPanel displayAuthor = createTextField("displayAuthor", null, 30, 15,
document.getAuthor(), "Author: ", true);
displayUserPanel.add(displayAuthor, gbc);
gbc.gridy = 4;
JPanel displayCategory = createTextField("displayCategory", null, 30, 15,
document.getCategory(), "Category: ", true);
displayUserPanel.add(displayCategory, gbc);
gbc.gridy = 5;
JPanel displayPublisher = createTextField("displayPublisher", null, 30, 15,
document.getPublisher(), "Publisher: ", true);
displayUserPanel.add(displayPublisher, gbc);
gbc.gridy = 6;
JPanel displayQuantity = createTextField("displayQuantity", null, 30, 15,
String.valueOf(document.getQuantity()), "Quantity: ", true);
displayUserPanel.add(displayQuantity, gbc);
gbc.gridy = 7;
JPanel displayPrice = createTextField("displayPrice", null, 30, 15,
String.valueOf((int) document.getPrice()) + " VND", "Price: ", true);
displayUserPanel.add(displayPrice, gbc);
gbc.gridy = 8;
gbc.insets = new Insets(5, 40, 5, 40);
RoundedButton saveButton = createButton(35, 17, "Save", 100, 35,
colorButton, hoverColor, false);
gbc.anchor = GridBagConstraints.EAST;
displayUserPanel.add(saveButton, gbc);
saveButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Retrieve input values from textFieldMap
document.setAuthor(author);
document.setCategory(category);
document.setPublisher(publisher);
document.setQuantity(Integer.parseInt(quantity));
document.setPrice(Long.parseLong(price));
userPanel.revalidate();
userPanel.repaint();
loadBooksToTable(DatabaseConnection.getAllDocuments(), model);
bookCardLayout.show(bookContentPane, "list");
}
});
bookListPanel.setBackground(Color.WHITE);
}
String[] columnNames = {"ID", "Title", "Category", "Author", "Publisher",
"Quantity", "Price"};
DefaultTableModel model = new DefaultTableModel(data, columnNames) {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
JTable table = new JTable(model);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.getColumnModel().getColumn(0).setPreferredWidth(25);
table.getColumnModel().getColumn(1).setPreferredWidth(400);
table.getColumnModel().getColumn(2).setPreferredWidth(100);
table.getColumnModel().getColumn(3).setPreferredWidth(200);
table.getColumnModel().getColumn(4).setPreferredWidth(150);
table.getColumnModel().getColumn(5).setPreferredWidth(50);
table.getColumnModel().getColumn(6).setPreferredWidth(60);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setPreferredSize(new Dimension(600, 350));
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
bookListPanel.add(scrollPane);
return bookListPanel;
}
sidebar.add(homeButton);
sidebar.add(chatButton);
sidebar.add(userButton);
sidebar.add(bookButton);
sidebar.add(manageButton);
sidebar.add(logoutButton);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setBorder(BorderFactory.createEmptyBorder()); // Bỏ viền của
JScrollPane
scrollPane.getViewport().setBackground(Color.WHITE); // Đặt màu nền cho
viewport của JScrollPane
notificationPanel.add(infoPanel);
notificationContainer.add(Box.createRigidArea(new Dimension(0,
10)));
notificationContainer.add(notificationPanel);
}
}
notificationContainer.revalidate();
notificationContainer.repaint();
}
if (parsedDate.before(currentDate)) {
JOptionPane.showMessageDialog(null, "Return date cannot
be in the past!");
return;
}
document.setQuantity(updatedDoc.getQuantity());
}
// Refresh borrowed books list
addBorrowedBook();
}
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setBorder(BorderFactory.createEmptyBorder()); // Loại bỏ viền
mặc định
scrollPane.getViewport().setBackground(Color.WHITE); // Đặt màu nền cho
viewport
bookPanel.add(infoPanel);
bookPanel.add(Box.createHorizontalGlue());
returnButton.addActionListener(e -> {
if (user.returnDocument(document, borrowedDoc.getQuantity(),
InitInformation.library)) {
borrowedBookContainer.remove(bookPanel);
borrowedBookContainer.revalidate();
borrowedBookContainer.repaint();
}
});
bookPanel.add(returnButton);
borrowedBookContainer.add(bookPanel);
borrowedBookContainer.add(Box.createRigidArea(new Dimension(0,
10)));
}
borrowedBookContainer.revalidate();
borrowedBookContainer.repaint();
}
}
}
originalIcon0 = new ImageIcon("lma\\src\\resource\\edit.png"); //
SửaaddUserPanelại đường dẫn nếu
scaledImage0 = originalIcon0.getImage().getScaledInstance(25, 25,
Image.SCALE_SMOOTH);
edit = new ImageIcon(scaledImage0);
if (edit.getIconWidth() == -1) {
System.out.println("Hình ảnh không thể tải ở adminUI .");
if (icon != null) {
JLabel textLabel = new JLabel(icon);
textPanel.add(textLabel, BorderLayout.WEST);
}
textPanel.setOpaque(false);
textPanel.add(textField, BorderLayout.CENTER);
return textPanel;
}
if (icon != null) {
JLabel iconLabel = new JLabel(icon);
labelPanel.add(Box.createRigidArea(new Dimension(5, 0))); // khoảng
cách giữa label và icon
labelPanel.add(iconLabel);
}
textPanel.setOpaque(false);
textPanel.add(labelPanel, BorderLayout.WEST); // Thêm label panel vào WEST
textPanel.add(textField, BorderLayout.CENTER); // Text field ở CENTER
return textPanel;
}
@Override
public void actionPerformed(ActionEvent e) {
JButton currentButton = (JButton) e.getSource(); // Nút được bấm
// Khôi phục màu nút trước đó (nếu có và không phải nút hiện tại)
if (selectedButton != null && selectedButton != currentButton) {
selectedButton.setBackground(colorBackground); // Màu nền gốc
selectedButton.setOpaque(false);
}
break;
case "managepanel":
cardLayout.show(contentPane, "borrowedBookList");
break;
}
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// Vẽ ImageIcon tại vị trí (x, y)
images[currentImageIndex].paintIcon(this, g, x, y);
}
};
return panel;
}