0% found this document useful (0 votes)
3 views3 pages

Java Exp1 Neel

The document outlines an experiment for creating a Java GUI using Swing that includes various components such as text boxes, check boxes, radio buttons, and a submit button. The objective is to help students understand the structure of Java Swing and implement event handling. The provided code demonstrates how to set up the GUI and handle form submission with a confirmation dialog.
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)
3 views3 pages

Java Exp1 Neel

The document outlines an experiment for creating a Java GUI using Swing that includes various components such as text boxes, check boxes, radio buttons, and a submit button. The objective is to help students understand the structure of Java Swing and implement event handling. The provided code demonstrates how to set up the GUI and handle form submission with a confirmation dialog.
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

Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore Shri

Vaishnav Institute of Information Technology


Advanced JAVA BTIT411

Experiment No.: 1

Title: A Java program for creating text box, check box, radio button, list, and submit button
using Swing.

Objective: To design a simple Java Graphical User Interface (GUI) that collects user input
using various form controls such as JTextField, JCheckBox, JRadioButton, JComboBox, and
JButton.

Outcome: After successful completion of this program, the student will be able to:

 Understand basic Java Swing GUI structure.


 Create text input fields using JTextField.
 Use JCheckBox for multiple selection.
 Use JRadioButton and ButtonGroup for single selection.
 Create dropdown lists using JComboBox.
 Implement event handling for the submit button.

Theory: Java Swing is a part of Java Foundation Classes (JFC) used to create window-based
applications. It provides lightweight components that work the same across all platforms.

+1

1. JTextField: Allows users to enter a single line of text, such as a name or email.

+1

2. JCheckBox: Allows users to select multiple options from a set (e.g., hobbies).
3. JRadioButton: Allows users to select only one option from a group. These must be
added to a ButtonGroup to ensure only one is selectable at a time.

+1

4. JComboBox: Provides a dropdown list of options in a compact form (e.g., course


selection).
5. JButton: An implementation of a "push" button used to trigger an action or process
form data.

Code:

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

public class StudentForm extends JFrame {

23100BTCSE14979 Neelabh shrivastava


Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore Shri
Vaishnav Institute of Information Technology
Advanced JAVA BTIT411

public StudentForm() {
// Setting up the Frame [cite: 49, 53]
setTitle("Student Form");
setSize(350, 450);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(10, 1, 5, 5)); // Layout for spacing
[cite: 54, 72]

// 1. Text Box (Name) [cite: 101, 102]


add(new JLabel("Name:"));
JTextField nameField = new JTextField();
add(nameField);

// 2. Check Box (Hobbies) [cite: 104, 105]


add(new JLabel("Hobbies:"));
JPanel hobbyPanel = new JPanel(new FlowLayout([Link]));
JCheckBox cb1 = new JCheckBox("Reading"); [cite: 106]
JCheckBox cb2 = new JCheckBox("Sports"); [cite: 107]
JCheckBox cb3 = new JCheckBox("Music"); [cite: 108]
[Link](cb1); [Link](cb2); [Link](cb3);
add(hobbyPanel);

// 3. Radio Button (Gender) [cite: 109, 110]


add(new JLabel("Gender:"));
JPanel genderPanel = new JPanel(new FlowLayout([Link]));
JRadioButton male = new JRadioButton("Male"); [cite: 111]
JRadioButton female = new JRadioButton("Female"); [cite: 112]
ButtonGroup genderGroup = new ButtonGroup(); // Ensures single
selection
[Link](male); [Link](female);
[Link](male); [Link](female);
add(genderPanel);

// 4. List / Drop-down (Course) [cite: 113, 114]


add(new JLabel("Course:"));
String[] courses = {"[Link]", "BCA", "BBA", "MCA"}; [cite: 116,
117, 118, 119]
JComboBox<String> courseList = new JComboBox<>(courses);
add(courseList);

// 5. Submit Button [cite: 121, 122]


JButton submitBtn = new JButton("Submit");
[Link](new Color(0, 123, 255)); // Matches blue
style [cite: 86]
[Link]([Link]); [cite: 87]
add(submitBtn);

// Action for Submit Button


[Link](e -> {
[Link](this, "Form Submitted
Successfully!");
});

setVisible(true); // Makes the window appear [cite: 54]


}

23100BTCSE14979 Neelabh shrivastava


Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore Shri
Vaishnav Institute of Information Technology
Advanced JAVA BTIT411

public static void main(String[] args) {


new StudentForm();
}
}

Output: A desktop window titled "Student Form" appears with a text field for the name, three
checkboxes for hobbies, two radio buttons for gender, a dropdown menu for course selection,
and a blue "Submit" button. Clicking "Submit" triggers a confirmation dialog.

Output:

23100BTCSE14979 Neelabh shrivastava

You might also like