All Swing Controls Reference



Swing JLabel Class

The class JLabel can display either text, an image, or both. Label's contents are aligned by setting the vertical and horizontal alignment in its display area. By default, labels are vertically centered in their display area. Text-only labels are leading edge aligned, by default; image-only labels are horizontally centered, by default.

Class constructors

S.NOConstructor & Description
1JLabel()

Creates a JLabel instance with no image and with an empty string for the title.

2JLabel(Icon image)

Creates a JLabel instance with the specified image.

3JLabel(Icon image, int horizontalAlignment)

Creates a JLabel instance with the specified image and horizontal alignment.

4JLabel(String text)

Creates a JLabel instance with the specified text.

5JLabel(String text, Icon icon, int horizontalAlignment)

Creates a JLabel instance with the specified text, image, and horizontal alignment.

6JLabel(String text, int horizontalAlignment)

Creates a JLabel instance with the specified text and horizontal alignment.

Class methods

S.NOMethodDescription
1Icon getIcon()Returns the graphic image (glyph, icon) that the label displays.
2String getText() Returns the text string that the label displays.
3void setIcon(Icon icon) Defines the icon this component will display.
4void setText(String text) Defines the single line of text this component will display.

Swing JButton Class

The class JButton is an implementation of a push button. This component has a label and generates an event when pressed. It can have Image also.

Listener

ActionListener: The class which processes the ActionEvent should implement this interface.The object of that class must be registered with a component. The object can be registered using the addActionListener() method. When the action event occurs, that object's actionPerformed method is invoked.

  • actionPerformed(ActionEvent e)

    Invoked when an action occurs.

Class constructors

S.NOConstructor & Description
1JButton()

Creates a button with no set text or icon.

2JButton(Action a)

Creates a button where properties are taken from the Action supplied.

3JButton(Icon icon)

Creates a button with an icon.

4JButton(String text)

Creates a button with text.

5JButton(String text, Icon icon)

Creates a button with initial text and an icon.

Class methods

S.NOMethodDescription
2String getLabel() Returns the text string that the Button displays.
4void setLabel(String text) Defines the single line of text this component will display.

Swing JCheckBox Class

The class JCheckBox is an implementation of a check box - an item that can be selected or deselected, and which displays its state to the user.

Listener

ItemListener: The class which processes the ItemEvent should implement this interface.The object of that class must be registered with a component. The object can be registered using the addItemListener() method. When the action event occurs, that object's itemStateChanged method is invoked.

  • void itemStateChanged(ItemEvent e)

    Invoked when an item has been selected or deselected by the user.

Class constructors

S.NOConstructor & Description
1JCheckBox()

Creates an initially unselected check box button with no text, no icon.

2JCheckBox(Action a)

Creates a check box where properties are taken from the Action supplied.

3JCheckBox(Icon icon)

Creates an initially unselected check box with an icon.

4JCheckBox(Icon icon, boolean selected)

Creates a check box with an icon and specifies whether or not it is initially selected.

5JCheckBox(String text)

Creates an initially unselected check box with text.

6JCheckBox(String text, boolean selected)

Creates a check box with text and specifies whether or not it is initially selected.

7JCheckBox(String text, Icon icon)

Creates an initially unselected check box with the specified text and icon.

8JCheckBox(String text, Icon icon, boolean selected)

Creates a check box with text and icon, and specifies whether or not it is initially selected.


Swing JRadioButton Class

The class JRadioButton is an implementation of a radio button - an item that can be selected or deselected, and which displays its state to the user.

Listener

ActionListener: The class which processes the ActionEvent should implement this interface.The object of that class must be registered with a component. The object can be registered using the addActionListener() method. When the action event occurs, that object's actionPerformed method is invoked.

  • actionPerformed(ActionEvent e)

    Invoked when an action occurs.

Class constructors

S.NOConstructor & Description
1JRadioButton()

Creates an initially unselected radio button with no set text.

2JRadioButton(Action a)

Creates a radiobutton where properties are taken from the Action supplied.

3JRadioButton(Icon icon)

Creates an initially unselected radio button with the specified image but no text.

4JRadioButton(Icon icon, boolean selected)

Creates a radio button with the specified image and selection state, but no text.

5JRadioButton(String text, boolean selected)

Creates a radio button with the specified text and selection state.

6JRadioButton(String text, Icon icon)

Creates a radio button that has the specified text and image, and that is initially unselected.

7JRadioButton(String text, Icon icon, boolean selected)

Creates a radio button that has the specified text, image, and selection state.


Swing JList Class

The class JList is a component which displays a list of objects and allows the user to select one or more items. A separate model, ListModel, maintains the contents of the list.

Class constructors

S.NOConstructor & Description
1JList()

Constructs a JList with an empty, read-only, model.

2JList(Vector v)

Constructs a JList that displays the elements in the specified Vector.

Class methods

S.NOMethodDescription
1ListModel getModel() Returns the data model that holds the list of items displayed by the JList component.
2int getSelectedIndex() Returns the smallest selected cell index; the selection when only a single item is selected in the list.
3int[] getSelectedIndices() Returns an array of all of the selected indices, in increasing order.
4Object getSelectedValue() Returns the value for the smallest selected cell index; the selected value when only a single item is selected in the list.
5int getSelectedIndex() Returns the smallest selected cell index; the selection when only a single item is selected in the list.

Swing JTextField Class

The class JTextField is a component which allows the editing of a single line of text.

Class constructors

S.NOConstructor & Description
1JTextField()

Constructs a new TextField.

2JTextField(String text)

Constructs a new TextField initialized with the specified text.

Class methods

S.NOMethodDescription
2String getText() Returns the text string that the label displays.
4void setText(String text) Defines the single line of text this component will display.

Swing JTextArea Class

The class JTextArea is a multi-line area to display plain text.

Class constructors

S.NOConstructor & Description
1JTextArea()

Constructs a new TextArea.

2JTextArea(int rows, int columns)

Constructs a new empty TextArea with the specified number of rows and columns.

3JTextArea(String text)

Constructs a new TextArea with the specified text displayed.

4JTextArea(String text, int rows, int columns)

Constructs a new TextArea with the specified text and number of rows and columns.

Class methods

S.NOMethodDescription
1void append(String str) Appends the given text to the end of the document.
2int getColumns() Defines the single line of text this component will display.
3int getLineCount() Determines the number of lines contained in the area.
4int getRows() Returns the number of rows in the TextArea.

Swing ImageIcon Class

The class ImageIcon is an implementation of the Icon interface that paints Icons from Images.

Class constructors

S.NOConstructor & Description
1ImageIcon()

Creates an uninitialized image icon.

2ImageIcon(Image image)

Creates an ImageIcon from an image object.

3ImageIcon(String filename)

Creates an ImageIcon from the specified file.

4ImageIcon(URL location)

Creates an ImageIcon from the specified URL.

Class methods

S.NOMethodDescription
1String getDescription() Gets the description of the image.
2int getIconHeight() Gets the height of the icon.
3int getIconWidth() Gets the width of the icon.
4Image getImage() Returns this icon's Image.

Share this article on