0% found this document useful (0 votes)
4 views12 pages

PR7 Java PDF

The document outlines an experiment for designing a Graphical User Interface (GUI) registration form using Java Swing, which includes various input components like text boxes, checkboxes, and radio buttons. It provides theoretical background on Swing, its components, and differences from AWT, along with a sample code for creating the GUI. The experiment aims to demonstrate the functionality of the registration form and achieve user-friendly interaction.

Uploaded by

sanmith.18171
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views12 pages

PR7 Java PDF

The document outlines an experiment for designing a Graphical User Interface (GUI) registration form using Java Swing, which includes various input components like text boxes, checkboxes, and radio buttons. It provides theoretical background on Swing, its components, and differences from AWT, along with a sample code for creating the GUI. The experiment aims to demonstrate the functionality of the registration form and achieve user-friendly interaction.

Uploaded by

sanmith.18171
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

KJSCE/IT/AI & DS/SY BTECH/SEM-III/PL/2024-

2025

Experiment No. 7

Title: Design Graphical User Interface

(A Constituent College of Somaiya Vidyavihar University)


KJSCE/IT/AI & DS/SY BTECH/SEM-III/PL/2024-
2025

Batch:B2 Roll No.: 16010423013 Experiment No.:07

Aim: Create GUI containing registration form which will accept input from the user and display
it on a button click. (Form should contain textbox, checkbox, radio-button, drop down menu etc.)
___________________________________________________________________________

Resources needed: Text Editor /IDE, JDK 1.8

Theory

Swing:Java Swing is a part of Java Foundation Classes (JFC) that is used to create window-
based applications. It is built on the top of AWT (Abstract Windowing Toolkit) API and
entirely written in [Link] AWT, Java Swing provides platform-independent and
lightweight components.

The [Link] package provides classes for java swing API such as JButton, JTextField,
JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etc.

There are many differences between java awt and swing that are given below.

No. Java AWT Java Swing


1) AWT components are platform- Java swing components are platform-
dependent. independent.
2) AWT components are heavyweight. Swing components are lightweight.
3) AWT doesn't support pluggable look Swing supports pluggable look and feel.
and feel.
4) AWT provides less components than Swing provides more powerful components such
Swing. as tables, lists, scrollpanes, colorchooser,
tabbedpane etc.
5) AWT doesn't follows MVC (Model Swing follows MVC.
View Controller) where model
represents data, view represents
presentation and controller acts as an
interface between model and view.

Swing Component:

Common constructor
Component Important methods Useful Listeners
parameters
Text
Icon
ActionListener
JButton Text/icon
Action
Text setSelected() ActionListener
JCheckBox Text/selected isSelected() ChangeListener

(A Constituent College of Somaiya Vidyavihar University)


KJSCE/IT/AI & DS/SY BTECH/SEM-III/PL/2024-
2025

setSelected()
Text ActionListener
isSelected()
JRadioButton Text/selected ChangeListener
[Link](...)

Array of items getSelectedValue() ListSelectionListe


ListModel getSelectedItem() ner

JList
No columns
JTextField Initial text DocumentListener
(Added to
getText()
Document object
setText()
No rows/columns returned
getDocument()
Rows/cols/text by getDocument().
)
JTextArea
addTab()
insertTab()
getSelectedComponent
Tab placement ChangeListener
()
setSelectedComponent
JTabbedPane ()

Component
Component, scrollbar setViewportView()
options
JScrollPane

Orientation, 2 components
For orientation,
use [Link] setDividerLocation()
AL_SPLIT or JScrollPane.V
ERTICAL_SPLIT.
JSplitPane
JComponent MouseListener

___________________________________________________________________________

Results: ( Code and Screen shots of GUI developed with Output )

(A Constituent College of Somaiya Vidyavihar University)


KJSCE/IT/AI & DS/SY BTECH/SEM-III/PL/2024-
2025

import [Link].*;
import [Link].*;
import [Link].*;

