javaswing1-170324224205
javaswing1-170324224205
What is Swing?
Method Description
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.
It is used to set the specified Icon on the
void setIcon(Icon b)
button.
Icon getIcon() It is used to get the Icon of the button.
It is used to set the mnemonic on the
void setMnemonic(int a)
button.
It is used to add the action listener to this
void addActionListener(ActionListener a)
object.
a mnemonic defines a key that lets you select an item from an active menu by typing the key.
Thus, a mnemonic allows you to use the keyboard to select an item from a menu that is already
being displayed.
In this case, the name is passed in name and the mnemonic is passed in mnen. Second, you can
set the mnemonic by calling setMnemonic( ).
Example of Java JButton
Java JLabel
The object of JLabel class is a component for placing text in a
container. It is used to display a single line of read only text. The text
can be changed by an application but a user cannot edit it directly. It
inherits JComponent class.
Constructor Description
Creates a JLabel instance with no image
JLabel()
and with an empty string for the title.
Creates a JLabel instance with the
JLabel(String s)
specified text.
Creates a JLabel instance with the
JLabel(Icon i)
specified image.
Creates a JLabel instance with the
JLabel(String s, Icon i, int
specified text, image, and horizontal
horizontalAlignment)
alignment.
Commonly used Methods of JLabel:
Methods Description
t returns the text string that a label
String getText()
displays.
It defines the single line of text this
void setText(String text)
component will display.
void setHorizontalAlignment(int It sets the alignment of the label's
alignment) contents along the X axis.
It returns the graphic image that the
Icon getIcon()
label displays.
It returns the alignment of the label's
int getHorizontalAlignment()
contents along the X axis.
Java JLabel Example
Java JTextField
The object of a JTextField class is a text component that allows the editing
of a single line text. It inherits JTextComponent class.
Constructor Description
Methods Description
It is used to add the specified action
void addActionListener(ActionListener l) listener to receive action events from this
textfield.
It returns the currently set Action for this
Action getAction() ActionEvent source, or null if no Action is
set.
void setFont(Font f) It is used to set the current font.
Constructor Description
Creates an unselected radio button with
JRadioButton()
no text.
Creates an unselected radio button with
JRadioButton(String s)
specified text.
Creates a radio button with the specified
JRadioButton(String s, boolean selected)
text and selected status.
Commonly used Methods:
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.
It is used to set the specified Icon on the
void setIcon(Icon b)
button.
Icon getIcon() It is used to get the Icon of the button.
It is used to set the mnemonic on the
void setMnemonic(int a)
button.
It is used to add the action listener to this
void addActionListener(ActionListener a)
object.
Java JRadioButton Java JRadioButton Example with
Example ActionListener
Java JComboBox
The object of Choice class is used to show popup menu of choices.
Choice selected by user is shown on the top of a menu. It inherits
JComponent class.
JComboBox class declaration
public class JComboBox extends JComponent implements ItemSelectable, ListDataListener, Acti
onListener, Accessible
Constructor Description
Creates a JComboBox with a default data
JComboBox()
model.
Creates a JComboBox that contains the
JComboBox(Object[] items)
elements in the specified array.
Creates a JComboBox that contains the
JComboBox(Vector<?> items)
elements in the specified Vector.
Commonly used Methods Of JComboBox :
Methods Description
void addItem(Object anObject) It is used to add an item to the item list.
It is used to delete an item to the item
void removeItem(Object anObject)
list.
It is used to remove all the items from
void removeAllItems()
the list.
It is used to determine whether the
void setEditable(boolean b)
JComboBox is editable.
void addActionListener(ActionListener a) It is used to add the ActionListener.
void addItemListener(ItemListener i) It is used to add the ItemListener.
Java JComboBox Java JComboBox Example with ActionListen
Example