private void updateView() { if (employee != null) { txfName.setText(employee.getName()); txfWage.setText("" + employee.getWage()); Company companyOfEmployee = employee.getCompany(); if (companyOfEmployee == null) cbxCompanies.setSelectedIndex(0); else { int index = 0; boolean found = false; int i = 0; while (!found && i < cbxCompanies.getItemCount()) { Company company = cbxCompanies.getItemAt(i); if (company == companyOfEmployee) { found = true; index = i; } else i++; } cbxCompanies.setSelectedIndex(index); } } else { txfName.setText(""); txfWage.setText(""); cbxCompanies.setSelectedIndex(0); } }
// This method is called when a button is pressed. public void actionPerformed(ActionEvent e) { if (e.getSource() == btnOk) { String name = txfName.getText().trim(); if (name.length() == 0) { // TODO: notify user return; } String wageStr = txfWage.getText().trim(); if (wageStr.length() == 0) { // TODO: notify user return; } int wage = -1; try { wage = Integer.parseInt(wageStr); } catch (NumberFormatException ex) { // TODO: notify user return; } if (wage < 0) { // TODO: notify user return; } Company company = null; if (cbxCompanies.getSelectedIndex() >= 1) { company = (Company) cbxCompanies.getItemAt(cbxCompanies.getSelectedIndex()); } /** ** update storage *** */ if (employee == null) { Employee newEmployee = service.createEmployee(name, wage); if (company != null) service.updateCompanyOfEmployee(newEmployee, company); } else { if (!employee.getName().equals(name) || employee.getWage() != wage) service.updateEmployee(employee, name, wage); if (employee.getCompany() != company) service.updateCompanyOfEmployee(employee, company); } closedByOk = true; EmployeeCUDialog.this.setVisible(false); } if (e.getSource() == btnCancel) { closedByOk = false; EmployeeCUDialog.this.setVisible(false); } }