0% found this document useful (0 votes)
174 views

Commonly Used Methods of Component Class

The document discusses common methods of several Swing components: - The Component class methods include adding components, setting size, layout, and visibility. - The JButton class declaration extends AbstractButton and includes constructors to create buttons with text or icons. Common AbstractButton methods set text, icon, enable/disable, and add action listeners. - The JLabel class declaration extends JComponent and includes constructors to create labels with text or icons. Common methods get/set text, icon, and horizontal alignment. - The JTextField class declaration extends JTextComponent and includes constructors to create text fields with or without initial text and columns. Common methods add/remove action listeners and set font.

Uploaded by

Sudha Madhuri
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
174 views

Commonly Used Methods of Component Class

The document discusses common methods of several Swing components: - The Component class methods include adding components, setting size, layout, and visibility. - The JButton class declaration extends AbstractButton and includes constructors to create buttons with text or icons. Common AbstractButton methods set text, icon, enable/disable, and add action listeners. - The JLabel class declaration extends JComponent and includes constructors to create labels with text or icons. Common methods get/set text, icon, and horizontal alignment. - The JTextField class declaration extends JTextComponent and includes constructors to create text fields with or without initial text and columns. Common methods add/remove action listeners and set font.

Uploaded by

Sudha Madhuri
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Commonly used Methods of Component class

The methods of Component class are widely used in java swing that are given below.

Method Description

public void add(Component c) add a component on another component.

public void setSize(int width,int height) sets size of the component.

public void setLayout(LayoutManager m) sets the layout manager for the component.

public void setVisible(boolean b) sets the visibility of the component. It is by default f


JButton class declaration
Let's see the declaration for javax.swing.JButton class.

1. public class JButton extends AbstractButton implements Accessible

Commonly used Constructors:

Constructor Description

JButton() It creates a button with no text and icon.

JButton(String s) It creates a button with the specified text.

JButton(Icon i) It creates a button with the specified icon object.

Commonly used Methods of AbstractButton class:

Methods Description

void setText(String s) It is used to set specified text on button

String getText() It is used to return the text of the button.

void setEnabled(boolean b) It is used to enable or disable the button.

void setIcon(Icon b) It is used to set the specified Icon on the button.

Icon getIcon() It is used to get the Icon of the button.

void setMnemonic(int a) It is used to set the mnemonic on the button.

void addActionListener(ActionListener a) It is used to add the action listener to this object.


JLabel class declaration
Let's see the declaration for javax.swing.JLabel class.

1. public class JLabel extends JComponent implements SwingConstants, Accessible

Commonly used Constructors:

Constructor Description

JLabel() Creates a JLabel instance with no image and with an


empty string for the title.

JLabel(String s) Creates a JLabel instance with the specified text.

JLabel(Icon i) Creates a JLabel instance with the specified image.

JLabel(String s, Icon i, int Creates a JLabel instance with the specified text, image,
horizontalAlignment) and horizontal alignment.

Commonly used Methods:

Methods Description

String getText() t returns the text string that a label displays.

void setText(String text) It defines the single line of text this component will
display.

void setHorizontalAlignment(int It sets the alignment of the label's contents along the X
alignment) axis.

Icon getIcon() It returns the graphic image that the label displays.

int getHorizontalAlignment() It returns the alignment of the label's contents along the
X axis.
JTextField class declaration
Let's see the declaration for javax.swing.JTextField class.

1. public class JTextField extends JTextComponent implements SwingConstants

Commonly used Constructors:

Constructor Description

JTextField() Creates a new TextField

JTextField(String text) Creates a new TextField initialized with the specified text.

JTextField(String text, int Creates a new TextField initialized with the specified text and
columns) columns.

JTextField(int columns) Creates a new empty TextField with the specified number of
columns.

Commonly used Methods:

Methods Description

void addActionListener(ActionListener l) It is used to add the specified action listener to receive


action events from this textfield.

Action getAction() It returns the currently set Action for this ActionEvent
source, or null if no Action is set.

void setFont(Font f) It is used to set the current font.

void It is used to remove the specified action listener so that it


removeActionListener(ActionListener l) no longer receives action events from this textfield.

You might also like