NEP 2020 Version II S.Y. B.C.A.
(AICTE) 25-26
,
Assignment No 6:- GUI Designing Using Swing
Theory :
Introduction to Swing
Swing is a part of Java Foundation Classes (JFC) used to create Graphical User Interface (GUI)
applications.
Swing components are lightweight, platform-independent, and written completely in Java. Swing
provides a rich set of components such as buttons, labels, text fields, text areas, menus, dialog
boxes and file chooser classes.
Swing Architecture:
The design of the Swing component classes is based on the Model-View-Controller architecture,
or MVC.
1. The model stores the data.
2. The view creates the visual representation from the data in the model.
3. The controller deals with user interaction and modifies the model and/or the view.
Layout Manager:
The job of a layout manager is to arrange components on a container.
Each container has a layout manager associated with it. To change the layout manager for a
container, use the setLayout() method.
Syntax:
setLayout(LayoutManager obj)
Types of Layout Managers:
AWT package provides following types of Layout Managers
1. Flow Layout
2. Border Layout
3. Card Layout
4. Grid Layout
5. Grid Bag Layout
Examples:
Asst. Prof. [Link] Modern College of Arts Science and
Commerce,Ganeshkhind,Pune-16.
1
NEP 2020 Version II S.Y. B.C.A. (AICTE) 25-26
,
JPanel p1 = new JPanel ()
[Link] (new FlowLayout());
[Link] (new BorderLayout ());
[Link] (new GridLayout (3,4));
1. Flow Layout:
This layout will display the components in sequence from left to right, from top
to bottom.
Costructor:
FlowLayout f1 = new FlowLayout ();
FlowLayout f1 = new FlowLayout (int align);
FlowLayout f1 = new FlowLayout (int align, int hgap, int vgap);
For Example : Java Program to demonstrate Flow Layout Manager
import [Link].*;
import [Link].*;
public class FlowLayoutDemo
{
JFrame f;
FlowLayoutDemo ()
{
f = new JFrame ();
JLabel l1 = new JLabel ("Enter Name");
JTextField tf1 = new JTextField (10);
JButton b1 = new JButton ("SUBMIT");
[Link] (l1);
[Link] (tf1);
[Link] (b1);
[Link] (new FlowLayout ([Link]));
//setting flow layout of right alignment
[Link] (300, 300);
[Link] (true);
}
Asst. Prof. [Link] Modern College of Arts Science and
Commerce,Ganeshkhind,Pune-16.
2
NEP 2020 Version II S.Y. B.C.A. (AICTE) 25-26
,
public static void main (String[]args)
{
new FlowLayoutDemo ();
}
}
2. Border Layout:
This layout will display the components along the border of the container. This layout contains
five locations. Locations are North, South, East, West, and Center. The default region is the
Center.
Costructor:
BorderLayout bl = new BorderLayout();
BorderLayout bl = new BorderLayout(int vgap, int hgap);
For Example : Java Program to demonstrate Border Layout Manager.
import [Link].*;
public class BorderLayoutDemo
{
public static void main (String[]args)
{
Frame f1 = new Frame ();
Asst. Prof. [Link] Modern College of Arts Science and
Commerce,Ganeshkhind,Pune-16.
3
NEP 2020 Version II S.Y. B.C.A. (AICTE) 25-26
,
[Link] (250, 250);
Button b1 = new Button ("Button1");
Button b2 = new Button ("Button2");
Button b3 = new Button ("Button3");
Button b4 = new Button ("Button4");
Button b5 = new Button ("Button5");
[Link] (b1, [Link]);
[Link] (b2, [Link]);
[Link] (b3, [Link]);
[Link] (b4, [Link]);
[Link] (b5);
[Link] (true);
}
}
Output:
[Link] Layout:
A card layout represents a stack of cards displayed on a container. At a time only one card can be
visible and each can contain the only component.
Costructor :
CardLayout cl = new CardLayout();
CardLayout cl = new CardLayout(int hgap, int vgap);
To add the components in CardLayout we use add method:
add(“Cardname”, Component);
Methods of CardLayout
1. first(Container): It is used to flip to the first card of the given container.
2. last(Container): It is used to flip to the last card of the given container.
3. next(Container): It is used to flip to the next card of the given container.
4. previous(Container): It is used to flip to the previous card of the given container.
Asst. Prof. [Link] Modern College of Arts Science and
Commerce,Ganeshkhind,Pune-16.
4
NEP 2020 Version II S.Y. B.C.A. (AICTE) 25-26
,
5. show(Container, cardname): It is used to flip to the specified card with the given name.
For Example: Java Program to demonstrate Card Layout Manager.
import [Link].*;
import [Link].*;
import [Link];
import [Link].*;
public class CardLayoutDemo extends JFrame implements ActionListener
{
JButton b1, b2, b3, b4, b5;
CardLayout cl;
Container c;
CardLayoutDemo ()
{
b1 = new JButton ("Button1");
b2 = new JButton ("Button2");
b3 = new JButton ("Button3");
b4 = new JButton ("Button4");
b5 = new JButton ("Button5");
c = [Link] ();
cl = new CardLayout (10, 20);
[Link] (cl);
[Link] ("Card1", b1);
[Link] ("Card2", b2);
[Link] ("Card3", b3);
[Link] (this);
[Link] (this);
[Link] (this);
setVisible (true);
setSize (400, 400);
setTitle ("Card Layout");
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
Asst. Prof. [Link] Modern College of Arts Science and
Commerce,Ganeshkhind,Pune-16.
5
NEP 2020 Version II S.Y. B.C.A. (AICTE) 25-26
,
public void actionPerformed (ActionEvent ae)
{
[Link] (c);
} public static void main (String[]args)
{
new CardLayoutDemo ();
}
}
Output:
4. Grid Layout:
The layout will display the components in the format of rows and columns
statically. The container will be divided into a table of rows and columns.
Costructor:
GridLayout gl = new GridLayout(int rows, int cols);
GridLayout gl = new GridLayout(int rows, int cols, int vgap, int hgap);
For Example: Java Program to demonstrate Grid Layout Manager.
import [Link].*;
import [Link].*;
public class GridLayoutDemo
{
public static void main (String[]args)
{
Asst. Prof. [Link] Modern College of Arts Science and
Commerce,Ganeshkhind,Pune-16.
6
NEP 2020 Version II S.Y. B.C.A. (AICTE) 25-26
,
Frame f1 = new Frame ();
[Link] (250, 250);
GridLayout ob = new GridLayout (2, 2);
[Link] (ob);
Panel p1 = new Panel ();
Label l1 = new Label ("Enter name");
TextField tf = new TextField (10);
Button b1 = new Button ("Submit");
[Link] (l1);
[Link] (tf);
[Link] (b1);
[Link] (p1);
Panel p2 = new Panel ();
[Link] (p2);
Panel p3 = new Panel ();
[Link] (p3);
Label l2 = new Label ("Welcome to Java");
[Link] (l2);
[Link] (true); } }
Output:
Asst. Prof. [Link] Modern College of Arts Science and
Commerce,Ganeshkhind,Pune-16.
7
NEP 2020 Version II S.Y. B.C.A. (AICTE) 25-26
Container in Swing:
[Link]
JFrame is a top-level container used to create the main application window.
Syntax:
JFrame f = new JFrame();
[Link]
JPanel is an intermediate container used to group Swing components.
Syntax:
JPanel p = new JPanel();
Swing Components and Syntax
JLabel – Displays text
JLabel l = new JLabel("Text");
JButton – Performs an action on click
JButton b = new JButton("Button");
JTextField – Accepts single-line input
JTextField t = new JTextField(15);
JTextArea – Accepts multi-line input
JTextArea ta = new JTextArea(5,20);
JCheckBox – Allows multiple selections
JCheckBox c = new JCheckBox("Option");
JRadioButton – Allows single selection
JRadioButton r = new JRadioButton("Choice");
JList – Displays list of items
JList list = new JList(data);
JComboBox – Displays drop-down list
JComboBox cb = new JComboBox(data);
Asst. Prof. [Link] Modern College of Arts Science and
Commerce,Ganeshkhind,Pune-16.
8
NEP 2020 Version II S.Y. B.C.A. (AICTE) 25-26
,
Dialog Boxes (JOptionPane)
Message Dialog
[Link](f,"Message");
Confirmation Dialog
[Link](f,"Confirm?");
Input Dialog
[Link]("Enter value");
JFileChooser
JFileChooser is used to open or save files using a standard dialog box.
Syntax:
JFileChooser fc = new JFileChooser();
[Link](f);
Sample Swing Program
This program demonstrates JFrame, JLabel, JTextField, JTextArea, JButton and simple event handling
using Swing.
import [Link].*;
import [Link].*;
import [Link].*;
class SampleSwingProgram{
public static void main(String args[]) {
JFrame f = new JFrame("Sample Swing Program");
[Link](400,300);
Asst. Prof. [Link] Modern College of Arts Science and
Commerce,Ganeshkhind,Pune-16.
9
NEP 2020 Version II S.Y. B.C.A. (AICTE) 25-26
,
[Link](new FlowLayout());
[Link](JFrame.EXIT_ON_CLOSE);
JLabel l1 = new JLabel("Enter Name:");
JTextField t1 = new JTextField(15);
JTextArea ta = new JTextArea(5,25);
JButton b1 = new JButton("Click Me");
[Link](l1);
[Link](t1);
[Link](b1);
[Link](ta);
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
[Link]("Welcome " + [Link]() + " to Java Swing!");
});
[Link](true); } }
Asst. Prof. [Link] Modern College of Arts Science and
Commerce,Ganeshkhind,Pune-16.
10
NEP 2020 Version II S.Y. B.C.A. (AICTE) 25-26
SET A
1. Write a Java Swing program to create a basic window using JFrame and display a welcome
message using JLabel.
2. Write a Java Swing program using JLabel, JTextField and JButton to accept user name and
display greeting message.
3. Write a Java Swing program to demonstrate JTextArea.
4. Write a Java Swing program to demonstrate FlowLayout.
5. Write a Java Swing program using BorderLayout.
SET B
1. Write a Java Swing program using JCheckBox and JRadioButton.
2. Write a Java Swing program using JList and JComboBox.
3. Write a Java Swing program using JMenuBar, JMenu and JMenuItem.
4. Write a Java Swing program to demonstrate Message, Confirmation and Input dialogs using
JOptionPane.
5. Write a Java Swing program using JFileChooser.
Assignment Evaluation
0: Not Done [ ] 1: Incomplete [ ] 2: Late Complete [ ]
3: Needs Improvement [ ] 4: Complete [ ] 5: Well Done [ ]
Signature of the instructor:
Asst. Prof. [Link] Modern College of Arts Science and
Commerce,Ganeshkhind,Pune-16.
11
NEP 2020 Version II S.Y. B.C.A. (AICTE) 25-26
Asst. Prof. [Link] Modern College of Arts Science and
Commerce,Ganeshkhind,Pune-16.
12
NEP 2020 Version II S.Y. B.C.A. (AICTE) 25-26
Asst. Prof. [Link] Modern College of Arts Science and
Commerce,Ganeshkhind,Pune-16.
13
NEP 2020 Version II S.Y. B.C.A. (AICTE) 25-26
Asst. Prof. [Link] Modern College of Arts Science and
Commerce,Ganeshkhind,Pune-16.
14
NEP 2020 Version II S.Y. B.C.A. (AICTE) 25-26
Asst. Prof. [Link] Modern College of Arts Science and
Commerce,Ganeshkhind,Pune-16.
15
NEP 2020 Version II S.Y. B.C.A. (AICTE) 25-26
Asst. Prof. [Link] Modern College of Arts Science and
Commerce,Ganeshkhind,Pune-16.
16