Starter Tutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.
Home » Programming » Advanced Java » Swing » Java program to create sample application form using JFrame and swing controls
Suryateja Pericherla Categories: Swing. No Comments on Java program to create sample application form using JFrame and swing controls
Join Our Newsletter - Tips, Contests and Other Updates
Email
Name

Aim

Write a java program to create sample application form using JFrame and swing controls.

 

Theory

Java provides javax.swing package which does not need any help of the native operating system for creating GUI controls. Hence programs created using swings are portable.

 

Swings offers the following features:

  • Lightweight Components: The components created using swings package doesn’t need the help of native operating system. Swings is a part of Java Foundation Classes (JFC) which are purely developed in Java. Also swing components require less memory and CPU cycles than their counterpart AWT components. Hence swing components are called lightweight components.
  • Pluggable Look and Feel: Swings supports pluggable look and feel i.e., the appearance of a component can be separated from how the component behaves. This enables programmers to assign different look (themes) for the same component without changing its behavior.

 

Components and Containers

A swing GUI contains two main elements: components and containers. A component is an individual control like a button or label. A container is an object which can hold other components and containers.

 

Components

Swing components are derived from the class JComponent (except the four top-level containers). JComponent supports pluggable look and feel and supports the functionality common for all the components. JComponent class inherits the AWT classes Container and Component.

 

All swing components are represented as classes and are present in javax.swing package. Some of the components available in the Swings package are listed below:

Java Swing Component Classes

Containers

Swing provides two types of containers. First are top-level containers also called heavyweight containers. These are at the top in the containment hierarchy. The top-level containers are JFrame, JApplet, JWindow, and JDialog. All the containers inherit the AWT classes Component and Container.

 

Second are the lightweight containers which can be nested inside top-level containers. Lightweight containers are inherited from the class JComponent. Example of a lightweight container is JPanel.

 

Layout Managers

A layout manager is one which automatically manages the arrangement of various of components in a container. Each container will have a default layout manager.

 

The layout manager can be set by using the setLayout() method whose general form is as follows:

void setLayout(LayoutManager layoutObj)

 

We can manually arrange the position of each component (not recommended) by passing null to setLayout() method and by using setBounds() method on each component. Different layout managers available are:

  • FlowLayout
  • BorderLayout
  • GridLayout
  • CardLayout
  • GridBagLayout

 

FlowLayout Manager

The flow layout manager arranges the components one after another from left-to-right and top-to-bottom manner. The flow layout manager gives some space between components. Flow layout manager instance can be created using anyone of the following constructors:

  • FlowLayout()
  • FlowLayout(int how)
  • FlowLayout(int how, int hspace, int vspace)

 

In the above constructors, how specifies the alignment, hspace specifies horizontal space, and vspace specifies vertical space.

 

Program

The java program is given below:

