Java Reference
In-Depth Information
244 ActionListener creatAction = new ActionListener()
245 {
246 public void
247 actionPerformed(ActionEvent e)
248 {
249 Account child;
250 // get the info via a Dialog (of sorts)
251 if (askem == null) {
252 askem = new AcctDialog(frame, "New Subaccount");
253 } else {
254 askem.clear();
255 askem.setVisible(true);
256 }
257 String subName = askem.getName();
258 String subAmnt = askem.getAmnt();
259
260 // if empty, assume the operation was cancelled, else:
261 if ((subName != null) && (subName.length() > 0)) {
262 child = current.createSub(subName, subAmnt);
263 setStatus();
264 model.fireTableDataChanged(); // notify the table
265 }
266 }
267 };
268 creat.addActionListener(creatAction);
We defined the action for the View Subaccount button (as we said you
could) elsewhere in the program. Its action is defined in lines 54-75. Then on
line 271 we connect the action to the button. (We'll get back to this button's
action, too, once we've discussed the JTable .) But after we've attached the ac-
tion, we also disable the button (line 273).
270 // function is to get selection from table and cd there
271 view.addActionListener(cdAction);
272 // but it starts off disabled, since there is no data yet
273 view.setEnabled(false);
In Swing, a button is either enabled or disabled. Enabled buttons are the
active ones on which you can click. Disabled buttons are grayed out and not
responsive to clicks. We can make a button either active or inactive with a
method on the button called setEnabled() whose argument is a
boolean true to enable the button, false to disable it. For example:
Search WWH ::




Custom Search