1.
Introduction to Swing
Definition
Swing is a part of Java Foundation Classes used to build platform■independent graphical user
interfaces.
Syntax
import [Link].*;
Example
Creating a simple Swing window.
Program
import [Link].*;
class Demo{
public static void main(String args[]){
JFrame f=new JFrame("Swing");
[Link](300,200);
[Link](true);
}
}
Output
A simple window appears.
2. Hierarchy of Swing Components
Definition
The Swing class hierarchy shows the relationship between containers and components used in GUI
applications.
Syntax
JComponent -> JButton, JLabel, JTextField
Example
Understanding component relationships.
Program
import [Link].*;
class Demo{
public static void main(String args[]){
JFrame f=new JFrame();
JLabel l=new JLabel("Hierarchy Example");
[Link](l);
[Link](300,200);
[Link](true);
}
}
Output
Window with label displayed.
3. Top Level Containers
Definition
Top level containers are the main windows that hold Swing components such as JFrame, JDialog and
JWindow.
Syntax
JFrame frame = new JFrame();
Example
Using a JFrame as main container.
Program
import [Link].*;
class Demo{
public static void main(String args[]){
JFrame f=new JFrame("Container");
[Link](300,200);
[Link](true);
}
}
Output
Main application window appears.
4. JFrame
Definition
JFrame is a top■level container used to create a main window in Swing applications.
Syntax
JFrame f = new JFrame();
Example
Creating a frame window.
Program
import [Link].*;
class Demo{
public static void main(String args[]){
JFrame f=new JFrame("Frame");
[Link](300,200);
[Link](true);
}
}
Output
Frame window displayed.
5. JWindow
Definition
JWindow is a borderless top■level container used for splash screens or temporary windows.
Syntax
JWindow w = new JWindow();
Example
Simple window without title bar.
Program
import [Link].*;
class Demo{
public static void main(String args[]){
JWindow w=new JWindow();
[Link](300,200);
[Link](true);
}
}
Output
A plain window without border appears.
6. JDialog
Definition
JDialog is used to create dialog boxes for user interaction such as messages or confirmations.
Syntax
JDialog d = new JDialog();
Example
Displaying dialog window.
Program
import [Link].*;
class Demo{
public static void main(String args[]){
JDialog d=new JDialog();
[Link](200,150);
[Link](true);
}
}
Output
Dialog box displayed.
7. JPanel
Definition
JPanel is a lightweight container used to organize and group Swing components.
Syntax
JPanel p = new JPanel();
Example
Grouping components in panel.
Program
import [Link].*;
class Demo{
public static void main(String args[]){
JFrame f=new JFrame();
JPanel p=new JPanel();
[Link](new JButton("OK"));
[Link](p);
[Link](300,200);
[Link](true);
}
}
Output
Panel containing button appears.
8. JButton
Definition
JButton is a push button component used to perform an action when clicked.
Syntax
JButton b = new JButton("Click");
Example
Button inside a frame.
Program
import [Link].*;
class Demo{
public static void main(String args[]){
JFrame f=new JFrame();
JButton b=new JButton("Click");
[Link](b);
[Link](300,200);
[Link](true);
}
}
Output
Button displayed in window.
9. JToggleButton
Definition
JToggleButton represents a two■state button that can be toggled between selected and deselected states.
Syntax
JToggleButton t = new JToggleButton();
Example
Toggle button example.
Program
import [Link].*;
class Demo{
public static void main(String args[]){
JFrame f=new JFrame();
JToggleButton t=new JToggleButton("Toggle");
[Link](t);
[Link](300,200);
[Link](true);
}
}
Output
Toggle button appears.
10. JCheckBox
Definition
JCheckBox allows users to select or deselect an option in a GUI.
Syntax
JCheckBox c = new JCheckBox("Java");
Example
Checkbox selection example.
Program
import [Link].*;
class Demo{
public static void main(String args[]){
JFrame f=new JFrame();
JCheckBox c=new JCheckBox("Java");
[Link](c);
[Link](300,200);
[Link](true);
}
}
Output
Checkbox displayed.
11. JRadioButton
Definition
JRadioButton allows users to choose only one option from a group of options.
Syntax
JRadioButton r = new JRadioButton("Option");
Example
Radio button selection.
Program
import [Link].*;
class Demo{
public static void main(String args[]){
JFrame f=new JFrame();
JRadioButton r=new JRadioButton("Option");
[Link](r);
[Link](300,200);
[Link](true);
}
}
Output
Radio button appears.
12. JLabel
Definition
JLabel is used to display a short text string or image in a GUI.
Syntax
JLabel l = new JLabel("Text");
Example
Displaying text label.
Program
import [Link].*;
class Demo{
public static void main(String args[]){
JFrame f=new JFrame();
JLabel l=new JLabel("Hello");
[Link](l);
[Link](300,200);
[Link](true);
}
}
Output
Label appears.
13. JTextField
Definition
JTextField allows the user to enter a single line of text.
Syntax
JTextField t = new JTextField(20);
Example
Text input field example.
Program
import [Link].*;
class Demo{
public static void main(String args[]){
JFrame f=new JFrame();
JTextField t=new JTextField(20);
[Link](t);
[Link](300,200);
[Link](true);
}
}
Output
Text field displayed.
14. JTextArea
Definition
JTextArea is used for entering or displaying multiple lines of text.
Syntax
JTextArea t = new JTextArea();
Example
Multi■line text input.
Program
import [Link].*;
class Demo{
public static void main(String args[]){
JFrame f=new JFrame();
JTextArea t=new JTextArea();
[Link](t);
[Link](300,200);
[Link](true);
}
}
Output
Text area appears.
15. JList
Definition
JList displays a list of items from which a user can select one or more items.
Syntax
JList list = new JList();
Example
List selection example.
Program
import [Link].*;
class Demo{
public static void main(String args[]){
JFrame f=new JFrame();
String items[]={"Java","Python","C"};
JList l=new JList(items);
[Link](l);
[Link](300,200);
[Link](true);
}
}
Output
List of items displayed.
16. JComboBox
Definition
JComboBox provides a drop■down list from which the user can select an item.
Syntax
JComboBox c = new JComboBox();
Example
Dropdown selection example.
Program
import [Link].*;
class Demo{
public static void main(String args[]){
JFrame f=new JFrame();
String items[]={"Java","C++","Python"};
JComboBox cb=new JComboBox(items);
[Link](cb);
[Link](300,200);
[Link](true);
}
}
Output
Combo box appears.
17. JScrollPane
Definition
JScrollPane provides scroll bars to view components that are larger than the display area.
Syntax
JScrollPane sp = new JScrollPane(component);
Example
Scrollable text area.
Program
import [Link].*;
class Demo{
public static void main(String args[]){
JFrame f=new JFrame();
JTextArea t=new JTextArea(10,20);
JScrollPane sp=new JScrollPane(t);
[Link](sp);
[Link](300,200);
[Link](true);
}
}
Output
Scrollable text area displayed.