- Inherits from:
- NSControl : NSView : NSResponder : NSObject
- Package:
- com.apple.yellow.application
NSButton is a subclass of NSControl that intercepts mouse-down events and sends an action message to a target object when it's clicked or pressed.
The NSButton can send its action continuously and display highlighting in several different ways. What's more, an NSButton can have a key equivalent that's eligible for triggering whenever the NSButton's NSPanel or NSWindow is the key window.
NSButton and NSMatrix both provide a control view, which is needed to display an NSButtonCell object. However, while NSMatrix requires you to access the NSButtonCells directly, most of NSButton's methods are "covers" for identically declared methods in NSButtonCell. (In other words, the implementation of the NSButton method invokes the corresponding NSButtonCell method for you, allowing you to be unconcerned with the NSButtonCell's existence.) The only NSButtonCell methods that don't have covers relate to the font used to display the key equivalent, and to specific methods for highlighting or showing the NSButton's state (these last are usually set together with NSButton's setButtonType method).
By virtue of its NSButtonCell, NSButton is an NSControl and displays its state depending on the configuration of the NSButtonCell. The NSButton can have two or three states. If it has two, they are on and off. If it has three, they are on, off, and mixed. A mixed state is useful for a checkbox or radio button that reflects the status of a feature. For example, suppose you have a checkbox that makes the selected text bold. If all the selected text is bold, it's on. If none of the selected text is bold, it's off. If the text has a combination of bold and plain text, it's mixed. Now suppose you click the checkbox. If you turn it on, all the text becomes bold. If you turn it off, all the text becomes plain. If you select the mixed state, the text remains as it is.
By default, a button has two states. You can allow the third state with the method setAllowsMixedState. To set the button's state directly, use setState. To cycle through all available states, use setNextState. Note that the state is used as the value, so NSControl methods like setIntValue actually set the state.
Override the designated initializer (NSView's initWithFrame: method) if you create a subclass of NSButton that performs its own initialization. If you want to use a custom NSButtonCell subclass with your subclass of NSButton, you have to override the setCellClass method, as described in "Creating New NSControls" in the NSControl class specification.
See the NSButtonCell class specification for more on NSButton's behavior.
- Constructors
- NSButton
- Setting the button type
- setButtonType
- Setting the state
- allowsMixedState
- setAllowsMixedState
- setNextState
- setState
- state
- Setting the repeat interval
- interval
- periodicDelay
- setPeriodicDelayAndInterval
- Setting the titles
- alternateTitle
- attributedAlternateTitle
- attributedTitle
- setAlternateTitle
- setAttributedAlternateTitle
- setAttributedTitle
- setTitle
- setTitleWithMnemonic
- title
- Setting the images
- alternateImage
- image
- imagePosition
- setAlternateImage
- setImage
- setImagePosition
- Modifying graphic attributes
- bezelStyle
- isBordered
- isTransparent
- setBordered
- setBezelStyle
- setShowsBorderOnlyWhileMouseInside
- setTransparent
- showsBorderOnlyWhileMouseInside
- Displaying
- highlight
- Setting the key equivalent
- keyEquivalent
- keyEquivalentModifierMask
- setKeyEquivalent
- setKeyEquivalentModifierMask
- Handling events and action messages
- performClick
- performKeyEquivalent
- Playing sound
- setSound
- sound
public NSButton()
public NSButton(NSRect aRect)
public boolean allowsMixedState()
See Also: setAllowsMixedState, setNextState
public NSImage alternateImage()
See Also: image, imagePosition, keyEquivalent, setButtonType
public String alternateTitle()
See Also: attributedAlternateTitle, setButtonType, title
public NSAttributedString attributedAlternateTitle()
See Also: attributedTitle, setButtonType
public NSAttributedString attributedTitle()
See Also: attributedAlternateTitle, setButtonType
public int bezelStyle()
See Also: setBezelStyle
public void highlight(boolean flag)
See Also: setButtonType
public NSImage image()
See Also: alternateImage, setButtonType
public int imagePosition()
Return Value | Meaning |
NoImage |
The button doesn't display an image (this is the default) |
ImageOnly |
The button displays an image, but not a title |
ImageLeft |
The image is to the left of the title |
ImageRight |
The image is to the right of the title |
ImageBelow |
The image is below the title |
ImageAbove |
The image is above the title |
ImageOverlaps |
The image overlaps the title |
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, setImage, setTitle
public float interval()
The default value is taken from a user's defaults (60 seconds maximum). If the user hasn't specified a value, it defaults to 0.075 seconds.
See Also: isContinuous NSControl, periodicDelay, setPeriodicDelayAndInterval
public boolean isBordered()
public boolean isTransparent()
public String keyEquivalent()
See Also: performKeyEquivalent, keyEquivalentFont (NSButtonCell)
public int keyEquivalentModifierMask()
NSEvent.ControlKeyMask
, NSEvent.AlternateKeyMask
,
and NSEvent.CommandKeyMask
bits.See Also: keyEquivalent
public void performClick(Object sender)
See Also: performKeyEquivalent
public boolean performKeyEquivalent(NSEvent anEvent)
See Also: keyEquivalentModifierMask
public float periodicDelay()
The default value is taken from a user's defaults (60 seconds maximum). If the user hasn't specified a value, it defaults to 0.4 seconds.
See Also: interval, setPeriodicDelayAndInterval, isContinuous (NSControl)
public void setAllowsMixedState(boolean flag)
See Also: allowsMixedState, setNextState
public void setAlternateImage(NSImage image)
See Also: setButtonType, setImage
public void setAlternateTitle(String aString)
See Also: setTitle, setTitleWithMnemonic, setButtonType, setFont (NSButtonCell)
public void setAttributedAlternateTitle(NSAttributedString aString)
See Also: setAttributedTitle, setButtonType, setFont (NSButtonCell)
public void setAttributedTitle(NSAttributedString aString)
See Also: setAttributedAlternateTitle, setButtonType, setFont (NSButtonCell)
public void setBezelStyle(int bezelStyle)
Bezel Style | Description |
NeXTBezelStyle |
A rectangular button with a 2 pixel border. It looks like an OPENSTEP 4.2 button and is available for backwards compatibility only. |
PushButtonBezelStyle |
A rounded rectangle button, designed for text. |
SmallIconButtonBezelStyle |
A rectangular button with a 2 pixel border, designed for icons. |
MediumIconButtonBezelStyle |
A rectangular button with a 3 pixel border, designed for icons. |
LargeIconButtonBezelStyle |
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 NSButtonCell's setGradientType method.
If the button is not bordered, the bezel style is ignored.
See Also: bezelStyle
public void setBordered(boolean flag)
public void setButtonType(int aType)
The types available are for the most common button types, which are also accessible in Interface Builder. You can configure different behavior with NSButtonCell's setHighlightsBy and setShowsStateBy methods.
aType can be one of eight constants:
Button Type | Description |
MomentaryLight |
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 Light" in Interface Builder's Button Inspector. This is the default button type. |
MomentaryPushButton |
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 Push" in Interface Builder's Button Inspector. |
MomentaryChangeButton |
While the button is held down, the alternate image and alternate title are displayed. The normal image or 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 "NSSwitch," and
the alternate image set to "NSHighlightedSwitch" (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 "NSRadioButton" and the alternate
image is set to "NSHighlightedRadioButton" (these are system
bitmaps). This type of button is available as a separate palette
item in Interface Builder. |
See Also: setAlternateImage, setImage, setButtonType (NSButtonCell)
public void setImage(NSImage image)
See Also: setImagePosition, setAlternateImage, setButtonType
public void setImagePosition(int aPosition)
public void setKeyEquivalent(String charCode)
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: performKeyEquivalent, setAlternateImage, setImage, setImagePosition, setKeyEquivalentFont (NSButtonCell)
public void setKeyEquivalentModifierMask(int mask)
NSEvent.ControlKeyMask
, NSEvent.AlternateKeyMask
,
and NSEvent.CommandKeyMask
bits.See Also: setKeyEquivalent
public void setNextState()
See Also: allowsMixedState, setAllowsMixedState
public void setPeriodicDelayAndInterval(
float delay,
float interval)
The maximum value allowed for both delay and interval is 60.0 seconds; if a larger value is supplied, it is ignored and 60.0 seconds is used.
See Also: setContinuous (NSControl)
public void setShowsBorderOnlyWhileMouseInside(boolean show)
If isBordered returns false, the border is never displayed, regardless of what this method returns.
See Also: showsBorderOnlyWhileMouseInside
public void setSound(Object aSound)
NSEvent.LeftMouseDown
.See Also: sound
public void setState(int value)
NSCell.OnState
, NSCell.OffState
,
or NSCell.MixedState
. If
necessary, this method also redraws the button.The cell can
have two or three states. If it has two, value can
be NSCell.OffState
(the normal or unpressed
state) and NSCell.OnState
(the alternate
or pressed state). If it has three, value can
be NSCell.OnState
(the feature is
in effect everywhere), NSCell.OffState
(the
feature is in effect nowhere), or NSCell.MixedState
(the
feature is in effect somewhere). Note that if the cell has only
two states and value is NSCell.MixedState
,
this method sets the cell's state to NSCell.OnState
.
Although
using the enumerated constants is preferred, value can
also be an integer. If the cell has two states, zero is treated
as NSCell.OffState
, and a non-zero
value is treated as NSCell.OnState
.
If the cell has three states, zero is treated as NSCell.OffState
;
a negative value, as NSCell.MixedState
;
and a positive value, as NSCell.OnState
.
To check whether the button uses the mixed state, use the method allowsMixedState.
public void setTitle(String aString)
See Also: setAlternateTitle, setButtonType, setTitleWithMnemonic, setFont (NSButtonCell)
public void setTitleWithMnemonic(String aString)
aButton.setTitleWithMnemonic( NSBundle.localizedString("Re&ceive"))];
See Also: setAlternateTitle, setButtonType, setTitle, setFont (NSButtonCell)
public void setTransparent(boolean flag)
public boolean showsBorderOnlyWhileMouseInside()
If isBordered returns false, the border is never displayed, regardless of what this method returns.
See Also: setShowsBorderOnlyWhileMouseInside
public Object sound()
See Also: setSound
public int state()
NSCell.OffState
(the
normal or unpressed state) or NSCell.OnState
(the
alternate or pressed state). If it has three, it returns NSCell.OnState
(the
feature is in effect everywhere), NSCell.OffState
(the feature
is in effect nowhere), or NSCell.MixedState
(the
feature is in effect somewhere).To check whether the button uses the mixed state, use the method allowsMixedState.
public String title()
See Also: alternateTitle, setButtonType, setTitle, setTitleWithMnemonic