Java Reference
In-Depth Information
do here because it can't be any other type of event—or we wouldn't have been
called. All we're doing with it is checking to see if the user just selected or dese-
lected something. The only action we take is to enable or disable the view
button.
Deep inside the cdaction object is a line that does the real action that
we're after with our selection. It says:
61 int row = list.getSelectedRow();
This shows that a JTable (list) has a method, getSelectedRow() , which
will return the row number of the row that the user has selected (that is,
clicked on). This is all part of the action listener (defined on lines 54-75 of
BudgetPro) for the View Subaccount button.
54 private ActionListener cdAction = new ActionListener()
55 {
56 public void
57 actionPerformed(ActionEvent e)
58 {
59 // this is the action for VIEW subdirectory;
60 // a "cd" into the subaccount.
61 int row = list.getSelectedRow();
62 // System.out.println("Row="+row); // DEBUG; TODO: REMOVE
63 if (row > -1) { // only if a row was selected
64 String subname = (String) model.getValueAt(row, 0);
// name column
65 Account next = current.getSub(subname);
66 if (next != null) {
67 current = next;
68 // System.out.println("cd to:"+current.getName());
69 setStatus();
70 // notify the table, too
71 model.fireTableDataChanged();
72 } // TODO: else infodialog or Beep.
73 }
74 }
75 } ;
With the row number in hand, the actionPerformed() method can
then use the row number to look up the account name. Since the account name
is in the first column (numbered 0) of our table, we call getValueAt(row, 0)
to get that name. Then we give the name to the current account to look up the
subaccount (line 65).
Search WWH ::




Custom Search