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

Exploring Swing: by K.Sindhujaa 20PA22

Swing is a GUI widget toolkit for Java that provides platform-independent and lightweight components. It includes common widgets like JButton, JTextField, JLabel, JList, JComboBox, JTree, and JTable. JLabel displays text or images. JTextField allows editing single-line text. JButton triggers actions on click. JList displays a list of selectable items. JComboBox displays a dropdown list. JTree shows hierarchical data. JTable displays data in a table.

Uploaded by

sindhujaa k
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views

Exploring Swing: by K.Sindhujaa 20PA22

Swing is a GUI widget toolkit for Java that provides platform-independent and lightweight components. It includes common widgets like JButton, JTextField, JLabel, JList, JComboBox, JTree, and JTable. JLabel displays text or images. JTextField allows editing single-line text. JButton triggers actions on click. JList displays a list of selectable items. JComboBox displays a dropdown list. JTree shows hierarchical data. JTable displays data in a table.

Uploaded by

sindhujaa k
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Exploring Swing

By
K.Sindhujaa
20PA22
Swing:-
• Swing is a GUI widget toolkit for Java

• It is a part of Java Foundation Classes (JFC) that is used to create window-


based applications. It is entirely written in java.

• Unlike AWT, Java Swing provides platform-independent and lightweight


components.

• 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[])
{

JFrame f= new JFrame("Label Example");

JLabel l1,l2;

l1=new JLabel("First Label");

l1.setBounds(50,50, 100,30);

l2=new JLabel("Second Label");

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.

Commonly used Constructors:

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:

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.
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);   
}  
}  
Sample Output:-
Java 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.

Commonly used Constructors:

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> dataModel) Creates a JList that displays elements from


the specified, non-null, model.
Commonly used Methods:

Void addListSelectionListener(ListSelectionListener listener) It is used to add a


listener to the list, to be 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.
import javax.swing.*;

public class List

public static void main(String args[]){

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);

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.

Commonly used Constructors:


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<?> items) Creates a JComboBox that contains the elements in the
specified Vector.
Commonly used Methods:

Commonly used Methods:


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.
Commonly used Methods:

void addActionListener(ActionListener a) It is used to add the ActionListener.


void addItemListener(ItemListener i) It is used to add the ItemListener.
Commonly used Methods:

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.

Commonly used Constructors:


JTree() Creates a JTree with a sample model.
JTree(Object[] value) Creates a JTree with every element of the specified array as the
child of a new root node.
JTree(TreeNode root) Creates a JTree with the specified TreeNode as its root, which
displays the root node.
Java JTable:-
The JTable class is used to display data in tabular form. It is composed of
rows and columns.
Constructors in JTable:
JTable(): A table is created with empty cells.
JTable(int rows, int cols): Creates a table of size rows * cols.
JTable(Object[][] data, Object []Column): A table is created with the specified
name where []Column defines the column names.
Functions in JTable:
addColumn(TableColumn []column) : adds a column at the end of the JTable.
clearSelection() : Selects all the selected rows and columns.
editCellAt(int row, int col) : edits the intersecting cell of the column number
col and row number row programmatically, if the given indices are valid and the
corresponding cell is editable.
setValueAt(Object value, int row, int col) : Sets the cell value as ‘value’ for the
position row, col in the JTable.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy