13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

366Chapter 16Getting in the Swing of Things: Designing a GUI for BudgetProThis call tells our table (list) that we want to allow the user to select <strong>on</strong>lya single row or column at a time. Valid opti<strong>on</strong>s are:ListSelecti<strong>on</strong>Model.SINGLE_SELECTIONListSelecti<strong>on</strong>Model.SINGLE_INTERVAL_SELECTIONListSelecti<strong>on</strong>Model.MULTIPLE_INTERVAL_SELECTIONThe latter two allow the user to select more than <strong>on</strong>e row at a time; multipleintervals mean that the selected rows can be disc<strong>on</strong>tinuous. (Think“Shift+click” versus “C<strong>on</strong>trol+click” as the user acti<strong>on</strong> that selects these.)So what will our program do, <strong>on</strong>ce the user has made a selecti<strong>on</strong>? The selectedrow is a subaccount of the current account and we will allow the user todisplay that account and its subaccount, if any. Think of it as “changingdirectory” into that account, to look at or change its status.For a table to take an acti<strong>on</strong> when a selecti<strong>on</strong> is made you need anotherlistener called a selecti<strong>on</strong> listener. We wrote:196 list.getSelecti<strong>on</strong>Model().addListSelecti<strong>on</strong>Listener(197 new ListSelecti<strong>on</strong>Listener()198 {199 public void200 valueChanged(ListSelecti<strong>on</strong>Event e)201 {202 ListSelecti<strong>on</strong>Model lsm =(ListSelecti<strong>on</strong>Model)e.getSource();203 if (lsm.isSelecti<strong>on</strong>Empty()) {204 view.setEnabled(false);205 } else {206 view.setEnabled(true);207 }208 } // valueChanged209 }210 );Similar to how a table has a table model behind it, it also has a selecti<strong>on</strong>model behind it. We d<strong>on</strong>’t need to reimplement an entire selecti<strong>on</strong> model; wejust retrieve the default <strong>on</strong>e from our table (list.getSelecti<strong>on</strong>Model())and add a listener to it so that it will notify us when something has changed.The javax.swing.event.ListSelecti<strong>on</strong>Listener is an interface with<strong>on</strong>ly <strong>on</strong>e method, so it’s easy to extend and override it in place, as we do, beginningat line 197. When called, it will be handed an event (e) and we take thesource of that event and coerce it to a ListSelecti<strong>on</strong>Model. That’s safe to

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!