Tạo bởi Trần Văn Điêp|
Hoàn thiện dự án quản lý sinh viên - java swing
Java Basic
Hoàn thiện dự án quản lý sinh viên - java swing
Yêu cầu viết chương trình như sau

Thực hiện sửa lại chức năng Save => Khi RollNo đã tồn tại thì cập nhật thông tin sinh viên đó trên Table
Khi người dùng click vào button Search thực hiện hiển thị dialog cho phép nhận Roll No. Tìm kiếm thông tin sinh viên theo RollNo. Nếu ko thấy hiển thị dialog thông báo không tìm thấy sinh viên nào có mã rollNo vừa nhập
TH tìm thấy hiển thị thông tin sinh viên trên form nhập trên
Code theo dự án sau
/*
* 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 lession13;
/**
*
* @author Diep.Tran
*/
public class Student {
String rollno, fullname, gender;
public Student() {
}
public Student(String rollno, String fullname, String gender) {
this.rollno = rollno;
this.fullname = fullname;
this.gender = gender;
}
public String getRollno() {
return rollno;
}
public void setRollno(String rollno) {
this.rollno = rollno;
}
public String getFullname() {
return fullname;
}
public void setFullname(String fullname) {
this.fullname = fullname;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
/*
* 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 lession13;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
/**
*
* @author Diep.Tran
*/
public class MainFrame extends javax.swing.JFrame {
DefaultTableModel tableModel;
List<Student> studentList = new ArrayList<>();
/**
* Creates new form MainFrame
*/
public MainFrame() {
initComponents();
tableModel = (DefaultTableModel) tblStudent.getModel();
}
/**
* 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">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
txtRollNo = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
txtFullName = new javax.swing.JTextField();
cbGender = new javax.swing.JComboBox<>();
jLabel3 = new javax.swing.JLabel();
btnSave = new javax.swing.JButton();
btnReset = new javax.swing.JButton();
btnSearch = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
tblStudent = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("C1907L Tutorial");
jPanel1.setBackground(new java.awt.Color(204, 255, 204));
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Input Student's Detail Information"));
jPanel1.setForeground(new java.awt.Color(0, 153, 255));
jLabel1.setText("Roll No:");
jLabel2.setText("Full Name:");
cbGender.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Male", "Female" }));
jLabel3.setText("Gender:");
btnSave.setText("Save");
btnSave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSaveActionPerformed(evt);
}
});
btnReset.setText("Reset");
btnReset.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnResetActionPerformed(evt);
}
});
btnSearch.setText("Search");
btnSearch.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSearchActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(39, 39, 39)
.addComponent(txtRollNo, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(cbGender, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtFullName, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnSave, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnReset, javax.swing.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE)
.addComponent(btnSearch, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(9, 9, 9)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(txtRollNo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnSave))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(txtFullName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnReset))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(cbGender, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3)
.addComponent(btnSearch))
.addContainerGap(18, Short.MAX_VALUE))
);
tblStudent.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"No", "Roll No", "Full Name", "Gender"
}
) {
boolean[] canEdit = new boolean [] {
false, false, true, false
};
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
jScrollPane1.setViewportView(tblStudent);
if (tblStudent.getColumnModel().getColumnCount() > 0) {
tblStudent.getColumnModel().getColumn(0).setResizable(false);
tblStudent.getColumnModel().getColumn(1).setResizable(false);
tblStudent.getColumnModel().getColumn(2).setResizable(false);
tblStudent.getColumnModel().getColumn(3).setResizable(false);
}
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 476, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.out.println("Click save button");
String rollNo = txtRollNo.getText();
String fullname = txtFullName.getText();
String gender = cbGender.getSelectedItem().toString();
if(rollNo.isEmpty() || fullname.isEmpty()) {
System.out.println("Enter fullname & rollno");
JOptionPane.showMessageDialog(rootPane, "Enter fullname & rollno");
return;
}
for (Student student : studentList) {
if(student.getRollno().equalsIgnoreCase(rollNo)) {
// JOptionPane.showMessageDialog(rootPane, "RollNo existed");
//update student theo du lieu moi
updateTableStudent();
return;
}
}
Student std = new Student(rollNo, fullname, gender);
studentList.add(std);
System.out.println("rollNo : " + rollNo + ", fullname: " + fullname + ", gender : " + gender);
tableModel.addRow(new Object[] {tableModel.getRowCount() + 1, rollNo, fullname, gender});
}
private void updateTableStudent() {
//Xoa du lieu trong Table
//hien thi lai thong tin sinh vien tren Table
//Xu dung 1 vong for duyet qua tat ca cac phan tu trong mang studentList
}
private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.out.println("Click reset button");
txtRollNo.setText("");
txtFullName.setText("");
cbGender.setSelectedIndex(0);
}
private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//Viet them chuc nang nay.
}
/**
* @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(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MainFrame.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 MainFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnReset;
private javax.swing.JButton btnSave;
private javax.swing.JButton btnSearch;
private javax.swing.JComboBox<String> cbGender;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable tblStudent;
private javax.swing.JTextField txtFullName;
private javax.swing.JTextField txtRollNo;
// End of variables declaration
}