Home
Completing the BankDesktopController class
In this section we add control logic to the BankDesktopController class, as well as the code to look up customer and account information from the EJB application.
Note: The code found in this section can be copied from the complete BankDesktopController class that is supplied in the sample code:
c:\7672code\appclient\BankDesktopController.javaWe suggest that you copy the sections as noted in our procedure from the completed BankDesktopController.java (step by step).
To complete the BankDesktopController class, do these steps:
The BankDesktopController.java is open in the Java editor.
The client invokes methods of the EJBBankBean session bean through its remote interface. Inject the remote interface of the session bean: public class BankDesktopController implements ActionListener {
@EJB(name="ejb/bank",beanInterface=EJBBankRemote.class)
static EJBBankRemote bank;
After every step, select Source Æ Organize Imports (or press Ctrl+Shift+O) to generate the required import statement.
Add two fields to the beginning of the class definition: private BankDesktop desktop = null;
private AccountTableModel accountTableModel = null;
Locate the constructor and add the code (the new code is in bold): public BankDesktopController() {
desktop = new BankDesktop();
accountTableModel = new AccountTableModel();
desktop.getTblAccounts().setModel(accountTableModel);
desktop.getBtnSearch().addActionListener(this);
desktop.setVisible(true);
}
Locate the main method, add the throws clause, and add three lines: public static void main(String[] args) throws Exception {
BankDesktopController controller = new BankDesktopController();
// without the next line, app client fails in IDE, works outside
bank.getAccounts("xxx");
}
Locate the actionPerformed method stub and complete the method as shown in Example | 7-1. Example 17-1 Complete actionPerformed method
public void actionPerformed(ActionEvent e) {// we know that we are only listening to action events from// the search button, so...String ssn = desktop.getTfSSN().getText();try {// look up the customerCustomer customer = bank.getCustomer(ssn);if (customer == null) throw new ITSOBankException("Customer not found: " + ssn);// look up the accountsAccount[] accounts = bank.getAccounts(ssn);// update the user interfacedesktop.getTfTitle().setText(customer.getTitle());desktop.getTfFirstName().setText(customer.getFirstName());desktop.getTfLastName().setText(customer.getLastName());// store the accounts in the table model and set the model in the GUIaccountTableModel.setAccounts(accounts);} catch (ITSOBankException x) {// unknown customer. Report this using the output fields...desktop.getTfTitle().setText("(not found)");desktop.getTfFirstName().setText("(not found)");desktop.getTfLastName().setText("(not found)");accountTableModel.setAccounts(new Account[0]);}}
Save and close the BankDesktopController.
ibm.com/redbooks