public class RegistrationForm {


public static void main(String[] args) {
JFrame frame = new JFrame("Registration Form");
[Link](450, 550);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();

// Colors and fonts


Color bgColor = new Color(245, 245, 245);
Color btnColor = new Color(0, 153, 204);
Font titleFont = new Font("Arial", [Link], 22);
Font labelFont = new Font("Arial", [Link], 14);

[Link]().setBackground(bgColor);

JLabel titleLabel = new JLabel("User Registration");


[Link](titleFont);
[Link](new Color(0, 102, 153));

JLabel nameLabel = new JLabel("Name:");


[Link](labelFont);
JTextField nameField = new JTextField(15);
[Link]([Link](new Color(180, 180, 180), 2,
true));

JLabel genderLabel = new JLabel("Gender:");


[Link](labelFont);
JRadioButton male = new JRadioButton("Male");
JRadioButton female = new JRadioButton("Female");
[Link](bgColor);
[Link](bgColor);
ButtonGroup genderGroup = new ButtonGroup();

(A Constituent College of Somaiya Vidyavihar University)


KJSCE/IT/AI & DS/SY BTECH/SEM-III/PL/2024-
2025

[Link](male);
[Link](female);

JLabel hobbiesLabel = new JLabel("Hobbies:");


[Link](labelFont);
JCheckBox reading = new JCheckBox("Reading");
JCheckBox traveling = new JCheckBox("Traveling");
JCheckBox coding = new JCheckBox("Coding");
[Link](bgColor);
[Link](bgColor);
[Link](bgColor);

JLabel countryLabel = new JLabel("Country:");


[Link](labelFont);
String[] countries = {"USA", "Canada", "India", "UK"};
JComboBox<String> countryBox = new JComboBox<>(countries);
[Link]([Link](new Color(180, 180, 180), 2,
true));

JButton submitButton = new JButton("Submit");


[Link](btnColor);
[Link]([Link]);
[Link](labelFont);
[Link](false);
[Link]([Link](btnColor, 2, true));
[Link]([Link](Cursor.HAND_CURSOR));

JTextArea outputArea = new JTextArea(6, 20);


[Link]([Link](new Color(180, 180, 180), 2,
true));
[Link](false);
JScrollPane scrollPane = new JScrollPane(outputArea);

[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = [Link]();
String gender = [Link]() ? "Male" : [Link]() ? "Female" : "Not
specified";

(A Constituent College of Somaiya Vidyavihar University)


KJSCE/IT/AI & DS/SY BTECH/SEM-III/PL/2024-
2025

StringBuilder hobbies = new StringBuilder();


if ([Link]()) [Link]("Reading ");
if ([Link]()) [Link]("Traveling ");
if ([Link]()) [Link]("Coding ");
String country = (String) [Link]();

[Link]("Name: " + name + "\nGender: " + gender + "\nHobbies: " +


hobbies + "\nCountry: " + country);
}
});

[Link] = new Insets(10, 10, 10, 10);

[Link] = 0;
[Link] = 0;
[Link] = 2;
[Link] = [Link];
[Link](titleLabel, gbc);

[Link] = 1;
[Link] = 0;
[Link] = 1;
[Link](nameLabel, gbc);

[Link] = 1;
[Link] = 1;
[Link](nameField, gbc);

[Link] = 0;
[Link] = 2;
[Link](genderLabel, gbc);

[Link] = 1;
[Link] = 2;
JPanel genderPanel = new JPanel(new FlowLayout([Link], 0, 0));
[Link](bgColor);
[Link](male);

(A Constituent College of Somaiya Vidyavihar University)


KJSCE/IT/AI & DS/SY BTECH/SEM-III/PL/2024-
2025

[Link](female);
[Link](genderPanel, gbc);

[Link] = 0;
[Link] = 3;
[Link](hobbiesLabel, gbc);

[Link] = 1;
[Link] = 3;
JPanel hobbiesPanel = new JPanel(new FlowLayout([Link], 0, 0));
[Link](bgColor);
[Link](reading);
[Link](traveling);
[Link](coding);
[Link](hobbiesPanel, gbc);

[Link] = 0;
[Link] = 4;
[Link](countryLabel, gbc);

[Link] = 1;
[Link] = 4;
[Link](countryBox, gbc);

