この課題では、従業員レコードを削除するように アプリケーションをプログラムします。 次のリストは、アプリケーションに使用させたい動作を説明しています。
この動作を追加するには、この課題で概要が示される次の手順を行います。
「削除」ボタンを使用可能または使用不可になるようにプログラムするには、 行が選択されたときにボタンを使用可能にするリスナーをテーブルに追加します。
employeesTable = new JTable();
employeesTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { getDeleteButton().setEnabled(getEmployeesTable().getSelectedRowCount() != 0); } });
import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener;これで、テーブル内の行を選択すると、「削除」ボタンが使用可能になります。
actionPerformed イベントを「削除」ボタンに追加して、 「削除の確認」ダイアログ・ボックスが開くように、イベントをプログラムします。
deleteButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed() } });
deleteButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { getConfirmDialog().setVisible(true); } });
「削除の確認」ダイアログのテキスト・フィールドをバインドして、 削除する従業員のファーストネームを表示します。
これで、employeesTable の選択された行にある firstName 列にテキスト・フィールドがバインドされます。
次の手順で、Web サービス上で removeEmployee(java.lang.Integer) メソッドを呼び出すように、 「はい」ボタンをバインドします。
このコンポーネント状態は、その状態を変更する必要がないため、 「はい」ボタンが常時使用可能であることを意味します。
このステップでは、イベントを (「はい」ボタンそのものにではなく) 「はい」ボタンのバインダー に追加します。 従業員が除去された後、「削除の確認」ダイアログ・ボックスが閉じるようにしたいということは、 バインダーが正常にサービスをデータ・ソースに呼び出した後で、それを行いたいということを意味します。
イベントを「はい」ボタンのバインダー にバインドして、 バインダーがアクションを終了した後で「削除の確認」ダイアログを非表示にします。
重要: イベントは、ボタンそのものに追加するのではなく、ボタンのバインダーに追加します。
removeEmployeeAction.addActionBinderListener(new jve.generated.IActionBinder.ActionBinderListener() { public void afterActionPerformed(jve.generated.IActionBinder.ActionBinderEvent e) { System.out.println("afterActionPerformed()"); // TODO Auto-generated Event stub afterActionPerformed() } public void beforeActionPerformed(jve.generated.IActionBinder.ActionBinderEvent e) {} });
removeEmployeeAction.addActionBinderListener(new jve.generated.IActionBinder.ActionBinderListener() { public void afterActionPerformed(jve.generated.IActionBinder.ActionBinderEvent e) { getConfirmDialog().setVisible(false); } public void beforeActionPerformed(jve.generated.IActionBinder.ActionBinderEvent e) {} });
このイベント・コードにより、バインダーのアクションが実行されると「削除の確認」ダイアログ・ボックスが非表示になります。
これで、 アプリケーションを実行したときに、 テーブル内で従業員を選択して、「削除」ボタンをクリックし、 「はい」をクリックして削除を確認できます。 従業員レコードは、この登録簿から除去され、従業員のリストは除去を反映したものになります。