Java Reference
In-Depth Information
The Dimension Class
Objects of the class Dimension are used with buttons, menu items, and other objects to
specify a size. The Dimension class is in the package java.awt . The parameters in the
following constructor are pixels.
CONSTRUCTOR
Dimension( int width, int height)
EXAMPLE
aButton.setPreferredSize( new Dimension(30, 50));
The setActionCommand Method
When the user clicks a button or menu item, it fires an action event that normally goes
to one or more action listeners where it becomes an argument to an actionPerformed
method. This action event includes a String instance variable that is known as the
action command for the button or menu item and that is retrieved with the accessor
method getActionCommand . The action command in the event is copied from an
instance variable in the button or menu item object. If you do nothing to change it,
the action command is the string written on the button or the menu item. The method
setActionCommand given in Display 17.15 for the class AbstractButton can be used
with any JButton or JMenuItem to change the action command for that component.
Among other things, this will allow you to have different action commands for two
buttons, two menu items, or a button and menu item even though they have the same
string written on them.
The method setActionCommand takes a String argument that becomes the new
action command for the calling button or menu item. For example, consider the
following code:
action
command
JButton nextButton = new JButton("Next");
setAction
Command
nextButton.setActionCommand("Next Button");
JMenuItem chooseNext = new JMenuItem("Next");
chooseNext.setActionCommand("Next Menu Item");
If we had not used setActionCommand in the preceding code, then the button
nextButton and the menu item chooseNext would both have the action command
"Next" and so we would have no way to tell which of the two components nextButton
and chooseNext an action event "Next" came from. However, using the method
setActionCommand, we can give them the different action commands "Next Button"
and "Next Menu Item" .
The action command for a JButton or JMenuItem is kept as the value of a private
instance variable for the JButton or JMenuItem . The method setActionCommand is
simply an ordinary mutator method that changes the value of this instance variable.
 
Search WWH ::




Custom Search