Exploring Swing: by K.Sindhujaa 20PA22
Exploring Swing: by K.Sindhujaa 20PA22
By
K.Sindhujaa
20PA22
Swing:-
• Swing is a GUI widget toolkit for Java
• The javax.swing package provides classes for java swing API such as JButton,
JTextField, JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etc.
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 only text. The text can’t be edited
by the User directly. It inherits JComponent class. By default labels are
vertically centered but the user can change the alignment of label.
Constructor of the class are :
JLabel() : creates a blank label with no text or image in it.
JLabel(String s) : creates a new label with the string specified.
JLabel(Icon i) : creates a new label with a image on it.
JLabel(String s, Icon i, int align) : creates a new label with a string, an image and a
specified horizontal alignment
Commonly used methods of the class are :
getIcon() : returns the image that that the label displays
setIcon(Icon i) : sets the icon that the label will display to image i
getText() : returns the text that the label will display
setText(String s) : sets the text that the label will display to string s
Example Program:-
import javax.swing.*;
class Label
{
public static void main(String args[])
{
JLabel l1,l2;
l1.setBounds(50,50, 100,30);
l2.setBounds(50,100, 100,30);
f.add(l1); f.add(l2);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
}
Sample Output :-
Java JtextField :-
The object of a JTextField class is a text component that allows the editing of a
single line text.
The constructor of the class are :
JTextField() : constructor that creates a new TextField
JTextField(int columns) : constructor that creates a new empty TextField
with specified number of columns.
JTextField(String text) : constructor that creates a new empty text field
initialized with the given string.
JTextField(String text, int columns) : constructor that creates a new empty
textField with the given string and a specified number of columns .
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:
setColumns(int n) :set the number of columns of the text field.
setFont(Font f) : set the font of text displayed in text field.
addActionListener(ActionListener l) : set an ActionListener to the text field.
int getColumns() :get the number of columns in the textfield.
Sample Program :-
import javax.swing.*;
class TextFieldExample
{
public static void main(String args[])
{
JFrame f= new JFrame("TextField Example");
JTextField t1,t2;
t1=new JTextField();
t1.setBounds(50,100, 200,30);
t2=new JTextField();
t2.setBounds(50,150, 200,30);
f.add(t1); f.add(t2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
Sample Output:
Java JButtons :-
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.
JList(ary[] listData) Creates a JList that displays the elements in the specified
array.
ListModel getModel() It is used to return the data model that holds a list of items
displayed by the JList component.
l1.addElement("Item1");
l1.addElement("Item2");
l1.addElement("Item3");
l1.addElement("Item4");
list.setBounds(100,100, 75,75);
f.add(list);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
Sample output:-
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.
Java JTree:-
The JTree class is used to display the tree structured data or hierarchical
data. JTree is a complex component. It has a 'root node' at the top most which is
a parent for all nodes in the tree.