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.

358Chapter 16Getting in the Swing of Things: Designing a GUI for BudgetPro203 if (lsm.isSelecti<strong>on</strong>Empty()) {204 view.setEnabled(false);205 } else {206 view.setEnabled(true);207 }However, we start with the View Subaccount butt<strong>on</strong> disabled until theuser has created and selected some subaccounts worth viewing.16.7.2.6 The createStatus() RevisitedThere is <strong>on</strong>e other butt<strong>on</strong> <strong>on</strong> the BudgetPro applicati<strong>on</strong>, <strong>on</strong>e that is not locatedin this bottom panel of butt<strong>on</strong>s. It’s the <strong>on</strong>e <strong>on</strong> the status line. It, too, starts updisabled or grayed out—but it has an image in it. Any JButt<strong>on</strong> can c<strong>on</strong>taineither text or an image, or both, but we’ve chosen to do just <strong>on</strong>e or the otherin our applicati<strong>on</strong>. We declare it like any other butt<strong>on</strong>:private JButt<strong>on</strong> upt<strong>on</strong>;but for its initializati<strong>on</strong> we use a variati<strong>on</strong> of the JButt<strong>on</strong> c<strong>on</strong>structor, <strong>on</strong>e thattakes an ImageIc<strong>on</strong> object as its parameter:upt<strong>on</strong> = new JButt<strong>on</strong>(new ImageIc<strong>on</strong>("net/multitool/gui/back.gif"));Why do we do that all in <strong>on</strong>e line? When you read it, you can certainlythink of it as two steps:ImageIc<strong>on</strong> backup = new ImageIc<strong>on</strong>("net/multitool/gui/back.gif");upt<strong>on</strong> = new JButt<strong>on</strong>(backup);but we have no other need for the image, so we d<strong>on</strong>’t need to keep a referencefor it in a variable. Some programmers prefer to write it out in two simple steps,as it is easier to read and perhaps to maintain. We’ve chosen to put it all in theJButt<strong>on</strong>’s c<strong>on</strong>structor to show that we’re making no other use of the image.Which style do you prefer?And what about a butt<strong>on</strong> that needs to c<strong>on</strong>tain both text and an image?There is a c<strong>on</strong>structor that takes both a String and an ImageIc<strong>on</strong>. Thenyou can set certain attributes of the JButt<strong>on</strong> to positi<strong>on</strong> the textrelative to the image. Look in the Javadoc of JButt<strong>on</strong> for the methodssetVerticalTextPositi<strong>on</strong>() and setHoriz<strong>on</strong>talTextPositi<strong>on</strong>().

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

Saved successfully!

Ooh no, something went wrong!