Important: The information in this document is obsolete and should not be used for new development.
Inherits from | |
Implements | |
Package | com.apple.cocoa.application |
Companion guide |
NSButtonCell is a subclass of NSActionCell used to implement the user interfaces of push buttons, switches, and radio buttons. It can also be used for any other region of a view that’s designed to send a message to a target when clicked. The NSButton subclass of NSControl uses a single NSButtonCell.
NSButtonCell implements the user interface of NSButton.
Setting an NSButtonCell's integer, float, double, or object value results in a call to setState
with the value converted to integer. In the case of setObjectValue
, null
is equivalent to 0, and a non-null
object that doesn't respond to intValue
sets the state to 1. Otherwise, the state is set to the object's intValue
. Similarly, querying the integer, float, double, or object value of an NSButtonCell returns the current state in the requested representation. In the case of objectValue
, this is a number containing true
for on, false
for off, and integer value -1 for the mixed state.
For more information on NSButtonCell’s behavior, see the NSButton and NSMatrix class specifications.
In its implementation of the compare
method (declared in NSCell), NSButtonCell throws a BadComparisonException
if the otherCell argument is not of the NSButtonCell class.
alternateMnemonic
alternateMnemonicLocation
alternateTitle
attributedAlternateTitle
attributedTitle
setAlternateMnemonicLocation
setAlternateTitle
setAlternateTitleWithMnemonic
setAttributedAlternateTitle
setAttributedTitle
setFont
setTitle
setTitleWithMnemonic
title
keyEquivalent
keyEquivalentFont
keyEquivalentModifierMask
setKeyEquivalent
setKeyEquivalentModifierMask
setKeyEquivalentFont
setKeyEquivalentFontAndSize
backgroundColor
bezelStyle
gradientType
imageDimsWhenDisabled
isOpaque
isTransparent
setBackgroundColor
setBezelStyle
setShowsBorderOnlyWhileMouseInside
setGradientType
setImageDimsWhenDisabled
setTransparent
showsBorderOnlyWhileMouseInside
Creates an empty NSButtonCell.
public NSButtonCell
()
Creates an NSButtonCell initialized with aString.
public NSButtonCell
(String aString)
Creates an NSButtonCell initialized with anImage.
public NSButtonCell
(NSImage anImage)
If anImage is null
, no image is set.
Returns the image that appears on the receiver when it’s in its alternate state, or null
if there is no alternate image.
public NSImage alternateImage
()
Note that some button types don’t display an alternate image. Buttons don’t display images by default.
Returns the character in the alternate title (the title displayed on the receiver when it’s in its alternate state) that’s marked as the “keyboard mnemonic.”
public String alternateMnemonic
()
Mnemonics are not supported in Mac OS X.
Returns an unsigned integer indicating the character in the alternate title (the title displayed on the receiver when it’s in its alternate state) that’s marked as the “keyboard mnemonic.”
public int alternateMnemonicLocation
()
If the alternate title doesn’t have a keyboard mnemonic, NSArray.NotFound
is returned. Mnemonics are not supported in Mac OS X.
setAlternateMnemonicLocation
alternateMnemonic
setAlternateTitleWithMnemonic
mnemonicLocation
(NSCell)Returns the string that appears on the receiver when it’s in its alternate state, or the empty string if the receiver doesn’t display an alternate title.
public String alternateTitle
()
Note that some button types don’t display an alternate title. By default, a button’s alternate title is “Button.”
Returns the string that appears on the receiver when it’s in its alternate state as an NSAttributedString, or an empty attributed string if the receiver doesn’t display an alternate title.
public NSAttributedString attributedAlternateTitle
()
Note that some button types don’t display an alternate title. By default, a button’s alternate title is “Button.”
Returns the string that appears on the receiver when it’s in its normal state as an NSAttributedString, or an empty attributed string if the receiver doesn’t display a title.
public NSAttributedString attributedTitle
()
A button’s title is always displayed if the button doesn’t use its alternate contents for highlighting or displaying the alternate state. By default, a button’s title is “Button.”
Returns the background color of the button.
public NSColor backgroundColor()
The background color is used only when drawing borderless buttons.
Returns the appearance of the receiver’s border.
public int bezelStyle
()
See “Constants” for the list of the possible values.
Draws the border of the button using the current bezel style.
public void drawBezel
(NSRect frame, NSView controlView)
This method is called automatically when the button is redrawn; you should not call it directly. The frame parameter contains the bounding rectangle of the button. The controlView parameter contains the control being drawn.
Draws the image associated with the button’s current state.
public void drawImage
(NSImage image, NSRect frame, NSView controlView)
This method is called automatically when the button is redrawn; you should not call it directly. The frame parameter contains the bounding rectangle of the button. The controlView parameter contains the control being drawn.
You specify the primary and alternate images for the button using Interface Builder.
Draws the button’s title centered vertically in the specified frame rectangle.
public NSRect drawTitle
(NSAttributedString title, NSRect frame, NSView controlView)
This method is called automatically when the button is redrawn; you should not call it directly. The controlView parameter contains the control being drawn.
Returns the gradient of the receiver’s border.
public int gradientType
()
See “Constants” for the list of the possible values.
Returns the logical OR of flags that indicate the way the receiver highlights when it receives a mouse-down event.
public int highlightsBy
()
See NSCell’s “Constants” section for the list of flags.
Returns whether the receiver’s image and text appear “dim” when the receiver is disabled.
public boolean imageDimsWhenDisabled
()
By default, all button types except SwitchButton
and RadioButton
do dim when disabled. When SwitchButton
s and RadioButton
s are disabled, only the associated text dims.
Returns the position of the receiver’s image relative to its title.
public int imagePosition
()
The return value is one of the image positions described in NSCell’s “Constants” section.
If the title is above, below, or overlapping the image, or if there is no image, the text is horizontally centered within the button.
setImagePosition
setButtonType
setTitle
setImage
(NSCell)Returns true
if the receiver draws over every pixel in its frame, false
if not.
public boolean isOpaque
()
A button cell is opaque only if it isn’t transparent and if it has a border.
Returns true
if the receiver is transparent, false
otherwise.
public boolean isTransparent
()
A transparent button never draws itself, but it receives mouse-down events and tracks the mouse properly.
Returns the key-equivalent character of the receiver, or the empty string if one hasn’t been defined.
public String keyEquivalent
()
Buttons don’t have a default key equivalent.
Returns the font used to draw the key equivalent, or null
if the receiver doesn’t have a key equivalent.
public NSFont keyEquivalentFont
()
The default font is the same as that used to draw the title.
Returns the mask indicating the modifier keys that are applied to the receiver’s key equivalent.
public int keyEquivalentModifierMask
()
The only mask bits relevant in button key-equivalent modifier masks are NSEvent.ControlKeyMask
, NSEvent.AlternateKeyMask
, and NSEvent.CommandKeyMask
bits.
Draws the receiver’s border.
public void mouseEntered
(NSEvent event)
The event argument is the event object generated by the mouse movement. This method is called only when the cursor moves onto the receiver and showsBorderOnlyWhileMouseInside
returns true
.
Erases the receiver’s border.
public void mouseExited
(NSEvent event)
The event argument is the event object generated by the mouse movement. This method is called only when the cursor moves off the receiver and showsBorderOnlyWhileMouseInside
returns true
.
Simulates the user clicking the receiver with the cursor.
public void performClick
(Object sender)
This method essentially highlights the button, sends the button’s action message to the target object, and then unhighlights the button. If an exception is thrown while the target object is processing the action message, the button is unhighlighted before the exception is propagated out of performClick
.
Sets the image that appears on the receiver when it’s in its alternate state to image and, if necessary, redraws the contents of the receiver.
public void setAlternateImage
(NSImage image)
Note that some button types don’t display an alternate image.
alternateImage
setButtonType
setImage
(NSCell)Sets the character in the alternate title (the title displayed on the receiver when it’s in its alternate state) that’s to be marked as the “keyboard mnemonic.”
public void setAlternateMnemonicLocation
(int location)
If you don’t want the alternate title to have a keyboard mnemonic, specify a location of NSArray.NotFound
. Mnemonics are not supported in Mac OS X.
The setAlternateMnemonicLocation
method doesn’t cause the button cell to be redisplayed.
Sets the title that’s displayed on the receiver when it’s in its alternate state to aString.
public void setAlternateTitle
(String aString)
Note that some button types don’t display an alternate title.
alternateTitle
setAlternateMnemonicLocation
setAlternateTitleWithMnemonic
setTitle
setButtonType
setFont
Sets the title that is displayed on the receiver when it’s in its alternate state to aString, taking into account the fact that an embedded “&” character is not a literal but instead marks the alternate state’s “keyboard mnemonic.”
public void setAlternateTitleWithMnemonic
(String aString)
Mnemonics are not supported in Mac OS X.
If necessary, setAlternateTitleWithMnemonic
redraws the button cell. Note that some button types don’t display an alternate title.
Sets the string that appears on the receiver when it’s in its alternate state to the attributed string aString.
public void setAttributedAlternateTitle
(NSAttributedString aString)
Note that some button types don’t display an alternate title.
attributedAlternateTitle
setAlternateMnemonicLocation
setAlternateTitleWithMnemonic
setAttributedTitle
setButtonType
setFont
Sets the string that appears on the receiver when it’s in its normal state to the attributed string aString and redraws the button.
public void setAttributedTitle
(NSAttributedString aString)
The title is always shown on buttons that don’t use their alternate contents when highlighting or displaying their alternate state.
Sets the background color of the button.
public void setBackgroundColor(NSColor)
The background color is used only when drawing borderless buttons.
Sets the appearance of the border, if the receiver has one.
public void setBezelStyle
(int bezelStyle)
bezelStyle must be one of the values specified in “Constants.”
A button uses shading to look like it’s sticking out or pushed in. You can set the shading with setGradientType
.
If the receiver is not bordered, the bezel style is ignored.
Sets how the receiver highlights while pressed and how it shows its state.
public void setButtonType
(int aType)
setButtonType
redisplays the receiver before returning.
The types available are for the most common button types, which are also accessible in Interface Builder; you can configure different behavior with the setHighlightsBy
and setShowsStateBy
methods.
The aType argument can be one of the constants defined in “Constants.”
setAlternateImage
setImage
(NSCell)Sets the font used to display the title and alternate title to the font specified by fontObj.
public void setFont
(NSFont fontObj)
Does nothing if the receiver has no title or alternate title.
If the button cell has a key equivalent, its font is not changed, but the key equivalent’s font size is changed to match the new title font.
Sets the type of gradient to use for the receiver.
public void setGradientType
(int gradientType)
If the receiver has no border, this method has no effect on its appearance. A concave gradient is darkest in the top-left corner; a convex gradient is darkest in the bottom-right corner. Weak versus strong is how much contrast exists between the colors used in opposite corners.
The gradientType argument can be one of the constants defined in “Constants.”
Sets the way the receiver highlights itself while pressed.
public void setHighlightsBy
(int aType)
aType can be the logical OR of one or more of the cell masks described in NSCell’s “Constants” section.
If both NSCell.ChangeGrayCellMask
and NSCell.ChangeBackgroundCellMask
are specified, both are recorded, but which behavior is used depends on the button cell’s image. If the button has no image, or if the image has no alpha (transparency) data, NSCell.ChangeGrayCellMask
is used. If the image does have alpha data, NSCell.ChangeBackgroundCellMask
is used; this arrangement allows the color swap of the background to show through the image’s transparent pixels.
Sets whether the receiver’s image appears “dim” when the button cell is disabled, depending on the Boolean value flag.
public void setImageDimsWhenDisabled
(boolean flag)
By default, all button types except SwitchButton
and RadioButton
do dim when disabled. When SwitchButton
s and RadioButton
s are disabled, only the associated text dims. The default setting for this condition is reasserted whenever you invoke setButtonType
, so be sure to specify the button cell’s type before you invoke setImageDimsWhenDisabled
.
Sets the position of the receiver’s image relative to its title.
public void setImagePosition
(int aPosition)
See NSCell’s “Constants” section for a listing of possible values for aPosition.
Sets the key equivalent character of the receiver, specified by aKeyEquivalent, and redraws the receiver’s inside if it displays a key equivalent instead of an image.
public void setKeyEquivalent
(String aKeyEquivalent)
The key equivalent isn’t displayed if the image position is set to NSCell.NoImage
, NSCell.ImageOnly
, or NSCell.ImageOverlaps
; that is, the button must display both its title and its “image” (the key equivalent in this case), and they must not overlap.
To display a key equivalent on a button, set the image and alternate image to null
, then set the key equivalent, then set the image position.
Sets the font used to draw the key equivalent to the font specified by fontObj and redisplays the receiver if necessary.
public void setKeyEquivalentFont
(NSFont fontObj)
Does nothing if the receiver doesn’t have a key equivalent associated with it. The default font is the same as that used to draw the title.
Sets by name and size the font used to draw the key equivalent and redisplays the receiver if necessary.
public void setKeyEquivalentFontAndSize
(String fontName, float fontSize)
Does nothing if the receiver doesn’t have a key equivalent associated with it. The default font is the same as that used to draw the title.
Sets the mask indicating the modifier keys to be applied to the receiver’s key equivalent using the value of mask.
public void setKeyEquivalentModifierMask
(int mask)
The only mask bits relevant in button key-equivalent modifier masks are NSEvent.ControlKeyMask
, NSEvent.AlternateKeyMask
, and NSEvent.CommandKeyMask
bits.
Sets the message delay and interval for the receiver.
public void setPeriodicDelayAndInterval
(float delay, float interval)
These two values are used if the receiver is configured (by a setContinuous
message) to continuously send the action message to the target object while tracking the mouse. delay is the amount of time (in seconds) that a continuous button will pause before starting to periodically send action messages to the target object. interval is the amount of time (also in seconds) between those messages.
The maximum value allowed for both delay and interval is 60.0 seconds; if a larger value is supplied, it’s ignored, and 60.0 seconds is used.
setContinuous
(NSCell)Sets whether the receiver’s border is displayed only when the cursor is over the button.
public void setShowsBorderOnlyWhileMouseInside
(boolean show)
If show is true
, the border is displayed only when the cursor is within the receiver’s border and the button is active. If show is false
, the receiver’s border continues to be displayed when the cursor is outside button’s bounds.
Sets the way the receiver indicates its alternate state.
public void setShowsStateBy
(int aType)
aType should be the logical OR of one or more of the cell masks described in NSCell’s “Constants” section.
If both NSCell.ChangeGrayCellMask
and NSCell.ChangeBackgroundCellMask
are specified, both are recorded, but the actual behavior depends on the button cell’s image. If the button has no image, or if the image has no alpha (transparency) data, NSCell.ChangeGrayCellMask
is used. If the image exists and has alpha data, NSCell.ChangeBackgroundCellMask
is used; this arrangement allows the color swap of the background to show through the image’s transparent pixels.
Sets the sound that’s played when the user presses the receiver to aSound.
public void setSound
(Object aSound)
The sound is played during a mouse-down event, such as NSEvent.LeftMouseDown
.
Sets the title displayed by the receiver when in its normal state to aString and, if necessary, redraws the receiver’s contents.
public void setTitle
(String aString)
This title is always shown on buttons that don’t use their alternate contents when highlighting or displaying their alternate state.
Sets the title displayed on the receiver when it’s in its normal state to aString, taking into account the fact that an embedded “&” character is not a literal but instead marks the normal state’s “keyboard mnemonic.”
public void setTitleWithMnemonic
(String aString)
If necessary, setTitleWithMnemonic
redraws the button cell. This title is always shown on buttons that don’t use their alternate contents when highlighting or displaying their alternate state. Mnemonics are not supported in Mac OS X.
setAlternateTitleWithMnemonic
setTitleWithMnemonic
(NSCell)setMnemonicLocation
(NSCell)public void setTransparent
(boolean flag)
Sets whether the receiver is transparent, depending on the Boolean value flag, and redraws the receiver if necessary. A transparent button tracks the mouse and sends its action, but doesn’t draw. A transparent button is useful for sensitizing an area on the screen so that an action gets sent to a target when the area receives a mouse click.
Returns true
if the receiver’s border is displayed only when the cursor is over the button and the button is active.
public boolean showsBorderOnlyWhileMouseInside
()
By default, this method returns false
.
Returns the logical OR of flags that indicate the way the receiver shows its alternate state.
public int showsStateBy
()
See NSCell’s “Constants” section for the list of flags.
Returns the sound that’s played when the user presses the button.
public Object sound
()
Returns the title displayed on the receiver when it’s in its normal state (this title is always displayed if the button doesn’t use its alternate contents for highlighting or displaying the alternate state).
public String title
()
Returns the empty string if the button doesn’t display a title. By default, a button’s title is “Button.”
setTitle
alternateTitle
setButtonType
mnemonic
(NSCell)mnemonicLocation
(NSCell)These are the bezel styles used by bezelStyle
and setBezelStyle
:
These are the button types that can be specified using setButtonType
Button Type |
Description |
---|---|
While the button is held down, the alternate image and alternate title are displayed. The normal image and title are displayed when the button isn’t pressed. This option is called “Momentary Change” in Interface Builder’s Button Inspector. |
|
The first click both highlights and causes the button to be “pushed in” if the button is bordered. A second click returns it to its normal state. This option is called “Push On/Push Off” in Interface Builder’s Button Inspector. |
|
The first click highlights the button. A second click returns it to the normal (unhighlighted) state. This option is called “On/Off” in Interface Builder’s Button Inspector. |
|
After the first click, the button displays its alternate image or title. A second click returns the button to its normal state. This option is called “Toggle” in Interface Builder’s Button Inspector. |
|
This style is a variant of |
|
Similar to |
|
|
While the button is held down it’s shown as “lit.” This type of button is best for simply triggering actions, as it doesn’t show its state; it always displays its normal image or title. |
|
While the button is held down it’s shown as “lit,” and also “pushed in” to the screen if the button is bordered. This type of button is best for simply triggering actions, as it doesn’t show its state; it always displays its normal image or title. |
© 1997, 2007 Apple Inc. All Rights Reserved. (Last updated: 2007-02-01)