[Link] = 0;
[Link] = 5;
[Link] = 2;
[Link] = [Link];
[Link] = [Link];
[Link](submitButton, gbc);

[Link] = 0;
[Link] = 6;
[Link] = 2;
[Link] = [Link];
[Link](scrollPane, gbc);

(A Constituent College of Somaiya Vidyavihar University)


KJSCE/IT/AI & DS/SY BTECH/SEM-III/PL/2024-
2025

[Link](true);
}
}

(A Constituent College of Somaiya Vidyavihar University)


KJSCE/IT/AI & DS/SY BTECH/SEM-III/PL/2024-
2025

___________________________________________________________________________

Questions:
1. Write a program for demonstration of JMenu, JTable and JComboBox
import [Link].*;
import [Link];
import [Link].*;
import [Link];
import [Link];

public class MenuTableComboBoxDemo {


public static void main(String[] args) {
JFrame frame = new JFrame("JMenu, JTable, JComboBox Demo");
[Link](600, 400);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](new BorderLayout());

JMenuBar menuBar = new JMenuBar();


JMenu fileMenu = new JMenu("File");
JMenu helpMenu = new JMenu("Help");

JMenuItem newItem = new JMenuItem("New");


JMenuItem exitItem = new JMenuItem("Exit");
JMenuItem aboutItem = new JMenuItem("About");

[Link](newItem);
[Link](exitItem);
[Link](aboutItem);

[Link](fileMenu);
[Link](helpMenu);

[Link](menuBar);

String[] countries = {"USA", "Canada", "India", "UK", "Australia"};


JComboBox<String> countryComboBox = new JComboBox<>(countries);

String[] columnNames = {"Country", "Population"};


String[][] data = {
{"USA", "331 million"},
{"Canada", "38 million"},
{"India", "1.4 billion"},
{"UK", "68 million"},

(A Constituent College of Somaiya Vidyavihar University)


KJSCE/IT/AI & DS/SY BTECH/SEM-III/PL/2024-
2025

{"Australia", "25 million"}


};

DefaultTableModel tableModel = new DefaultTableModel(data, columnNames);


JTable table = new JTable(tableModel);
JScrollPane tableScrollPane = new JScrollPane(table);

[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
String selectedCountry = (String) [Link]();
for (int i = 0; i < [Link](); i++) {
if ([Link](i, 0).equals(selectedCountry)) {
[Link](i, i);
}
}
}
});

[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
[Link](0);
}
});

[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
[Link](frame, "JMenu, JTable, JComboBox
Demo\nVersion 1.0", "About", JOptionPane.INFORMATION_MESSAGE);
}
});

JPanel panel = new JPanel(new BorderLayout());


[Link](countryComboBox, [Link]);
[Link](tableScrollPane, [Link]);

[Link](panel, [Link]);
[Link](true);
}
}

(A Constituent College of Somaiya Vidyavihar University)


KJSCE/IT/AI & DS/SY BTECH/SEM-III/PL/2024-
2025

Outcomes:

(A Constituent College of Somaiya Vidyavihar University)


KJSCE/IT/AI & DS/SY BTECH/SEM-III/PL/2024-
2025

Illustrate the use of collection classes, functional programming and GUI programming with
java.

Conclusion: (Conclusion to be based on the objectives and outcomes achieved)


the Java Swing registration form successfully meets the objectives by including various input
components (text box, radio buttons, dropdown, and checkbox) and displaying user inputs on
button click. It ensures all fields are filled, making the form functional and user-friendly,
achieving the desired outcome.

Grade: AA / AB / BB / BC / CC / CD/DD

Signature of faculty in-charge with date

References:

1. Herbert Schildt, “Java: The Complete Reference” Tata McGrawHill Publishing Company
Limited Tenth Edition, 2017
2. Sachin Malhotra, Saurab Choudhary, “Programming in Java” Oxford University Press
Second Edition, 2018 3.
3. D.T. Editorial Services, “Java 8 Programming Black Book” Dream tech Press Edition 2015
4. [Link]

(A Constituent College of Somaiya Vidyavihar University)

You might also like