Java GUI Applet and Swing Examples
Java GUI Applet and Swing Examples
Use suitable
Layout: -
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
Label l1,l2;
TextArea t;
TextField t1;
Button b;
b=new Button("Submit");
t=new TextArea();
t1=new TextField();
l1=new Label("Username");
l2=new Label("Password");
add(l1);
add(t);
add(l2);
add(t1);
add(b);
setLayout(new FlowLayout());
setSize(300,300);
}
2) Develop an application using List components to add names of 10 different cities.
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
List l;
JFrame f;
l=new List();
[Link]("Mumbai");
[Link]("Pune");
[Link]("Nashik");
[Link]("Thane");
[Link]("Kalyan");
[Link]("Karvar");
[Link]("Nagpur");
[Link]("Kolapur");
[Link]("Badlapur");
[Link]("Ulhasnagar");
add(l);
setLayout(new FlowLayout());
setSize(300,300);
}
3) Develop a swing program to show the use of ActionListener for JTextField
import [Link].*;
import [Link].*;
import [Link].*;
String p,p1;
Label l1,l2,l3;
JTextField t,t2;
JFrame f;
JButton b;
noob2()
t=new JTextField(5);
t2=new JTextField(5);
l3=new Label("");
b=new JButton("Submit");
[Link](l1);
[Link](t);
[Link](l2);
[Link](t2);
[Link](b);
[Link](l3);
[Link](this);
[Link](300,300);
[Link](new FlowLayout());
[Link](true);
p=[Link]();
p1=[Link]();
if(([Link]("user"))&&([Link]("no")))
else
new noob2();
4) Develop an application using to demonstrate the use of Radio Button and checkbox.
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
Label l1,l2;
CheckboxGroup cbg;
Checkbox c1,c2,c3,c4,c5;
public void init()
cbg=new CheckboxGroup();
c1=new Checkbox("Male",cbg,true);
c2=new Checkbox("Female",cbg,false);
c3=new Checkbox("Cricket");
c4=new Checkbox("Football");
c5=new Checkbox("Kabadi");
add(l1);
add(c1);
add(c2);
add(l2);
add(c3);
add(c4);
add(c5);
setLayout(new FlowLayout());
setSize(300,300);
import [Link].*;
import [Link].*;
import [Link].*;
JFrame f;
noob2()
{
for(int i=1;i<=25;i++)
[Link](new Button("Button"+i));
[Link](g);
[Link](true);
[Link](300,300);
new noob2();
import [Link].*;
import [Link].*;
import [Link].*;
JFrame f;
GridBagLayout gb;
Button b1,b2,b3,b4,b5;
noob2()
gb=new GridBagLayout();
[Link]=[Link];
[Link]=0;
[Link]=0;
[Link](b1,gbc);
[Link](b1);
[Link]=[Link];
[Link]=0;
[Link]=1;
[Link](b2,gbc);
[Link](b2);
[Link]=[Link];
[Link]=1;
[Link]=0;
[Link](b3,gbc);
[Link](b3);
[Link]=[Link];
[Link]=1;
[Link]=1;
[Link](b4,gbc);
[Link](b4);
[Link]=[Link];
[Link]=2;
[Link]=0;
[Link]=2;
[Link](b5,gbc);
[Link](b5);
[Link](gb);
[Link](true);
[Link](300,300);
new noob2();
import [Link].*;
import [Link].*;
import [Link].*;
CardLayout card;
Container c;
JButton b1,b2,b3,b4,b5;
noob2()
card=new CardLayout(40,30);
c=getContentPane();
[Link](card);
b1=new JButton("First");
b2=new JButton("Second");
b3=new JButton("Third");
b4=new JButton("Fourth");
b5=new JButton("Fifth");
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link]("a",b1);
[Link]("b",b2);
[Link]("c",b3);
[Link]("d",b4);
[Link]("e",b5);
[Link](c);
}
public static void main(String args[])
[Link](300,300);
[Link](true);
8) Develop a program to create checkable menu item ‘Picture’ under ‘Insert’ menu and ‘Paste’ menu
item under menu ‘Home’
import [Link].*;
import [Link].*;
class noob3
[Link](mi);
[Link](cmi);
[Link](m);
[Link](m1);
[Link](mbr);
[Link](true);
[Link](300,300);
}
}
9) Develop a program to create three menus such as PageLayout, References and Mailing. Disable the
Mailing menu.
import [Link].*;
import [Link].*;
class noob3
[Link](false);
[Link](m);
[Link](m1);
[Link](m2);
[Link](mbr);
[Link](true);
[Link](300,300);
import [Link].*;
import [Link].*;
import [Link].*;
{
JComboBox jb;
JLabel l;
noob3()
String state[]={"Mumbai","Pune","Goa","Thane"};
l=new JLabel("");
jb=new JComboBox(state);
[Link](jb);
[Link](l);
[Link](this);
[Link](jb);
[Link](l);
[Link](new FlowLayout());
[Link](true);
[Link](300,300);
new noob3();
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
JTree t;
JLabel jt;
noob3()
[Link](i1);
[Link](i2);
[Link](i3);
[Link](i4);
t=new JTree(i);
[Link](new MouseAdapter()
TreePath tp=[Link]([Link](),[Link]());
if(tp!=null)
[Link]([Link]());
else
[Link]("");
}
});
[Link](new BorderLayout());
[Link](t,[Link]);
[Link](jt,[Link]);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](300,300);
TreePath tp=[Link]([Link](),[Link]());
if(tp!=null)
[Link]([Link]());
else
[Link]("");
new noob3();
12) Develop a program to create a table of ‘Name’, ‘Percentage’ and ‘Grade’ for 10 students using JTable
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
{
noob3()
String data[][]={{"Shyam","99%","A+"},{"Ram","98%","A+"},{"Liam","97%","A+"},{"Noah","96%","A+"},
{"Oliver","95%","A+"},{"James","94%","A+"},{"Emma","93%","A+"},{"OLivia","92%","A+"},
{"Isabella","91%","A+"},{"Bella","90%","A+"}};
String cols[]={"Name","Percentage","Grade"};
[Link](new JScrollPane(table));
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](300,300);
new noob3();
13) Develop a program using JProgressBar to show the progress of ProgressBar when user clicks on
JButton
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
int a=0;
JProgressBar jpb;
JButton b;
noob3()
int min=0,max=100;
jpb=new JProgressBar([Link],min,max);
[Link](true);
[Link](this);
[Link](b);
[Link](jpb);
[Link](new FlowLayout());
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](300,300);
a=a+5;
[Link](a);
new noob3();
14) Develop a program using JProgressBar to show the progress of ProgressBar Using Thread class
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
JProgressBar jpb;
JButton b;
noob3()
int min=0,max=100;
jpb=new JProgressBar([Link],min,max);
b=new JButton("Start");
[Link](true);
[Link](this);
[Link](b);
[Link](jpb);
[Link](new FlowLayout());
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](300,300);
for(int i=0;i<=100;i++)
[Link](i);
try
[Link](30);
catch(Exception c)
[Link](c);
new no().start();
new noob3();
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
JTree t;
JLabel jt;
noob3()
[Link](j1);
[Link](j2);
[Link](j3);
[Link](j4);
[Link](k1);
[Link](k2);
[Link](k3);
[Link](k4);
[Link](j);
[Link](k);
t=new JTree(i);
[Link](new MouseAdapter()
TreePath tp=[Link]([Link](),[Link]());
if(tp!=null)
[Link]([Link]());
else
[Link]("");
});
[Link](new BorderLayout());
[Link](t,[Link]);
[Link](jt,[Link]);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](300,300);
TreePath tp=[Link]([Link](),[Link]());
if(tp!=null)
[Link]([Link]());
else
[Link]("");
new noob3();
16) Develop program to generate KeyEvent when a key is pressed and display ‘KeyPressed’ message in
statusbar.
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
JLabel l;
TextArea t;
int a;
noob3()
t=new TextArea();
[Link](new KeyAdapter()
a=[Link]();
[Link]("Key is Pressed");
});
[Link](new BorderLayout());
[Link](t,[Link]);
[Link](l,[Link]);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](300,300);
new noob3();
17) Develop a program to accept two numbers and display product of two numbers when pressed
“Multiply” button. Use suitable layout and Swing components.
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
JLabel l,l2,l3;
TextField t,t2;
JButton b;
noob3()
t2=new TextField("",20);
b=new JButton("Multiply");
[Link](this);
[Link](new FlowLayout());
[Link](l);
[Link](t);
[Link](l2);
[Link](t2);
[Link](b);
[Link](l3);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](300,300);
int a=[Link]([Link]());
int b=[Link]([Link]());
int c=a*b;
[Link]("The answer of multiplication is: "+c);
new noob3();
18) Develop a program to change the background color of applet when user performs events using
mouse.
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
addMouseListener(this);
setBackground([Link]);
}
setBackground([Link]);
setBackground([Link]);
setBackground([Link]);
setBackground([Link]);
19) Develop a program to count number of clicks performed by user in a Frame window.
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
/*<applet code="cinder" height=300 width=300></applet>*/
Label l;
addMouseListener(this);
add(l);
}
public void mouseReleased(MouseEvent e)
20) Develop a program to demonstrate the use of mouseDragged() and mouseMoved() methods of
MouseMotion Listener.
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class noob3 implements MouseMotionListener
{
JFrame f;
noob3()
{
f=new JFrame("My Frame");
[Link](this);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](300,300);
}
public void mouseDragged(MouseEvent e)
{
Graphics g=[Link]();
[Link]([Link]);
[Link]([Link](),[Link](),20,20);
}
public void mouseMoved(MouseEvent e)
{
Graphics g=[Link]();
[Link]([Link]);
[Link]([Link](),[Link](),20,20);
}
public static void main(String args[])
{
new noob3();
}
}
21) Develop a program using JPasswordField to set the password character as ‘#’ instead of ‘*’ Verify the
password and authenticate the user.
import [Link].*;
import [Link].*;
import [Link].*;
public class noob3 implements ActionListener
{
JFrame f;
JPasswordField pw,pw1;
JLabel l,l1,l2;
JButton b;
noob3()
{
f=new JFrame("My Frame");
pw=new JPasswordField(20);
pw1=new JPasswordField(20);
l=new JLabel("Enter Password: ");
l1=new JLabel("Re-Enter Password: ");
b=new JButton("Submit");
l2=new JLabel("");
[Link]('#');
[Link]("Please Enter your password");
[Link]("Please Enter your password");
[Link](l);
[Link](pw);
[Link](l1);
[Link](pw1);
[Link](b);
[Link](l2);
[Link](new FlowLayout());
[Link](this);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](300,300);
}
public void actionPerformed(ActionEvent e)
{
String a=[Link]();
String b=[Link]();
if([Link](b))
[Link]("Valid");
else
[Link]("Invalid");
}
public static void main(String args[])
{
new noob3();
}
}
22) Develop a program to accept password from user and if the length is less than 6 characters, then
error message should be displayed “Password length must be > 6 characters”. Use GenericServlet.
package hello1;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet("/hello3")
public hello3() {
super();
[Link]("text/html");
PrintWriter pw=[Link]();
String pass=[Link]("t1");
if([Link]()>6)
[Link]("<b>Valid password</>");
else
[Link]("<b>Invalid password</>");
[Link]();
}
}
Html code: -
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
</form>
</body>
</html>
23) Develop a program to accept password from user on client and check if the length is less than 6
characters on server , then error message should be displayed on client ”Password length must be > 6
characters”
Server: -
import [Link].*;
import [Link].*;
[Link]();
Client: -
import [Link].*;
import [Link].*;
[Link]([Link]());
[Link]([Link]());
[Link]();
}
}
24) Develop a program to demonstrate the use of WindowAdapter class in AWT. Add suitable
components.
import [Link].*;
import [Link].*;
import [Link].*;
public class noob3 implements WindowListener
{
JFrame f;
JLabel l;
noob3()
{
l=new JLabel("");
f=new JFrame("My Frame");
[Link](l);
[Link](new FlowLayout());
[Link](this);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](300,300);
}
public void windowActivated(WindowEvent we)
{
[Link]("Windows Activated");
}
public void windowDeactivated(WindowEvent we)
{
[Link]("Windows De-Activated");
}
public void windowClosed(WindowEvent we)
{
[Link]("Window is closed");
}
public void windowClosing(WindowEvent we)
{
[Link]("Window is closing");
}
public void windowDeiconified(WindowEvent we)
{
[Link]("Windows is deiconified");
}
public void windowIconified(WindowEvent we)
{
[Link]("Windows Iconified");
}
public void windowOpened(WindowEvent we)
{
[Link]("Windows Opened");
}
public static void main(String args[])
{
new noob3();
}
}
25) Develop a program using InetAddress class to retrieve IP address of computer when hostname is
entered by user.
import [Link].*;
import [Link].*;
public class tarnished
{
public static void main(String args[])
{
try
{
String hs;
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter the name for your local host:");
hs=[Link]();
InetAddress ip=[Link](hs);
[Link]("HostName: "+[Link]());
[Link]("IP Address: "+[Link]());
}
catch(Exception e)
{
[Link](e);
}
}
}
26) Develop a program using URLClass to retrieve the host, protocol, port an file of URL
[Link]
import [Link].*;
import [Link].*;
public class tarnished
{
public static void main(String args[]) throws MalformedURLException
{
try
{
URL hp=new URL("[Link]
[Link]("Port: - "+[Link]());
[Link]("Protocol: - "+[Link]());
[Link]("Host: -"+[Link]());
[Link]("File: -"+[Link]());
[Link]("Ext: -"+[Link]());
}
catch(Exception e)
{
[Link](e);
}
}
}
27) Develop a program to check credentials of user (Client will send user id and password to the server
and server will authenticate the client)
import [Link].*;
import [Link].*;
try {
while (true)
Socket cs = [Link]();
[Link]("Client connected");
[Link]("Auth Success");
} else
[Link]("Invalid Credentials");
[Link]();
catch (IOException e)
[Link]();
–client–
import [Link].*;
import [Link].*;
{ try
{
[Link](uid);
[Link](pwd);
[Link](resp);
[Link]();
} catch (IOException e)
{ [Link]();
28) Develop a program using DatagramPacket and DatagramSocket to create chat application.
import [Link].*;
import [Link].*;
InputStreamReader([Link]));
try {
int receivePort =
[Link]([Link]());
[Link]([Link]());
[Link](targetIP);
DatagramSocket(receivePort);
quit.");
try {
while (true) {
String message = [Link]();
if ([Link]("exit")) {
[Link]("Exiting chat...");
break;
catch (Exception e)
[Link]();
} }});
try
30)Develop a program to display name and rollno of students from “studenttable” having
parecentage>70(Columns in Table Rollno, Name, Percentage) (Use MsAccess Database)
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
class no
try
Connection con=[Link]("Jdbc:Odbc:hello");
Statement st=[Link]();
while([Link]())
[Link]("/t"+[Link](1)+"/t"+[Link](2));
[Link]();
[Link]();
catch(SQLException e)
[Link](e);
31)Develop a program to retrieve data using Resultset. (Columns in Table Rollno, FirstName, LastName,
TotalMarks, Grade)) (Use MsAccess Database)
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
class no
try
Connection con=[Link]("Jdbc:Odbc:hello");
Statement st=[Link]();
while([Link]())
{
[Link]("/t"+[Link](1)+"/t"+[Link](2)+”/t”+[Link](3)+”/t”+[Link](4)+”/
t”+[Link](5));
[Link]();
[Link]();
catch(SQLException e)
[Link](e);
32)Develop a program to update a record in database table. (Columns in Table Empcode, FirstName,
LastName, Salary, Position)) (Use MySQL Database)
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
class no
try
{
[Link](“[Link]”);
Connection con=[Link](“jdbc:mysql://Localhost:3306/msbte”,”root”,”root”);
Statement st=[Link]();
while([Link]())
[Link]("/t"+[Link](1)+"/t"+[Link](2)+”/t”+[Link](3)+”/t”+[Link](4)+”/
t”+[Link](5));
[Link]();
[Link]();
catch(SQLException e)
[Link](e);
33) Develop a program to update name of student from ‘ABC’ to “PQR’ in database. Retrieve data in the
table using ResultSet and display it. (Use MySQL Database)
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
class no
try
[Link](“[Link]”);
Connection con=[Link](“jdbc:mysql://Localhost:3306/msbte”,”root”,”root”);
Statement st=[Link]();
while([Link]())
[Link]("/t"+[Link](1)+"/t"+[Link](2));
[Link]();
[Link]();
catch(SQLException e)
{
[Link](e);
34) Develop a program to delete all records for a product whose “price is greater than 500” and “Id is
import [Link].*;
import [Link].*;
import [Link].*;
class no
try
[Link](“[Link]”);
Connection con=[Link](“jdbc:mysql://Localhost:3306/msbte”,”root”,”root”);
Statement st=[Link]();
while([Link]())
{
[Link]("/t"+[Link](1)+"/t"+[Link](2)+”/t”+[Link](3));
[Link]();
[Link]();
catch(SQLException e)
[Link](e);
package hello1;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet("/hello3")
super();
[Link]("text/html");
PrintWriter pw=[Link]();
String user=[Link]("t1");
[Link]("<b>Hello <b>"+user);
[Link]();
Html code: -
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
</form>
</body>
</html>
36) Develop a servlet to receive the parameter through HTML forms and send back received parameter
to browser
package hello1;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet("/hello3")
public hello3() {
super();
[Link]("text/html");
PrintWriter pw=[Link]();
String pass=[Link]("t1");
[Link]();
Html code: -
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
</form>
</body>
</html>
37) Develop a servlet to receive student subject marks through HTML forms and send the response
whether student passed or Failed in Examination
package hello1;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet("/hello3")
public hello3() {
super();
[Link]("text/html");
PrintWriter pw=[Link]();
String name=[Link]("t1");
int a=[Link]([Link]("t2"));
int b=[Link]([Link]("t3"));
int c=[Link]([Link]("t4"));
int d=[Link]([Link]("t5"));
int e=a+b+c+d;
int f=e/4;
if(f<=100&&f>=80)
else if(f<=79&&f>=60)
else if(f<=59&&f>=40)
else
[Link]();
Html code: -
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<input type="submit">
</form>
</body>
</html>
38) Develop a generic servlet program to authenticate a client using username and password.
package hello1;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet("/hello3")
public hello3() {
super();
}
public void service(ServletRequest request, ServletResponse response) throws
ServletException, IOException {
[Link]("text/html");
PrintWriter pw=[Link]();
String name=[Link]("t1");
String pass=[Link]("t2");
String a="nobody",b="hello";
if([Link](a)&&[Link](b))
[Link]("Valid user");
else
[Link]("Invalid user");
[Link]();
Html code: -
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<form name="name1" action="[Link]
<input type="submit">
</form>
</body>
</html>
39) Develop a servlet program to display various details about session using HttpSession methods.
package hello4;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet("/hello5")
public hello5() {
super();
// TODO Auto-generated constructor stub
try
[Link]("text/html");
PrintWriter pw=[Link]();
HttpSession session=[Link]();
String a=[Link]();
[Link](15);
int d=[Link]();
[Link]("Session Information");
[Link]();
catch(Exception ae)
{
doGet(request, response);
package hello4;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet("/hello5")
super();
[Link]("text/html");
PrintWriter pw=[Link]();
Cookie cookies[]=[Link]();
if(cookies!=null)
String name=cookies[i].getName();
String value=cookies[i].getValue();
[Link]("cookiename:"+name+"<br>");
[Link]("cookievalue:"+value);
[Link]();
[Link]("text/html");
PrintWriter pw=[Link]();
String user=[Link]("t1");
[Link](cookie);
[Link]("Cookie Added");
[Link]();
doGet(request, response);
41) Develop a generic servlet program to find whether the number given by the client is prime or not.
package hello1;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet("/hello3")
super();
[Link]("text/html");
PrintWriter pw=[Link]();
int i=[Link]([Link]("t1"));
int k=0;
for(int j=1;j<=i;j++)
if(i%j==0)
k++;
if(k==2)
else
[Link]();
}
Html code: -
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<input type="submit">
</form>
</body>
</html>