Java Reference
In-Depth Information
203 if (lsm.isSelectionEmpty()) {
204 view.setEnabled(false);
205 } else {
206 view.setEnabled(true);
207 }
However, we start with the
View Subaccount
button disabled until the
user has created and selected some subaccounts worth viewing.
16.7.2.6
There is one other button on the BudgetPro application, one that is not located
in this bottom panel of buttons. It's the one on the status line. It, too, starts up
disabled or grayed out—but it has an image in it. Any
JButton
can contain
either text or an image, or both, but we've chosen to do just one or the other
in our application. We declare it like any other button:
The
createStatus()
Revisited
private JButton upton;
but for its initialization we use a variation of the
JButton
constructor, one that
takes an
ImageIcon
object as its parameter:
upton = new JButton(new ImageIcon("net/multitool/gui/back.gif"));
Why do we do that all in one line? When you read it, you can certainly
think of it as two steps:
ImageIcon backup = new ImageIcon("net/multitool/gui/back.gif");
upton = new JButton(backup);
but we have no other need for the image, so we don't need to keep a reference
for 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 the
JButton
's constructor to show that we're making no other use of the image.
Which style do you prefer?
And what about a button that needs to contain both text and an image?
There is a constructor that takes both a
String
and an
ImageIcon
. Then
you can set certain attributes of the
JButton
to position the text
relative to the image. Look in the Javadoc of
JButton
for the methods
setVerticalTextPosition()
and
setHorizontalTextPosition()
.