当我点击保存按钮时我有内部jframe 员工列表和jdialog AddEditEmployee 我希望员工列表中的刷新jtable 然后 AddEditEmployee 已清除。
我正在尝试在保存按钮actionListener中调用 isTableModel 方法,但它不起作用。
请参阅下面的screenshot here和代码:
员工控制器
public class EmployeeController {
private EmployeeList employeeList_view;
private AddEditEmployee addEditEmployee_view;
private ImplementEmployeeList implementEmployeeList;
private List<Employees> employees;
public EmployeeController(EmployeeList employeeList_view) {
this.employeeList_view = employeeList_view;
implementEmployeeList = new EmployeeDAO();
employees = implementEmployeeList.getEmployeeList();
}
public EmployeeController(AddEditEmployee addEditEmployee_view){
this.addEditEmployee_view = addEditEmployee_view;
implementEmployeeList = new EmployeeDAO();
employees = implementEmployeeList.getEmployeeList();
}
public void reset(){
addEditEmployee_view.getJtfEmployeeID().setText("");
addEditEmployee_view.getJtfFirstName().setText("");
addEditEmployee_view.getJtfLastName().setText("");
}
public void isTableModel(){
employees = implementEmployeeList.getEmployeeList();
EmployeeTableModel employeeTableModel = new EmployeeTableModel(employees);
employeeList_view.getJtbEmployeeList().setModel(employeeTableModel);
}
public void insert(){
Employees emp = new Employees();
Functions func = new Functions();
emp.setEmployee_code(Integer.parseInt(addEditEmployee_view.getJtfEmployeeID().getText()));
emp.setFirst_name(addEditEmployee_view.getJtfFirstName().getText());
emp.setLast_name(addEditEmployee_view.getJtfLastName().getText());
emp.setGender(addEditEmployee_view.getJcbGender().getSelectedItem().toString());
emp.setDate_of_birth(addEditEmployee_view.getDateOfBirth().getDate());
emp.setHire_date(addEditEmployee_view.getHireDate().getDate());
emp.setMarital_status(addEditEmployee_view.getJcbMaritalStatus().getSelectedItem().toString());
emp.setPhoto(func.convertBufferedImageToBlob(addEditEmployee_view.getJlbPhoto()));
implementEmployeeList.insert(emp);
}
public void update(){
Employees emp = new Employees();
//emp.setEmployee_code(Integer.parseInt(addEditEmployee_view.getJtfEmployeeID().getText()));
emp.setFirst_name(addEditEmployee_view.getJtfFirstName().getText());
emp.setLast_name(addEditEmployee_view.getJtfLastName().getText());
emp.setGender(addEditEmployee_view.getJcbGender().getSelectedItem().toString());
emp.setDate_of_birth(addEditEmployee_view.getDateOfBirth().getDate());
emp.setHire_date(addEditEmployee_view.getHireDate().getDate());
emp.setMarital_status(addEditEmployee_view.getJcbMaritalStatus().getSelectedItem().toString());
implementEmployeeList.update(emp);
}
public void delete(){
implementEmployeeList.delete((Integer) employeeList_view.getJtbEmployeeList().getValueAt(employeeList_view.getJtbEmployeeList().getSelectedRow(),1));
}
} 员工TableModel
public class EmployeeTableModel extends AbstractTableModel {
List<Employees> employeesList;
public EmployeeTableModel(List<Employees> employeesList) {
this.employeesList = employeesList;
}
@Override
public int getRowCount() {
return employeesList.size();
}
@Override
public int getColumnCount() {
return 12;
}
@Override
public String getColumnName(int column) {
switch (column){
case 0: return "No";
case 1: return "Employee ID";
case 2: return "First Name";
case 3: return "Last Name";
case 4: return "Gender";
case 5: return "Date of Birth";
case 6: return "Marital Status";
case 7: return "Hire Date";
case 8: return "Address";
case 9: return "Telephone";
case 10: return "Email";
case 11: return "Notes";
default: return null;
}
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
switch (columnIndex){
case 0: return employeesList.get(rowIndex).getEmployee_id();
case 1: return employeesList.get(rowIndex).getEmployee_code();
case 2: return employeesList.get(rowIndex).getFirst_name();
case 3: return employeesList.get(rowIndex).getLast_name();
case 4: return employeesList.get(rowIndex).getGender();
case 5: return employeesList.get(rowIndex).getDate_of_birth();
case 6: return employeesList.get(rowIndex).getMarital_status();
case 7: return employeesList.get(rowIndex).getHire_date();
case 8: return employeesList.get(rowIndex).getAddress();
case 9: return employeesList.get(rowIndex).getTelephone();
case 10: return employeesList.get(rowIndex).getEmail();
case 11: return employeesList.get(rowIndex).getNotes();
default: return null;
}
}
}
AddEditEmployee_View
public class AddEditEmployee extends JDialog implements ActionListener {
private JPanel jpParents;
private JPanel jpProfilePicture;
private JLabel jlbPhoto;
private JButton jbBrowse;
private JTextField jtfEmployeeID;
private JTextField jtfFirstName;
private JTextField jtfLastName;
private JComboBox jcbGender;
private JComboBox jcbMaritalStatus;
private JTextField jtfAddress;
private JComboBox jcbProvince;
private JTextField jtfPostalCode;
private JTextField jtfPhone;
private JComboBox jcbDistrict;
private JTextField jtfEmail;
private JTextArea jtaNotes;
private JButton jbSave;
private JPanel jpBirthDay;
private JPanel jpHireDate;
private JDateChooser dateOfBirth;
private JDateChooser hireDate;
private enum gender {Male, Female}
private enum marital {Single, Married, devise}
private Functions func;
private EmployeeController employeeController;
private Image image = null;
public JButton getJbBrowse() {
return jbBrowse;
}
public JTextField getJtfEmployeeID() {
return jtfEmployeeID;
}
public JTextField getJtfFirstName() {
return jtfFirstName;
}
public JTextField getJtfLastName() {
return jtfLastName;
}
public JComboBox getJcbGender() {
return jcbGender;
}
public JDateChooser getDateOfBirth() {
return dateOfBirth;
}
public JDateChooser getHireDate() {
return hireDate;
}
public JComboBox getJcbMaritalStatus() {
return jcbMaritalStatus;
}
public JTextField getJtfAddress() {
return jtfAddress;
}
public JComboBox getJcbProvince() {
return jcbProvince;
}
public JTextField getJtfPostalCode() {
return jtfPostalCode;
}
public JTextField getJtfPhone() {
return jtfPhone;
}
public JComboBox getJcbDistrict() {
return jcbDistrict;
}
public JTextField getJtfEmail() {
return jtfEmail;
}
public JTextArea getJtaNotes() {
return jtaNotes;
}
public JButton getJbSave() {
return jbSave;
}
public JLabel getJlbPhoto() {
return jlbPhoto;
}
public AddEditEmployee() {
super();
setContentPane(jpParents);
setTitle("Add New Employee");
image = new ImageIcon(this.getClass().getResource("/images/newEmployee-16.png")).getImage();
setIconImage(image);
setSize(new Dimension(850, 580));
setLocationRelativeTo(null);
setOperationAction();
func = new Functions();
try {
func = new Functions();
Color color = this.getBackground();
jpProfilePicture.add(func.setCircleImage(jlbPhoto, 200, 200, "src/images/avatar.png"));
} catch (IOException e) {
e.printStackTrace();
}
dateOfBirth = new JDateChooser();
dateOfBirth.setOpaque(false);
dateOfBirth.setPreferredSize(new Dimension(165, 25));
dateOfBirth.setDateFormatString("dd/MM/yyyy");
dateOfBirth.getCalendarButton().setPreferredSize(new Dimension(24,24));
dateOfBirth.setIcon(new ImageIcon(this.getClass().getResource("/images/calendar.png")));
jpBirthDay.add(dateOfBirth);
hireDate = new JDateChooser();
hireDate.setOpaque(false);
hireDate.setPreferredSize(new Dimension(165, 25));
hireDate.setDateFormatString("dd/MM/yyyy");
hireDate.getCalendarButton().setPreferredSize(new Dimension(24,24));
hireDate.setIcon(new ImageIcon(this.getClass().getResource("/images/calendar.png")));
jpHireDate.add(hireDate);
jcbGender.setModel(new DefaultComboBoxModel(gender.values()));
jcbMaritalStatus.setModel(new DefaultComboBoxModel(marital.values()));
}
private void setOperationAction(){
jbBrowse.addActionListener(this);
jbSave.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jbBrowse){
func = new Functions();
func.browserImage(jlbPhoto, 200, 200);
} else if (e.getSource() == jbSave){
employeeController = new EmployeeController(this);
employeeController.insert();
employeeController.isTableModel();
}
}
} 的 EmployeeList_View
public class EmployeeList extends JInternalFrame implements ActionListener {
private JPanel jpParents;
private JTable jtbEmployeeList;
private JPanel jpSearchBar;
private JPanel jpOperator;
private JPanel jpOperation;
private JPanel jpPaginator;
private JButton jbAdd;
private JButton jbEdit;
private JButton jbDelete;
private EmployeeController empcontroller;
public JTable getJtbEmployeeList() {
return jtbEmployeeList;
}
public JButton getJbAdd() {
return jbAdd;
}
public JButton getJbEdit() {
return jbEdit;
}
public JButton getJbDelete() {
return jbDelete;
}
private JScrollPane jspEmployeeList;
private final String colHeader[] = {"No", "Employee ID", "First Name", "Last Name", "Gender", "Date of Birth",
"Marital Status", "Hire Date", "Address", "Telephone", "Email", "Note"};
private DefaultTableModel model;
public EmployeeList(String title, boolean resizable, boolean closable, boolean maximizable, boolean iconifiable) {
super(title, resizable, closable, maximizable, iconifiable);
setContentPane(jpParents);
setSize(850, 500);
setFrameIcon(new ImageIcon(this.getClass().getResource("/images/employee_list.png")));
JSearchTextField jtfSearch = new JSearchTextField();
jtfSearch.setColumns(15);
jtfSearch.setIcon(new ImageIcon(this.getClass().getResource("/images/search18.png")));
jtfSearch.setPreferredSize(new Dimension(30,27));
jpSearchBar.add(jtfSearch);
setOperatorAction();
empcontroller = new EmployeeController(this);
empcontroller.isTableModel();
}
private void setOperatorAction(){
jbAdd.addActionListener(this);
jbEdit.addActionListener(this);
jbDelete.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jbAdd){
new AddEditEmployee().setVisible(true);
}
}
} 的 EmployeeDAO
public class EmployeeDAO implements ImplementEmployeeList{
Connection connection;
final String insert = "INSERT INTO employees(employees_code, first_name, last_name, gender, date_of_birth, hire_date, marital_status, photo) VALUES(?,?,?,?,?,?,?,?)";
final String update = "UPDATE employees SET first_name = ?, last_name = ?, gender = ?, date_of_birth = ?, hire_date = ?, marital_status = ?, photo = ? WHERE employees_code = ?";
final String delete = "DELETE FROM employees WHERE employees_code = ?";
final String select = "SELECT * FROM employees";
final String search = "SELECT * FROM employees WHERE employees_code = ?";
public EmployeeDAO() {
this.connection = ConnectionConfiguraion.getConnection();
}
@Override
public void insert(Employees emp) {
PreparedStatement statement = null;
Functions func = new Functions();
try {
statement = connection.prepareStatement(insert, PreparedStatement.RETURN_GENERATED_KEYS);
statement.setInt(1, emp.getEmployee_code());
statement.setString(2, emp.getFirst_name());
statement.setString(3, emp.getLast_name());
statement.setString(4, emp.getGender());
statement.setDate(5, new Date(emp.getDate_of_birth().getTime()));
statement.setDate(6, new Date(emp.getHire_date().getTime()));
statement.setString(7, emp.getMarital_status());
statement.setBinaryStream(8, emp.getPhoto().getBinaryStream());
statement.executeUpdate();
JOptionPane.showMessageDialog(null, "New record was inserted successfully.", "Message", JOptionPane.INFORMATION_MESSAGE);
ResultSet rs = statement.getGeneratedKeys();
while (rs.next()){
emp.setEmployee_id(rs.getInt(1));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
@Override
public void update(Employees emp) {
PreparedStatement statement = null;
try {
statement = connection.prepareStatement(update);
//statement.setInt(1, emp.getEmployee_code());
statement.setString(1, emp.getFirst_name());
statement.setString(2, emp.getLast_name());
statement.setString(3, emp.getGender());
statement.setDate(4, (Date) emp.getDate_of_birth());
statement.setDate(5, (Date) emp.getHire_date());
statement.setString(6, emp.getMarital_status());
statement.setInt(7, emp.getEmployee_code());
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
@Override
public void delete(int id) {
PreparedStatement statement = null;
try {
statement = connection.prepareStatement(delete);
statement.setInt(1, id);
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
@Override
public List<Employees> getEmployeeList() {
List<Employees> employeesList = null;
try {
employeesList = new ArrayList<Employees>();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(select);
while (resultSet.next()){
Employees employees = new Employees();
employees.setEmployee_id(resultSet.getInt("employeesid"));
employees.setEmployee_code(resultSet.getInt("employees_code"));
employees.setFirst_name(resultSet.getString("first_name"));
employees.setLast_name(resultSet.getString("last_name"));
employees.setGender(resultSet.getString("gender"));
employees.setDate_of_birth(resultSet.getDate("date_of_birth"));
employees.setHire_date(resultSet.getDate("hire_date"));
employees.setMarital_status(resultSet.getString("marital_status"));
employees.setPhoto(resultSet.getBlob("photo"));
employeesList.add(employees);
}
} catch (SQLException e) {
e.printStackTrace();
}
return employeesList;
}
@Override
public List<Employees> getSearch(int employee_code) {
List<Employees> employeesList = null;
try {
employeesList = new ArrayList<Employees>();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(search);
while (resultSet.next()){
Employees employees = new Employees();
employees.setEmployee_id(resultSet.getInt("employeesid"));
employees.setEmployee_code(resultSet.getInt("employees_code"));
employees.setFirst_name(resultSet.getString("first_name"));
employees.setLast_name(resultSet.getString("last_name"));
employees.setGender(resultSet.getString("gender"));
employees.setDate_of_birth(resultSet.getTimestamp("date_of_birth"));
employees.setHire_date(resultSet.getTimestamp("hire_date"));
employees.setMarital_status(resultSet.getString("marital_status"));
employeesList.add(employees);
}
} catch (SQLException e) {
e.printStackTrace();
}
return employeesList;
}
}