The JButton Class

JButton is the simplest of the button types, adding very little to what is provided by the AbstractButton class. JButtons are buttons that are not toggled on and off but instead act as push buttons, which invoke some action when clicked. Figure 5-2 shows what these buttons look like in four of the Swing L&Fs.

JButtons in four L&Fs

Figure 5-2. JButtons in four L&Fs

Properties

The JButton class inherits most of its properties and default values from its superclasses. The exceptions to this are shown in Table 5-9. The model property is set to a new instance of DefaultButtonModel when a JButton is created.

Table 5-9. JButton properties

Property

Data type

get

is

set

Default value

accessibleContexto

AccessibleContext

·

  

JButton.AccessibleJButton( )

defaultButton

boolean

 

·

 

false

defaultCapable

boolean

 

·

·

true

modelo

ButtonModel

·

 

·

DefaultButtonModel( )

UIClassIDo

String

·

  

“ButtonUI”

ooverridden

See also properties from AbstractButton (Table 5-5).

     

The defaultButton property indicates whether the button is activated by default when some event occurs within the JRootPane containing the button. Typically, the event that would trigger the button would be an Enter key press, but this is actually up to the L&F implementation.

The defaultButton property cannot be set directly. Instead, it is set by telling the JRootPane which button should be the default. (We’ll cover JRootPane in Chapter 8—at this point, it’s enough ...

Get Java Swing, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.