PATH  Documentation > Mac OS X > Application Kit Reference: Java

Table of Contents

NSButtonCell


Inherits from:
NSActionCell : NSCell : NSObject
Package:
com.apple.yellow.application


Class Description


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. To create groups of switches or radio buttons, use an NSMatrix holding a set of NSButtonCells.

An NSButtonCell is a two-state cell; it's either "off" or "on," and can be configured to display the two states differently, with a separate title and/or image for either state. The two states are more often referred to as "normal" and "alternate." An NSButtonCell's state is also used as its value, so NSCell methods that set the value ( setIntValue and so on) actually set the NSButtonCell's state to "on" if the value provided is non-zero (or non-null for strings), and to "off" if the value is zero or null. Similarly, methods that retrieve the value return 1 for the "on" or alternate state ( stringValue returns a String containing a single character "1"), or 0 for the "off" or normal state ( stringValue returns a String containing a single character "0"). You can also use NSCell's setState and state methods to set or retrieve the state directly. After changing the state, send a display message to show the NSButtonCell's new appearance. (NSButton does this automatically.)

An NSButtonCell sends its action message to its target once if its view is clicked and it gets the mouse-down event, but can also send the action message continuously as long as the mouse is held down with the cursor inside the NSButtonCell. The NSButtonCell can show that it's being pressed by highlighting in several ways-for example, a bordered NSButtonCell can appear pushed into the screen, or the image or title can change to an alternate form while the NSButtonCell is pressed.

An NSButtonCell can also have a key equivalent (like a menu item). If the NSButtonCell is displayed in the key window, the NSButtonCell gets the first chance to receive events related to key equivalents. This feature is used quite often in modal panels that have an "OK" button. An NSButtonCell can either display a graphical image representing the key equivalent, or you can mark the keyboard "mnemonic" character in the NSButtonCell's title using setTitleWithMnemonic, setAlternateTitleWithMnemonic, or setAlternateMnemonicLocation.

For more information on NSButtonCell's behavior, see the NSButton and NSMatrix class specifications.


Exceptions


In its implementation of the compare method (declared in NSCell), NSButtonCell throws a BadComparisonException if the otherCell argument is not of the NSButtonCell class.




Method Types


Constructors
NSButtonCell
Setting the titles
alternateMnemonic
alternateMnemonicLocation
alternateTitle
attributedAlternateTitle
attributedTitle
setAlternateMnemonicLocation
setAlternateTitle
setAlternateTitleWithMnemonic
setAttributedAlternateTitle
setAttributedTitle
setFont
setTitle
setTitleWithMnemonic
title
Setting the images
alternateImage
imagePosition
setAlternateImage
setImagePosition
Setting the repeat interval
setPeriodicDelayAndInterval
Setting the key equivalent
keyEquivalent
keyEquivalentFont
keyEquivalentModifierMask
setKeyEquivalent
setKeyEquivalentModifierMask
setKeyEquivalentFont
setKeyEquivalentFontAndSize
Modifying graphic attributes
bezelStyle
gradientType
imageDimsWhenDisabled
isOpaque
isTransparent
setBezelStyle
setShowsBorderOnlyWhileMouseInside
setGradientType
setImageDimsWhenDisabled
setTransparent
showsBorderOnlyWhileMouseInside
Displaying
highlightsBy
setHighlightsBy
setShowsStateBy
setButtonType
showsStateBy
Playing sound
setSound
sound
Handling events and action messages
mouseEntered
mouseExited
performClick


Constructors



NSButtonCell

public NSButtonCell()

Description forthcoming.

public NSButtonCell(String aString)

Description forthcoming.

public NSButtonCell(NSImage anImage)

Description forthcoming.


Instance Methods



alternateImage

public NSImage alternateImage()

Returns the image that appears on the button when it's in its alternate state, or null if there is no alternate image. Note that some button types don't display an alternate image. Buttons don't display images by default.

See Also: imagePosition, keyEquivalent, setButtonType, image (NSCell)



alternateMnemonic

public String alternateMnemonic()

