You are on page 1of 8

/*

* To change this license header, choose License Headers in Project Properties.


* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package dssach;

import dssach.Book;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
*
* @author Snorlax
*/
public class BookFrame extends javax.swing.JFrame {
DefaultTableModel tableModel;

List<Book> bookList = new ArrayList<>();

/**
//// * Creates new form BookFrame
*/

public BookFrame() {
initComponents();
tableModel = (DefaultTableModel) tblBook.getModel();
showBook();
}

private void showBook(){

bookList = BookModify.findAll();

tableModel.setRowCount(0);

bookList.forEach((book) -> {
tableModel.addRow(new Object[] {tableModel.getRowCount() +
1,book.getMaSach(),
book.getTuaSach(),book.getTacGia(),book.getNhaXuatBan()});
});

}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-
BEGIN:initComponents
private void initComponents() {
jTextField8 = new javax.swing.JTextField();
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
txtTuaSach = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
txtMaSach = new javax.swing.JTextField();
txtTacGia = new javax.swing.JTextField();
txtNhaXuatBan = new javax.swing.JTextField();
jLabel7 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
jLabel9 = new javax.swing.JLabel();
btnSave = new javax.swing.JButton();
btnDelete = new javax.swing.JButton();
btnThoat = new javax.swing.JButton();
jLabel10 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
tblBook = new javax.swing.JTable();

jTextField8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField8ActionPerformed(evt);
}
});

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Nhập
thông tin sách", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0,
18))); // NOI18N
jPanel1.setToolTipText("");

jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("Tên Sách");

txtTuaSach.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtTuaSachActionPerformed(evt);
}
});

jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel2.setText("Mã Sách");

txtMaSach.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtMaSachActionPerformed(evt);
}
});

txtTacGia.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtTacGiaActionPerformed(evt);
}
});

txtNhaXuatBan.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtNhaXuatBanActionPerformed(evt);
}
});

jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel7.setText("Nhà Xuất Bản");

jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel8.setText("Tác Giả");

jLabel9.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N


jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel9.setText("DANH SÁCH");

btnSave.setText("LƯU");
btnSave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSaveActionPerformed(evt);
}
});

btnDelete.setText("XÓA");
btnDelete.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnDeleteActionPerformed(evt);
}
});

btnThoat.setText("THOÁT");
btnThoat.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnThoatActionPerformed(evt);
}
});

jLabel10.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N


jLabel10.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel10.setText("CHỨC NĂNG");

tblBook.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Mã Sách", "Tên Sách", "Tác Giả", "Nhà Xuất Bản"
}
) {
boolean[] canEdit = new boolean [] {
false, false, false, false
};
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
jScrollPane1.setViewportView(tblBook);

javax.swing.GroupLayout jPanel1Layout = new


javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(

jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAIL
ING)
.addComponent(jLabel9,
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING,
jPanel1Layout.createSequentialGroup()
.addComponent(jLabel10)
.addGap(48, 48, 48)
.addComponent(btnSave,
javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(54, 54, 54)
.addComponent(btnDelete,
javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(52, 52, 52)
.addComponent(btnThoat,
javax.swing.GroupLayout.PREFERRED_SIZE, 73,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING,
jPanel1Layout.createSequentialGroup()

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADI
NG)
.addComponent(jLabel1,
javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2,
javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel8,
javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel7,
javax.swing.GroupLayout.PREFERRED_SIZE, 87,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(26, 26, 26)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADI
NG)
.addComponent(txtNhaXuatBan,
javax.swing.GroupLayout.PREFERRED_SIZE, 410,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtTacGia,
javax.swing.GroupLayout.PREFERRED_SIZE, 410,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtMaSach,
javax.swing.GroupLayout.PREFERRED_SIZE, 410,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtTuaSach,
javax.swing.GroupLayout.PREFERRED_SIZE, 410,
javax.swing.GroupLayout.PREFERRED_SIZE))))
.addContainerGap(24, Short.MAX_VALUE))
.addComponent(jScrollPane1)
);
jPanel1Layout.setVerticalGroup(

jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASEL
INE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE,
26, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtTuaSach,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASEL
INE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE,
26, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtMaSach,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASEL
INE)
.addComponent(txtTacGia,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE,
26, javax.swing.GroupLayout.PREFERRED_SIZE))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASEL
INE)
.addComponent(txtNhaXuatBan,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE,
26, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASEL
INE)
.addComponent(btnSave)
.addComponent(btnDelete)
.addComponent(btnThoat)
.addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE,
40, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 40,
javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE,
207, Short.MAX_VALUE)
.addContainerGap())
);

javax.swing.GroupLayout layout = new


javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);

pack();
}// </editor-fold>//GEN-END:initComponents

private void txtTuaSachActionPerformed(java.awt.event.ActionEvent evt) {//GEN-


FIRST:event_txtTuaSachActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_txtTuaSachActionPerformed

private void txtMaSachActionPerformed(java.awt.event.ActionEvent evt) {//GEN-


FIRST:event_txtMaSachActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_txtMaSachActionPerformed

private void txtTacGiaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-


FIRST:event_txtTacGiaActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_txtTacGiaActionPerformed

private void jTextField8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-


FIRST:event_jTextField8ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jTextField8ActionPerformed

private void txtNhaXuatBanActionPerformed(java.awt.event.ActionEvent evt)


{//GEN-FIRST:event_txtNhaXuatBanActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_txtNhaXuatBanActionPerformed
private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-
FIRST:event_btnSaveActionPerformed
// TODO add your handling code here:
String MaSach = txtMaSach.getText();
String TuaSach = txtTuaSach.getText();
String TacGia = txtTacGia.getText();
String NhaXuatBan = txtNhaXuatBan.getText();

Book bk = new Book(TacGia,NhaXuatBan,TuaSach,MaSach);


BookModify.insert(bk);
showBook();
}//GEN-LAST:event_btnSaveActionPerformed

private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-


FIRST:event_btnDeleteActionPerformed

int selectedIndex = tblBook.getSelectedRow();


if(selectedIndex>=0){
Book bk=bookList.get(selectedIndex);

int option = JOptionPane.showConfirmDialog(this, "Ban co muon xoa sach nay ?");


System.out.println("option: "+option);

}
// TODO add your handling code here:
}//GEN-LAST:event_btnDeleteActionPerformed

private void btnThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-


FIRST:event_btnThoatActionPerformed
this.dispose(); // TODO add your handling code here:
}//GEN-LAST:event_btnThoatActionPerformed

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code
(optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the
default look and feel.
* For details see
http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {

java.util.logging.Logger.getLogger(BookFrame.class.getName()).log(java.util.logging
.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(BookFrame.class.getName()).log(java.util.logging
.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(BookFrame.class.getName()).log(java.util.logging
.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(BookFrame.class.getName()).log(java.util.logging
.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new BookFrame().setVisible(true);
}
});
}

// Variables declaration - do not modify//GEN-BEGIN:variables


private javax.swing.JButton btnDelete;
private javax.swing.JButton btnSave;
private javax.swing.JButton btnThoat;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField jTextField8;
public javax.swing.JTable tblBook;
private javax.swing.JTextField txtMaSach;
private javax.swing.JTextField txtNhaXuatBan;
private javax.swing.JTextField txtTacGia;
private javax.swing.JTextField txtTuaSach;
// End of variables declaration//GEN-END:variables
}

You might also like