Swing Components
Constructors
and Methods
This
page lists the constructors and methods of various Swing
components..
JComponent
Class Methods
Many
of the Swing components inherit methods from JComponent
or superclasses of JComponent .
Here
are some methods of the JComponent class or superclasses of JComponent
that are inherited by
child classes such as JButton , JLabel , JRadioButton ,
JCheckBox , JList , etc.
~Common
Methods~
|
Add
a tool tip that appears when the cursor is over the component
|
-
setFont(Font
f)
-
Font
getFont()
|
Set
or get the font of the text on the component
|
-
setVisible(boolean)
-
boolean
isVisible()
|
Set
or get whether the component is visible using true
or false as the argument
|
-
setEnabled(boolean)
-
boolean
isEnabled()
|
Enable
or disable the component using true
or false as the argument.
Check
to see if a component is enabled or disabled.
|
-
setOpaque(boolean)
-
boolean
isOpaque()
|
Set
the component so it is filled with a background color.
Check
to see if a component is opaque.
|
-
setBackground
(Color c)
-
Color
getBackground ()
|
Set
or get the component's background color.
|
-
setForeground
(Color c)
-
Color
getForeground ()
|
Set
or get
the text color.
|
-
add
(component)
-
remove(component)
|
add
or remove a component from the container.
|
Swing
Button Constructors & Methods
Here
are the constructors for the Swing Buttons - JButton , JRadioButton
and JCheckBox.
~JButton
Constructors~
|
creates
a plain button without any text
|
|
creates
a button with text
|
|
an
image is shown on the button
|
|
both
an image and text is shown on the button
|
~JCheckBox
Constructors~
|
Creates
a checkbox with no text, no icon, unselected
|
|
Creates
a checkbox with the specified text, no icon, unselected
|
|
Creates
a checkbox with the specified text, no icon, and selected if b is true
|
|
Creates
a checkbox with no text, the specified image, unselected
|
|
Creates
a checkbox with no text, the specified image, and selected if b is true
|
|
Creates
a checkbox with the specified text, the specified image, unselected
|
|
Creates
a checkbox with the specified text, the specified image, and selected if b is true
|
~JRadioButton
Constructors~
|
Creates
a radio button
with no text, no icon, unselected
|
|
Creates
a radio button
with the specified text, no icon, unselected
|
|
Creates
a radio button
with the specified text, no icon, and selected if b is true
|
|
Creates
a radio button
with no text, the specified image, unselected
|
|
Creates
a radio button
with no text, the specified image, and selected if b is true
|
|
Creates
a radio button with
the specified text, the specified image, unselected
|
|
Creates
a radio button
with the specified text, the specified image, and selected if b is true
|
Many
of the Swing button components inherit methods from JAbstractButton .
Here
are some methods JButton , JRadioButton and JCheckBox
can use - inherited from JAbstractButton .
~JAbstractButton
Methods~
|
Set
the text displayed by the button.
|
|
Set
the horizontal position of the button's text relative to the button's image
using as the argument: LEFT , CENTER , RIGHT ,
LEADING , and TRAILING (the default).
|
|
Set
the vertical position of the button's text relative to the button's image using
as the argument: TOP , CENTER (the default), and BOTTOM .
|
|
Set
the image displayed by the button when it's being pressed.
|
|
Use
setRolloverIcon(Icon m)
with setRolloverEnabled(true) to
make the button display the specified icon when the cursor passes over it.
|
JLabel
Constructors & Methods
Here
are the constructors for JLabel.
~JLabel
Constructors~
|
creates
a label with text displayed
|
|
creates
a label and justifies the text according to position .
|
|
an
image is shown on the button
|
|
creates
an label showing an image and justifies the image according to position .
|
|
creates
a label showing text and an image, justified according to position .
|
Here
are some methods of the JLabel class.
~JLabel
Methods~
-
setText(String
s)
-
getText()
|
Set
or get the text displayed by the label.
|
|
Set
the horizontal position of the label's text relative to the label's image
using as the argument: JLabel.LEFT ,
JLabel.CENTER
or JLabel.RIGHT .
|
|
Set
the vertical position of the label's text relative to the label's image using
as the argument: JLabel.TOP ,
JLabel.CENTER (the default), and
JLabel.BOTTOM .
|
JComboBox
Constructors & Methods
Here
are the constructors for JComboBox.
~JComboBox
Constructors~
|
Creates
a combo box with no items in the menu list
|
|
Creates
a combo box with items in the menu list specified by the Object array
|
Here
are some methods of the JComboBox class.
~JComboBox
Methods~
|
Add
or insert the specified object into the combo box list. The insertItemAt
method inserts the object at the specified index.
|
|
The
getSelectedItem method
gets the currently selected item from the combo box list. To retrieve a
particular item use the getItemAt
method and specify the item's index number.
|
|
Enable
or disable the combo box using true
or false as the argument.
|
|
Set
the combo box to be editable (b is true) or uneditable (b is false).
|
|
determine
if the combo box is editable.
|
|
Get
the number of items in the combo box list.
|
|
Sets
the selected item to
index .
|
-
removeAllItems()
-
removeItem(index
i)
-
removeItemAt(Object
obj)
|
Remove
all items from the combo box list or remove a particular item by specifying the
item's index number or remove a set of items, specified in the object array.
|
Swing
Text Components
Swing
text components can be used to display information or get text based
information from a user. Two useful text components are JTextField
and JTextArea . Both of these components descend from the JTextComponent
class from which they inherits
many methods.
~JTextComponent
Methods~
|
Returns
the selected text contained in this TextComponent.
|
|
Returns
the text contained in this TextComponent.
|
|
Sets
the text in the TextComponent.
|
|
Returns
the boolean indicating whether this TextComponent is editable or not.
|
|
Set
the specified boolean to indicate whether or not this TextComponent should be
editable.
|
|
Selects
the text between the specified start and end positions
|
|
Selects
all the text in the TextComponent.
|
JTextField
Here
are the constructors for JTextField.
~JTextField
Constructors~
|
Creates
a text field without any initial text in the field.
|
|
Creates
a text field with the initial text in the field specified by the string s.
|
|
Creates
a text field with the initial text in the field specified by the string s.
The integer argument specifies the desired column width of the text field
|
|
Creates
a text field with the desired column width specified by the integer
argument.
|
Here
are some methods for JTextField.
~JTextField
Methods~
|
Sets
the current font.
|
|
Sets
the horizontal alignment of the text. Alignment can be set to JTextField.LEFT ,
JTextField.CENTER
or JTextField.RIGHT .
|
JTextArea
Here
are the constructors for JTextArea.
~JTextArea
Constructors~
|
Creates
a text area without any initial text or a specific size.
|
|
Creates
a text area with the specified number of rows and columns.
|
|
Creates
a text area with the initial text in the area specified by the String s.
|
|
Creates
a text area with the initial text in the area specified by the String s and the specified
number of rows and columns.
|
Here
are some useful JTextArea methods:-
~JTextArea
Methods~
|
Returns
the number of lines contained in the area.
|
|
Inserts
the specified text at the specified position.
|
|
Sets
the current font.
|
|
Sets
the line-wrapping policy of the text area. If b is true then lines
will wrap, if b is false they won't.
|
|
Sets
the style of wrapping used if the text area is wrapping lines. If b is true
then wrapping will not occur in the middle of words, if b is false it
will.
|
JList
Constructors & Methods
Here
are two constructors for JList.
~JList
Constructors~
|
Constructs
a JList with an empty
model.
|
|
Constructs
a JList
that displays the elements in the
specified array.
|
Here
are some methods of the JList class.
~JList
Methods~
|
Clears
the selection - after calling this method isSelectionEmpty will
return true.
|
|
Returns
the first selected index; returns -1 if there is no selected item.
|
|
Returns
the first selected value, or null if the selection is empty.
|
|
Returns
true if the specified index is selected.
|
|
Returns
true if nothing is selected.
|
|
Selects
a single list item.
|
|