Returns the character in the alternate title (the title displayed on the button cell when it's in its alternate state) that's marked as the "keyboard mnemonic." If the alternate title doesn't have a keyboard mnemonic, the empty string is returned.

See Also: alternateMnemonicLocation, setAlternateTitleWithMnemonic, mnemonic( NSCell)



alternateMnemonicLocation

public int alternateMnemonicLocation()

Returns an unsigned integer indicating the character in the alternate title (the title displayed on the button cell when it's in its alternate state) that's marked as the "keyboard mnemonic." If the alternate title doesn't have a keyboard mnemonic, NSArray.NotFound is returned.

See Also: alternateMnemonic, setAlternateTitleWithMnemonic, mnemonicLocation (NSCell)



alternateTitle

public String alternateTitle()

Returns the string that appears on the button when it's in its alternate state, or the empty string if the button doesn't display an alternate title. Note that some button types don't display an alternate title. By default, a button's alternate title is "Button".

See Also: alternateMnemonic, attributedAlternateTitle, setButtonType, title



attributedAlternateTitle

public NSAttributedString attributedAlternateTitle()

Returns the string that appears on the button when it's in its alternate state as an NSAttributedString, or an empty attributed string if the button doesn't display an alternate title. Note that some button types don't display an alternate title. By default, a button's alternate title is "Button".

See Also: alternateMnemonic, attributedTitle, setButtonType



attributedTitle

public NSAttributedString attributedTitle()

Returns the string that appears on the button when it's in its normal state as an NSAttributedString, or an empty attributed string if the button doesn't display a title. 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".

See Also: attributedAlternateTitle, setButtonType, mnemonic (NSCell)



bezelStyle

public int bezelStyle()

Returns the appearance of the button's border. See setBezelStyle for the list of the possible values.

See Also: setBezelStyle



gradientType

public int gradientType()

Returns gradient of the button's border. See setGradientType for the list of the possible values.

highlightsBy

public int highlightsBy()

Returns the logical OR of flags that indicate the way the button cell highlights when it receives a mouse-down event. See setHighlightsBy for the list of flags.

See Also: showsStateBy



imageDimsWhenDisabled

public boolean imageDimsWhenDisabled()

Returns whether the button cell's image and text appear "dim" when the button cell is disabled. By default, all button types except SwitchButton and RadioButton do dim when disabled. When SwitchButtons and RadioButtons are disabled, only the associated text dims.

See Also: setButtonType, setImageDimsWhenDisabled



imagePosition

public int imagePosition()

Returns the position of the button's image relative to its title. The return value is one of the following:

If the title is above, below, or overlapping the image, or if there is no image, the text is horizontally centered within the button.

See Also: setButtonType, setTitle, setImage (NSCell)



isOpaque

public boolean isOpaque()

Returns true if the button cell draws over every pixel in its frame, false if not. The button cell is opaque only if it isn't transparent and if it has a border.

See Also: isTransparent



isTransparent

public boolean isTransparent()

Returns true if the button is transparent, false otherwise. A transparent button never draws itself, but it receives mouse-down events and tracks the mouse properly.

See Also: isOpaque



keyEquivalent

public String keyEquivalent()

Returns the key-equivalent character of the button, or the empty string if one hasn't been defined. Buttons don't have a default key equivalent.

See Also: keyEquivalentFont



keyEquivalentFont

public NSFont keyEquivalentFont()

Returns the font used to draw the key equivalent, or null if the button cell doesn't have a key equivalent. The default font is the same as that used to draw the title.

See Also: setFont



keyEquivalentModifierMask

public int keyEquivalentModifierMask()

Returns the mask indicating the modifier keys that are applied to the button's key equivalent. The only mask bits relevant in button key-equivalent modifier masks are NSEvent.ControlKeyMask, NSEvent.AlternateKeyMask, and NSEvent.CommandKeyMask bits.

See Also: keyEquivalent



mouseEntered

public void mouseEntered(NSEvent event)

Draws the button's border. This method is called only when the mouse moves onto the button and showsBorderOnlyWhileMouseInside returns true.

mouseExited

public void mouseExited(NSEvent event)

Erases the button's border. This method is called only when the mouse moves off the button and showsBorderOnlyWhileMouseInside returns true.

performClick

public void performClick(Object sender)

Simulates the user clicking the button with the mouse. 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 raised while the target object is processing the action message, the button is unhighlighted before the exception is propagated out of performClick.

setAlternateImage

public void setAlternateImage(NSImage image)

Sets the image that appears on the button when it's in its alternate state to image and, if necessary, redraws the contents of the button. Note that some button types don't display an alternate image.

See Also: setButtonType, setImage (NSCell)



setAlternateMnemonicLocation

public void setAlternateMnemonicLocation(int location)

Sets the character in the alternate title (the title displayed on the button cell when it's in its alternate state) that's to be marked as the "keyboard mnemonic." The character specified by location will be underlined; location can be any integer from 0 to 254. If you don't want the alternate title to have a keyboard mnemonic, specify a location of NSArray.NotFound.

setAlternateMnemonicLocation doesn't cause the button cell to be redisplayed.

See Also: setAlternateTitleWithMnemonic



setAlternateTitle

public void setAlternateTitle(String aString)

Sets the title that's displayed on the button when it's in its alternate state to aString. Note that some button types don't display an alternate title.

See Also: setAlternateMnemonicLocation, setAlternateTitleWithMnemonic, setTitle, setButtonType, setFont



setAlternateTitleWithMnemonic

public void setAlternateTitleWithMnemonic(String aString)

Sets the title that is displayed on the button cell 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." The character in the title that immediately follows the "&" character will be underlined.

If necessary, setAlternateTitleWithMnemonic redraws the button cell. Note that some button types don't display an alternate title.

See Also: setAlternateMnemonicLocation, setTitleWithMnemonic



setAttributedAlternateTitle

public void setAttributedAlternateTitle(NSttributedString aString)

Sets the string that appears on the button when it's in its alternate state to the attributed string aString. Note that some button types don't display an alternate title.

See Also: setAlternateMnemonicLocation, setAlternateTitleWithMnemonic, setAttributedTitle, setButtonType, setFont



setAttributedTitle

public void setAttributedTitle(NSAttributedString aString)

Sets the string that appears on the button when it's in its normal state to the attributed string aString and redraws the button. The title is always shown on buttons that don't use their alternate contents when highlighting or displaying their alternate state.

See Also: setAttributedAlternateTitle, setButtonType, setFont, setMnemonicLocation (NSCell)



setBezelStyle

public void setBezelStyle(int bezelStyle)

Sets the appearance of the border, if the button has one. bezelStyle must be one of the following:
Bezel Style Description
RoundedBezelStyle A rounded rectangle button, designed for text.
RegularSquareBezelStyle A rectangular button with a 2 pixel border, designed for icons.
ThickSquareBezelStyle A rectangular button with a 3 pixel border, designed for icons.
ThickerSquareBezelStyle A rectangular button with a 4 pixel border, designed for icons.

The button uses shading to look like it's sticking out or pushed in. You can set the shading with setGradientType.

If the button is not bordered, the bezel style is ignored.

See Also: bezelStyle



setButtonType

public void setButtonType(int aType)

Sets how the button highlights while pressed and how it shows its state. setButtonType redisplays the button 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.

aType can be one of eight constants:


Button Type Description
MomentaryPushInButton 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. This option is called "Momentary Push In" in Interface Builder's Button Inspector. This is the default button type.
MomentaryLightButton 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. This option is called "Momentary Light" in Interface Builder's Button Inspector.
MomentaryChangeButton 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.
PushOnPushOffButton 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.
OnOffButton 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.
ToggleButton The first click highlights the button, while a second click returns it to its normal state. Highlighting is performed by changing to the alternate title or image and showing the button as "pushed in" if the button is bordered. This option is called "Toggle" in Interface Builder's Button Inspector.
SwitchButton This is a variant of ToggleButton that has no border, with the default image set to "Switch," and the alternate image set to "HighlightedSwitch" (these are system bitmaps). This type of button is available as a separate palette item in Interface Builder.
RadioButton Like SwitchButton, but the default image is set to "RadioButton" and the alternate image is set to "HighlightedRadioButton" (these are system bitmaps). This type of button is available as a separate palette item in Interface Builder.

See Also: setAlternateImage, setImage (NSCell)



setFont

public void setFont(NSFont fontObj)

Sets the font used to display the title and alternate title. Does nothing if the button cell 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.

See Also: setKeyEquivalentFont, setKeyEquivalentFontAndSize, font (NSCell)



setGradientType

public void setGradientType(int gradientType)

Sets the type of gradient to use for the button. If the button has no border, this method has no affect on its appearance.

gradientType can be one of the following constants:


Value Description
GradientNone There is no gradient, so the button looks flat.
GradientConcanveWeak The top left corner is light gray and the bottom right corner is dark gray, so the button appears to be pushed in.
GradientConcaveStrong As with GradientConcanveWeak, the top left corner is light gray and the bottom right corner is dark gray, but the difference between the grays is greater, so the appearance of being pushed-in is stronger.
GradientConvexWeak The top left corner is dark gray and the bottom right corner is light gray, so the button appears to be sticking out.
GradientConcaveStrong As with GradientConvexWeak, the top left corner is dark gray and the bottom right corner is light gray, but the difference between the grays is greater, so the appearance of sticking out is stronger.

See Also: gradientType



setHighlightsBy

public void setHighlightsBy(int aType)

Sets the way the button cell highlights itself while pressed. aType can be the logical OR of one or more of the following constants:

If both ChangeGrayCellMask and 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, ChangeGrayCellMask is used. If the image does have alpha data, ChangeBackgroundCellMask is used; this allows the color swap of the background to show through the image's transparent pixels.

See Also: setShowsStateBy



setImageDimsWhenDisabled

public void setImageDimsWhenDisabled(boolean flag)

Sets whether the button cell's image appears "dim" when the button cell is disabled. By default, all button types except SwitchButton and RadioButton do dim when disabled. When SwitchButtons and RadioButtons are disabled, only the associated text associated 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.

setImagePosition

public void setImagePosition(int aPosition)

Sets the position of the button's image relative to its title. See the imagePosition method description for a listing of possible values for aPosition.

setKeyEquivalent

public void setKeyEquivalent(String aKeyEquivalent)

Sets the key equivalent character of the button, and redraws the button's inside if it displays a key equivalent instead of an image. 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.

See Also: setAlternateImage, setImagePosition, setKeyEquivalentFont, setImage (NSCell)



setKeyEquivalentFont

public void setKeyEquivalentFont(NSFont fontObj)

Sets the font used to draw the key equivalent, and redisplays the button cell if necessary. Does nothing if the button cell doesn't have a key equivalent associated with it. The default font is the same as that used to draw the title.

See Also: setFont



setKeyEquivalentFontAndSize

public void setKeyEquivalentFontAndSize( String fontName, float fontSize)

Sets by name and size the font used to draw the key equivalent, and redisplays the button cell if necessary. Does nothing if the button cell doesn't have a key equivalent associated with it. The default font is the same as that used to draw the title.

See Also: setFont



setKeyEquivalentModifierMask

public void setKeyEquivalentModifierMask(int mask)

Sets the mask indicating the modifier keys to be applied to the button's key equivalent. The only mask bits relevant in button key-equivalent modifier masks are NSEvent.ControlKeyMask, NSEvent.AlternateKeyMask, and NSEvent.CommandKeyMask bits.

See Also: setKeyEquivalent



setPeriodicDelayAndInterval

public void setPeriodicDelayAndInterval( float delay, float interval)

Sets the message delay and interval for the button. These two values are used if the button 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.

See Also: setContinuous (NSCell)



setShowsBorderOnlyWhileMouseInside

public void setShowsBorderOnlyWhileMouseInside(boolean show)

Sets whether the button cell's border is displayed only when the mouse is over the button. If show is true, the border is displayed only when the mouse is within the button cell's border and the button is active. If show is false, the button's border continues to be displayed when the mouse is outside button's bounds.

See Also: showsBorderOnlyWhileMouseInside



setShowsStateBy

public void setShowsStateBy(int aType)

Sets the way the button cell indicates its alternate state. aType should be the logical OR of one or more of the following constants:
Value Description
NoCellMask The button cell doesn't change. This mask is ignored if any others are set in aType. This is the default.
ContentsCellMask The button cell displays its alternate icon and/or title.
ChangeGrayCellMask The button cell swaps the "control color" (NSColor's controlColor) and white pixels on its background and icon.
ChangeBackgroundCellMask Same as ChangeGrayCellMask, but only the background pixels are changed.

If both ChangeGrayCellMask and 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, ChangeGrayCellMask is used. If the image exists and has alpha data, ChangeBackgroundCellMask is used; this allows the color swap of the background to show through the image's transparent pixels.

See Also: setHighlightsBy, showsStateBy



setSound

public void setSound(Object aSound)

Sets the sound that's played when the user presses the button. The sound is played during a mouse-down event, such as NSEvent.LeftMouseDown.

See Also: sound



setTitle

public void setTitle(String aString)

Sets the title displayed by the button cell when in its normal state to aString and, if necessary, redraws the button's contents. This title is always shown on buttons that don't use their alternate contents when highlighting or displaying their alternate state.

See Also: setAlternateTitle, setButtonType, setFont, setTitleWithMnemonic



setTitleWithMnemonic

public void setTitleWithMnemonic(String aString)

Sets the title displayed on the button cell 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." 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.

See Also: setAlternateTitleWithMnemonic, setTitleWithMnemonic (NSCell), setMnemonicLocation (NSCell)



setTransparent

public void setTransparent(boolean flag)

Sets whether the button is transparent, and redraws the button 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.

showsBorderOnlyWhileMouseInside

public boolean showsBorderOnlyWhileMouseInside()

Returns true the button's border is displayed only when the mouse is over the button and the button is active. By default, this method returns false.

See Also: setShowsBorderOnlyWhileMouseInside



showsStateBy

public int showsStateBy()

Returns the logical OR of flags that indicate the way the button cell shows its alternate state. See setShowsStateBy for the list of flags.

See Also: highlightsBy, setShowsStateBy



sound

public Object sound()

Returns the sound that's played when the user presses the button.

See Also: setSound



title

public String title()

Returns the title displayed on the button 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). Returns the empty string if the button doesn't display a title. By default, a button's title is "Button".

See Also: alternateTitle, setButtonType, mnemonic (NSCell), mnemonicLocation (NSCell)




Table of Contents