package javaprograms;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class RegForm extends JFrame implements ActionListener {
	JPanel p1;
	JLabel lblStudentRegdno;
	JTextField txtStudentRegdno;
	
	JPanel p2;
	JLabel lblStudentName;
	JTextField txtStudentName;
	
	JPanel p3;
	JLabel lblStudentMobile;
	JTextField txtStudentMobile;
	
	JPanel p4;
	JLabel lblCourse;
	JRadioButton rbBtech;
	JRadioButton rbMtech;
	JRadioButton rbOtherCourse;
	ButtonGroup bg1;
	
	JPanel p5;
	JLabel lblBranch;
	JRadioButton rbECE;
	JRadioButton rbEEE;
	JRadioButton rbCSE;
	JRadioButton rbIT;
	JRadioButton rbOtherBranches;
	ButtonGroup bg2;
	
	JPanel p6;
	JLabel lblSection;
	JComboBox jcb;
	String section[] = {"1","2","3","4","5","6","7","8"};
	
	JPanel p7;
	JButton btnSubmit;
	JButton btnClear;
	
	JPanel p8;
	JLabel lblStatus;
	
	RegForm(){
		p1 = new JPanel();
		lblStudentRegdno = new JLabel("Student Regd.No.:");
		txtStudentRegdno = new JTextField(30);
		p1.add(lblStudentRegdno);
		p1.add(txtStudentRegdno);
		
		p2 = new JPanel();
		lblStudentName = new JLabel("Student Name:");
		txtStudentName = new JTextField(30);
		p2.add(lblStudentName);
		p2.add(txtStudentName);
		
		p3 = new JPanel();
		lblStudentMobile = new JLabel("Student Mobile No.:");
		txtStudentMobile = new JTextField(30);
		p3.add(lblStudentMobile);
		p3.add(txtStudentMobile);
		
		p4 = new JPanel();
		lblCourse = new JLabel("Course:");
		rbBtech = new JRadioButton("B.Tech.");
		rbMtech = new JRadioButton("M.Tech.");
		rbOtherCourse = new JRadioButton("Others");
		bg1 = new ButtonGroup();
		bg1.add(rbBtech);
		bg1.add(rbMtech);
		bg1.add(rbOtherCourse);
		p4.add(lblCourse);
		p4.add(rbBtech);
		p4.add(rbMtech);
		p4.add(rbOtherCourse);
		
		p5 = new JPanel();
		lblBranch = new JLabel("Branch:");
		rbECE = new JRadioButton("ECE");
		rbEEE = new JRadioButton("EEE");
		rbCSE = new JRadioButton("CSE");
		rbIT = new JRadioButton("IT");
		rbOtherBranches = new JRadioButton("Others");
		bg2 = new ButtonGroup();
		bg2.add(rbECE);
		bg2.add(rbEEE);
		bg2.add(rbCSE);
		bg2.add(rbIT);
		bg2.add(rbOtherBranches);
		p5.add(lblBranch);
		p5.add(rbECE);
		p5.add(rbEEE);
		p5.add(rbCSE);
		p5.add(rbIT);
		p5.add(rbOtherBranches);
		
		p6 = new JPanel();
		lblSection = new JLabel("Section: ");
		jcb = new JComboBox(section);
		p6.add(lblSection);
		p6.add(jcb);
		
		p7 = new JPanel();
		btnSubmit = new JButton("Submit");
		btnClear = new JButton("Clear");
		p7.add(btnSubmit);
		p7.add(btnClear);
		btnSubmit.addActionListener(this);
		btnClear.addActionListener(this);
		
		p8 = new JPanel();
		lblStatus = new JLabel();
		p8.add(lblStatus);
		
		add(p1);
		add(p2);
		add(p3);
		add(p4);
		add(p5);
		add(p6);
		add(p7);
		add(p8);
		setSize(500, 600);
		setTitle("Registration Form");
		setLayout(new GridLayout(10,1));
		setVisible(true);
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	

	public static void main(String[] args) {
		RegForm f = new RegForm();
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand() == "Submit") {
			String status = "<html><body>Regd.No. = " + txtStudentRegdno.getText() + 
					" Name = " + txtStudentName.getText() +
					" Mobile No.= " + txtStudentMobile.getText();
			status += " <br>Course = ";
			if(rbBtech.isSelected())
				status += rbBtech.getText();
			else if(rbMtech.isSelected())
				status += rbMtech.getText();
			else
				status += rbOtherCourse.getText();
			status += " Branch = ";
			if(rbECE.isSelected())
				status += rbECE.getText();
			else if(rbEEE.isSelected())
				status += rbEEE.getText();
			else if(rbCSE.isSelected())
				status += rbCSE.getText();
			else if(rbIT.isSelected())
				status += rbIT.getText();
			else
				status += rbOtherBranches.getText();
			status += " Section = " + jcb.getSelectedItem() + "</html></body>";
			lblStatus.setText(status);	
		}
		else {
			lblStatus.setText("");
		}
	}
}

 

Input and Output

Student Details Java Swing

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
Twitter
Pinterest
Youtube
Instagram
Blogarama - Blog Directory