JAVA SWINGS
JLabel and ImageIcon:
JLabel is Swing’s easiest-to-use component.
JLabel can be used to display text and/or an icon.
It is a passive component in that it does not respond to user input.
Constructor:
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 horizontalAlignment):- Creates a JLabel instance with
the specified text, image, and horizontal alignment.
Methods:
String getText():- It 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 alignment):- It sets the alignment of the label's
contents along the X 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.
Eg:
// Java Program to create a
// blank label and add text to it.
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class text extends JFrame {
// frame
static JFrame f;
// label to diaplay text
static JLabel l;
// default constructor
text()
{
}
// main class
public static void main(String[] args)
{
// create a new frame to stor text field and button
f = new JFrame("label");
// create a label to display text
l = new JLabel();
// add text to label
l.setText("label text");
JAVA SWINGS
// create a panel
JPanel p = new JPanel();
// add label to panel
p.add(l);
// add panel to frame
f.add(p);
// set the size of frame
f.setSize(300, 300);
f.show();
}
}
JTextField:
JTextField is a part of javax.swing package. The class JTextField is a component
that allows editing of a single line of text. JTextField inherits the JTextComponent
class and uses the interface SwingConstants.
The constructor of the class are :
1. JTextField() : constructor that creates a new TextField
2. JTextField(int columns) : constructor that creates a new empty TextField
with specified number of columns.
3. JTextField(String text) : constructor that creates a new empty text field
initialized with the given string.
4. JTextField(String text, int columns) : constructor that creates a new empty
textField with the given string and a specified number of columns .
5. JTextField(Document doc, String text, int columns) : constructor that
creates a textfield that uses the given text storage model and the given number
of columns.
Methods of the JTextField are:
1. setColumns(int n) :set the number of columns of the text field.
2. setFont(Font f) : set the font of text displayed in text field.
3. addActionListener(ActionListener l) : set an ActionListener to the text field.
4. int getColumns() :get the number of columns in the textfield.
Following are the programs to implement JTextField.
1. Program to create a blank text field of definite number of columns.
// Java program to create a blank text
// field of definite number of columns.
import java.awt.event.*;
import javax.swing.*;
class text extends JFrame implements ActionListener {
// JTextField
static JTextField t;
// JFrame
static JFrame f;
// JButton
JAVA SWINGS
static JButton b;
// label to diaplay text
static JLabel l;
// default constructor
text()
{
}
// main class
public static void main(String[] args)
{
// create a new frame to stor text field and button
f = new JFrame("textfield");
// create a label to display text
l = new JLabel("nothing entered");
// create a new button
b = new JButton("submit");
// create a object of the text class
text te = new text();
// addActionListener to button
b.addActionListener(te);
// create a object of JTextField with 16 columns
t = new JTextField(16);
// create a panel to add buttons and textfield
JPanel p = new JPanel();
// add buttons and textfield to panel
p.add(t);
p.add(b);
p.add(l);
// add panel to frame
f.add(p);
// set the size of frame
f.setSize(300, 300);
f.show();
}
// if the vutton is pressed
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if (s.equals("submit")) {
// set the text of the label to the text of the field
l.setText(t.getText());
// set the text of field to blank
JAVA SWINGS
t.setText(" ");
}
}
}
JTextArea:
JTextArea is a part of java Swing package . It represents a multi line area that
displays text. It is used to edit the text .
JTextArea inherits JComponent class. The text in JTextArea can be set to different
available fonts and can be appended to new text . A text area can be customized to
the need of user .
Constructors of JTextArea are:
1. JTextArea() : constructs a new blank text area .
2. JTextArea(String s) : constructs a new text area with a given initial text.
3. JTextArea(int row, int column) : constructs a new text area with a given
number of rows and columns.
4. JTextArea(String s, int row, int column) : constructs a new text area with a
given number of rows and columns and a given initial text.
Commonly used methods :
1. append(String s) : appends the given string to the text of the text area.
2. getLineCount() : get number of lines in the text of text area.
3. setFont(Font f) : sets the font of text area to the given font.
4. setColumns(int c) : sets the number of columns of the text area to given
integer.
5. setRows(int r) : sets the number of rows of the text area to given integer.
6. getColumns() : get the number of columns of text area.
7. getRows() : get the number of rows of text area.
Eg Program to create a simple JTextArea
// Java Program to create a simple JTextArea
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class text extends JFrame implements ActionListener {
// JFrame
static JFrame f;
// JButton
static JButton b;
// label to diaplay text
static JLabel l;
// text area
static JTextArea jt;
// default constructor
text()
{
}
JAVA SWINGS
// main class
public static void main(String[] args)
{
// create a new frame to stor text field and button
f = new JFrame("textfield");
// create a label to display text
l = new JLabel("nothing entered");
// create a new button
b = new JButton("submit");
// create a object of the text class
text te = new text();
// addActionListener to button
b.addActionListener(te);
// create a text area, specifying the rows and columns
jt = new JTextArea(10, 10);
JPanel p = new JPanel();
// add the text area and button to panel
p.add(jt);
p.add(b);
p.add(l);
f.add(p);
// set the size of frame
f.setSize(300, 300);
f.show();
}
// if the button is pressed
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if (s.equals("submit")) {
// set the text of the label to the text of the field
l.setText(jt.getText());
}
}
}
JButton:
The JButton class is used to create a labeled button that has platform independent
implementation. The application result in some action when the button is pushed. It
inherits AbstractButton class.
Constructor:
Constructor Description
JButton() JAVA SWINGS
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.
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.
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.
Eg Program:
import javax.swing.*;
public class ButtonExample {
public static void main(String[] args) {
JFrame f=new JFrame("Button Example");
JButton b=new JButton("Click Here");
b.setBounds(50,100,95,30);
f.add(b);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
JCheckBox:
The JCheckBox class is used to create a checkbox. It is used to turn an option on (true)
or off (false). Clicking on a CheckBox changes its state from "on" to "off" or from "off" to
"on ".It inherits JToggleButton class.
JAVA SWINGS
Commonly used Constructors:
Constructor Description
JJCheckBox() Creates an initially unselected check box button with no
text,
no icon.
JChechBox(String s) Creates an initially unselected check box with text.
JCheckBox(String text, boolean Creates a check box with text and specifies whether or
selected) not it is
initially selected.
JCheckBox(Action a) Creates a check box where properties are taken from the
Action supplied.
Commonly used Methods:
Methods Description
AccessibleContext getAccessibleContext() It is used to get the AccessibleContext associated with
this JCheckBox.
protected String paramString() It returns a string representation of this JCheckBox.
Eg Program:
import javax.swing.*;
public class CheckBoxExample
{
CheckBoxExample(){
JFrame f= new JFrame("CheckBox Example");
JCheckBox checkBox1 = new JCheckBox("C++");
JAVA SWINGS
checkBox1.setBounds(100,100, 50,50);
JCheckBox checkBox2 = new JCheckBox("Java", true);
checkBox2.setBounds(100,150, 50,50);
f.add(checkBox1);
f.add(checkBox2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new CheckBoxExample();
}
}
JRadioButton:
The JRadioButton class is used to create a radio button. It is used to choose one option
from multiple options. It is widely used in exam systems or quiz.
It should be added in ButtonGroup to select one radio button only.
Commonly used Constructors:
Constructor Description
JRadioButton() Creates an unselected radio button with no text.
JRadioButton(String s) Creates an unselected radio button with specified text.
JRadioButton(String s, boolean Creates a radio button with the specified text and
selected) selected
status.
Commonly used Methods:
Methods Description
JAVA SWINGS
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.
Eg program:
import javax.swing.*;
public class RadioButtonExample {
JFrame f;
RadioButtonExample(){
f=new JFrame();
JRadioButton r1=new JRadioButton("A) Male");
JRadioButton r2=new JRadioButton("B) Female");
r1.setBounds(75,50,100,30);
r2.setBounds(75,100,100,30);
ButtonGroup bg=new ButtonGroup();
bg.add(r1);bg.add(r2);
f.add(r1);f.add(r2);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String[] args) {
new RadioButtonExample();
}
}
JAVA SWINGS
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.
Commonly used Constructors:
Constructor Description
JComboBox() Creates a JComboBox with a default data model.
JComboBox(Object[] items) Creates a JComboBox that contains the elements in the
specified array.
JComboBox(Vector<?> Creates a JComboBox that contains the elements in the
items) specified Vector.
Commonly used Methods:
Methods Description
void addItem(Object anObject) It is used to add an item to the item list.
void removeItem(Object anObject) It is used to delete an item to the item list.
void removeAllItems() It is used to remove all the items from the list.
void setEditable(boolean b) It is used to determine whether the JComboBox is
editable.
void addActionListener(ActionListener It is used to add the ActionListener.
a)
void addItemListener(ItemListener i) It is used to add the ItemListener.
JAVA SWINGS
Eg Program:
import javax.swing.*;
public class ComboBoxExample {
JFrame f;
ComboBoxExample(){
f=new JFrame("ComboBox Example");
String country[]={"India","Aus","U.S.A","England","Newzealand"};
JComboBox cb=new JComboBox(country);
cb.setBounds(50, 50,90,20);
f.add(cb);
f.setLayout(null);
f.setSize(400,500);
f.setVisible(true);
}
public static void main(String[] args) {
new ComboBoxExample();
}
}
JList:
The object of JList class represents a list of text items. The list of text items can be set
up so that the user can choose either one item or multiple items. It inherits JComponent
class.
Commonly used Constructors:
Constructor Description
JList() Creates a JList with an empty, read-only, model.
JList(ary[] listData) Creates a JList that displays the elements in the specified array.
JList(ListModel<ary> Creates a JList that displays elements from the specified, non-
dataModel) null,
model.
JAVA SWINGS
Commonly used Methods:
Methods Description
Void addListSelectionListener(ListSelectionListener It is used to add a listener to the list, to be
listener) notified
each time a change to the selection
occurs.
int getSelectedIndex() It is used to return the smallest selected
cell index.
ListModel getModel() It is used to return the data model that
holds a list
Of items displayed by the JList
component.
void setListData(Object[] listData) It is used to create a read-only ListModel
from
an array of objects.
Eg Program:
import javax.swing.*;
public class ListExample
{
ListExample(){
JFrame f= new JFrame();
DefaultListModel<String> l1 = new DefaultListModel<>();
l1.addElement("Item1");
l1.addElement("Item2");
l1.addElement("Item3");
l1.addElement("Item4");
JList<String> list = new JList<>(l1);
list.setBounds(100,100, 75,75);
f.add(list);
f.setSize(400,400);
JAVA SWINGS
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new ListExample();
}}
JPasswordField:
The object of a JPasswordField class is a text component specialized for password entry.
It allows the editing of a single line of text. It inherits JTextField class.
Commonly used Constructors:
Constructor Description
JPasswordField() Constructs a new JPasswordField, with a default document, null
starting text string, and 0 column width.
JPasswordField(int columns) Constructs a new empty JPasswordField with the specified
number of columns.
JPasswordField(String text) Constructs a new JPasswordField initialized with the specified
text.
JPasswordField(String text, int Construct a new JPasswordField initialized with the specified
columns) text and columns.
Eg Program:
import javax.swing.*;
public class PasswordFieldExample {
public static void main(String[] args) {
JFrame f=new JFrame("Password Field Example");
JPasswordField value = new JPasswordField();
JLabel l1=new JLabel("Password:");
l1.setBounds(20,100, 80,30);
value.setBounds(100,100,100,30);
JAVA SWINGS
f.add(value); f.add(l1);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
}