PR1: write a program to demonstrate the use of awt componants like
label,textfild,textarea,button,checkbox,radiobutton
import [Link].*;
import [Link].*;
public class RadioDemo
{
public static void main( String args[] )
{
Frame f = new Frame();
[Link](true);
[Link](400,400);
[Link](new FlowLayout());
Label l1 = new Label("Select Subjects:");
Checkbox cb1 = new Checkbox("English");
Checkbox cb2 = new Checkbox("Sanskrit");
Checkbox cb3 = new Checkbox("Hindi");
Checkbox cb4 = new Checkbox("Marathi");
Label l2 = new Label("Select Gender:");
CheckboxGroup cg = new CheckboxGroup();
Checkbox c1 = new Checkbox("Male",cg,true);
Checkbox c2 = new Checkbox("Female",cg,true);
[Link](l1);
[Link](cb1);
[Link](cb2);
[Link](cb3);
[Link](cb4);
[Link](l2);
[Link](c1);
[Link](c2);
}
Output:
PRACTICAL 2: WRITE A PROGRAM TO DESIGN A FORM
USING THE COMPONENTS LIST AND CHOICE.
import [Link].*;
public class ChoiceDemo
{
public static void main(String args[])
{
Frame f = new Frame();
[Link](400,400);
[Link](true);
[Link](new FlowLayout());
Choice c = new Choice();
[Link]("Maths");
[Link]("Physics");
[Link]("Chemistry");
[Link](c);
}
}
PRACTICAL 3: WRITE A PROGRAM TO DESIGN SIMPLE
CALCULATOR WITH THE USE OF GRIDLAYOUT
import [Link].*;
public class GridDemo
{
public static void main( String args[] )
{
Frame f = new Frame();
[Link](true);
[Link](400,400);
[Link](new GridLayout(2,2));
Font font = new Font("TimesRoman",[Link],25);
[Link](font);
Label l[] = new Label[25];
for(int i = 0 ; i < 25 ; i++)
{
String s = "";
s = [Link](i+1);
Color c = new Color(i,i+10,i+20);
l[i] = new Label();
[Link](c);
l[i].setBackground(c);
l[i].setText(s);
}
for(int i = 0 ; i < 25;i++)
{
[Link](l[i]);
}
}
}
PR4: write a program to create a two level card desk that allows the user to select
componant of panel using cardlayout
import [Link].*;
import [Link].*;
import [Link].*;
public class CardLayoutExample1 extends JFrame implements ActionListener
{
CardLayout crd;
// button variables to hold the references of buttons
JButton btn1, btn2, btn3;
Container cPane;
// constructor of the class
CardLayoutExample1()
{
cPane = getContentPane();
//default constructor used
// therefore, components will
// cover the whole area
crd = new CardLayout();
[Link](crd);
// creating the buttons
btn1 = new JButton("Apple");
btn2 = new JButton("Boy");
btn3 = new JButton("Cat");
// adding listeners to it
[Link](this);
[Link](this);
[Link](this);
[Link]("a", btn1); // first card is the button btn1
[Link]("b", btn2); // first card is the button btn2
[Link]("c", btn3); // first card is the button btn3
}
public void actionPerformed(ActionEvent e)
{
// Upon clicking the button, the next card of the container is shown
// after the last card, again, the first card of the container is shown upon clicking
[Link](cPane);
}
// main method
public static void main(String argvs[])
{
// creating an object of the class CardLayoutExample1
CardLayoutExample1 crdl = new CardLayoutExample1();
// size is 300 * 300
[Link](300, 300);
[Link](true);
[Link](EXIT_ON_CLOSE);
}
}
PRACTICAL 5: WRITE A PROGRAM USING AWT TO CREATE
A MENU BAR WHERE MENUBAR CONTAINS MENU ITEMS
SUCH AS FILE, EDIT, VIEW AND CREATE A SUBMENU UNDER
THE FILE MENU: NEW AND OPEN
import [Link].*;
import [Link];
import [Link];
import [Link];
import [Link];
public class MemuDialog extends Frame implements ActionListener ,ItemListener
{
Dialog dialog;
Label l;
MemuDialog()
{
MenuBar mBar = new MenuBar();
setMenuBar(mBar);
Menu file = new Menu("File");
MenuItem new_file = new MenuItem("New");
MenuItem open_file = new MenuItem("Open");
MenuItem save_file = new MenuItem("Save");
new_file.addActionListener(this);
open_file.addActionListener(this);
save_file.addActionListener(this);
[Link](new_file);
[Link](open_file);
[Link](save_file);
[Link](file);
Menu edit = new Menu("Edit");
MenuItem undo_edit = new MenuItem("Undo");
CheckboxMenuItem cut_edit = new CheckboxMenuItem("Cut");
CheckboxMenuItem copy_edit = new CheckboxMenuItem("Copy");
CheckboxMenuItem edit_edit = new CheckboxMenuItem("Paste");
undo_edit.addActionListener(this);
cut_edit.addItemListener(this);
copy_edit.addItemListener(this);
edit_edit.addItemListener(this);
Menu sub = new Menu("Save Type");
MenuItem sub1_sum = new MenuItem("Direct Save");
MenuItem sub2_sum = new MenuItem("Save As");
[Link](sub1_sum);
[Link](sub2_sum);
[Link](sub);
[Link](undo_edit);
[Link](cut_edit);
[Link](copy_edit);
[Link](edit_edit);
[Link](edit);
dialog = new Dialog(this,false);
[Link](200,200);
[Link]("Dialog Box");
Button b = new Button("Close");
[Link](this);
[Link](b);
[Link](new FlowLayout());
l = new Label();
[Link](l);
}
public void actionPerformed(ActionEvent ie)
{
String selected_item = [Link]();
switch(selected_item)
{
case "New": [Link]("New");
break;
case "Open": [Link]("Open");
break;
case "Save": [Link]("Save");
break;
case "Undo": [Link]("Undo");
break;
case "Cut": [Link]("Cut");
break;
case "Copy": [Link]("Copy");
break;
case "Paste": [Link]("Paste");
break;
default: [Link]("Invalid Input");
}
[Link](true);
if(selected_item.equals("Close"))
{
[Link]();
}
}
public void itemStateChanged(ItemEvent ie)
{
[Link]();
}
public static void main(String[] args)
{
MemuDialog md = new MemuDialog();
[Link](true);
[Link](400,400);
}
}
PRACTICAL 6: WRITE A PROGRAM USING SWING TO
DISPLAY A SCROLLPANE AND JCOMBOBOX IN AN JAPPLET
WITH THE ITEMS - ENGLISH, MARATHI, HINDI, SANSKRIT.
import [Link].*;
import [Link].*;
import [Link];
import [Link];
public class JComboBoxDemo extends JApplet implements ItemListener
{
JLabel JLabelObj ;
public void init()
{
setLayout(new FlowLayout());
setSize(400, 400);
setVisible(true);
JComboBox JComboBoxObj = new JComboBox();
[Link]("Solapur");
[Link]("Pune");
[Link]("Banglore");
[Link]("Mumbai");
[Link](this);
JLabelObj = new JLabel();
add(JComboBoxObj);
add(JLabelObj);
}
public void itemStateChanged(ItemEvent ie)
{
String stateName = (String) [Link]();
[Link]("You are in "+stateName);
}
}
/* <applet code="JComboBoxDemo" height="400" width="400"> </applet> */
PRACTICAL 7: WRITE A PROGRAM TO CREATE A JTREE.
import [Link].*;
import [Link].*;
import [Link].*;
public class JTreeDemo
{
public static void main(String[] args) {
JFrame JFrameMain = new JFrame();
[Link](true);
[Link](400,400);
DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("India");
DefaultMutableTreeNode maharashtraNode = new DefaultMutableTreeNode("Maharashtra");
DefaultMutableTreeNode gujrathNode = new DefaultMutableTreeNode("Gujrath");
[Link](maharashtraNode);
[Link](gujrathNode);
DefaultMutableTreeNode mumbaiSubNode = new DefaultMutableTreeNode("Mumbai");
DefaultMutableTreeNode puneSubNode = new DefaultMutableTreeNode("Pune");
DefaultMutableTreeNode nashikSubNode = new DefaultMutableTreeNode("Nashik");
DefaultMutableTreeNode nagpurSubNode = new DefaultMutableTreeNode("Nagpur");
[Link](mumbaiSubNode);
[Link](puneSubNode);
[Link](nashikSubNode);
[Link](nagpurSubNode);
JTree tree = new JTree(rootNode);
[Link](tree);
}
}
PRACTICAL 8: WRITE A PROGRAM TO CREATE A JTABLE.
import [Link].*;
import [Link].*;
import [Link].*;
public class JTableDemo
{
public static void main(String[] args) {
JFrame JFrameMain = new JFrame();
[Link](true);
[Link](400,400);
String colHeads[] = {"ID","Name","Salary"};
Object data[][] = {
{101,"Amit",670000},
{102,"Jai",780000},
{101,"Sachin",700000}
};
JTable JTableObj = new JTable(data,colHeads);
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp = new JScrollPane(JTableObj,v,h);
[Link](jsp,[Link]);
//[Link](JTableObj);
}
}
PRACTICAL 9: WRITE A PROGRAM TO LAUNCH A
JPROGRESSBAR
import [Link].*;
import [Link].*;
public class JProgresBarDemo
{
JProgressBar JProgressBarObj;
int i=0,num=0;
JProgresBarDemo()
{
JFrame JFrameMain = new JFrame();
[Link](true);
[Link](400,400);
[Link](new FlowLayout());
JProgressBarObj = new JProgressBar(0,2000);
[Link](0);
[Link](true);
[Link](JProgressBarObj);
}
public static void main(String[] args)
{
JProgresBarDemo jpd = new JProgresBarDemo();
[Link]();
}
public void iterate()
{
while(i<=2000){
[Link](i);
i =i+20;
try
{
[Link](150);
}
catch(Exception e)
{
}
}
}
}
Write a Program using JProgressBar to show the progress of Progressbar when user
clicks on JButton.
Ans:
import [Link].*;
import [Link].*;
import [Link];
import [Link];
public class JProgressBarApplet extends JApplet implements ActionListener
{
JProgressBar JProgressBarObj;
JButton JButtonObj;
int i=0;
public void init()
{
setSize(400,400);
setVisible(true);
setLayout(new FlowLayout());
JButtonObj = new JButton("Click Me");
[Link](this);
JProgressBarObj = new JProgressBar();
[Link](true);
[Link](0);
add(JButtonObj);
add(JProgressBarObj);
}
public void actionPerformed(ActionEvent ie)
{
[Link]();
}
public void iterate()
{
while(i<=2000)
{
[Link](i);
i=i+20;
try
{
[Link](150);
}
catch(Exception e)
{}
}
}
}
/* <applet code="JProgressBarApplet" height="400" width="400">
</applet>
*/
PRACTICAL 10: WRITE A PROGRAM TO DEMONSTRATE
STATUS OF KEY ON APPLET WINDOW SUCH AS
KEYPRESSED, KEYRELEASED, KEYUP, KEYDOWN
import [Link].*;
import [Link].*;
import [Link].*;
public class KeyEventDemo extends Applet implements KeyListener
String msg = "";
public void init()
addKeyListener(this);
public void keyReleased(KeyEvent k)
showStatus("Key Released");
repaint();
public void keyTyped(KeyEvent k)
showStatus("Key Typed");
repaint();
public void keyPressed(KeyEvent k)
{
showStatus("Key Pressed");
repaint();
public void paint(Graphics g)
[Link](msg, 10, 10);
/*
<applet code="KeyEventDemo" height="400" width="400">
</applet>
*/
PRACTICAL 11: WRITE A PROGRAM TO DEMONSTRATE
VARIOUS MOUSE EVENTS USING MOUSELISTENER AND
MOUSEMOTIONLISTENER INTERFACE
import [Link].*;
import [Link].*;
import [Link].*;
public class MouseColor extends Applet implements MouseMotionListener
{
public void init()
{
addMouseMotionListener(this);
}
public void mouseDragged(MouseEvent me)
{
setBackground([Link]);
repaint();
}
public void mouseMoved(MouseEvent me)
{
setBackground([Link]);
repaint();
}
}
/*
<applet code="MouseColor" width=300 height=300>
</applet>
*/
PRACTICAL 12: WRITE A PROGRAM TO DEMONSTRATE THE
USE OF JTEXTFIELD AND JPASSWORDFIELD USING
LISTENER INTERFACE
A)
import [Link].*;
import [Link].*;
public class JPasswordChange
{
public static void main(String[] args) {
JFrame f = new JFrame();
[Link](true);
[Link](400,400);
[Link](new FlowLayout());
JPasswordField pf = new JPasswordField(20);
[Link]('#');
[Link](pf);
}
B)
import [Link].*;
import [Link].*;
public class PassDemo extends JPanel
// create an object of the JLabel class
JLabel lblName;
JLabel lblPasswd;
// create an object of the JPassword class
JTextField txtName;
JPasswordField txtPasswd;
public PassDemo()
lblName = new JLabel("Enter the User Name: ");
txtName = new JTextField(10);
lblPasswd = new JLabel("Enter the Password: ");
txtPasswd = new JPasswordField(10);
[Link]('*');
// Add tooltips to the text fields
[Link]("Enter User Name");
[Link]("Enter Password");
//Add labels to the Panel.
add(lblName);
add(txtName);
add(lblPasswd);
add(txtPasswd);
public static void main(String[] args)
// calls the PassDemo constructor.
PassDemo demo = new PassDemo();
// set the text on the frame
JFrame frm = new JFrame("Password Demo");
[Link](demo);
/* setSize() method is used to specify the width and height of the frame */
[Link](275,300);
// To display the Frame
[Link](true);
WindowListener listener = new WindowAdapter()
public void windowClosing(WindowEvent winEvt)
[Link](0);
}; // End of WindowAdaptor() method
// Window listener activates the windowClosing() method
[Link](listener);
} // End of main() method
} // End of class declaration
PRACTICAL 14: WRITE A PROGRAM TO DEMONSTRATE THE
USE OF LNETADDRESS CLASS AND ITS FACTORY METHODS.
import [Link];
import [Link];
import [Link];
public class RetriveIP
{
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter Host Name: ");
String host = [Link]();
try
{
InetAddress ip = [Link](host);
[Link]("IP Adress of Computer is:"+[Link]());
}
catch(UnknownHostException e)
{
[Link](e);
}
}
}
PRACTICAL 15: WRITE A PROGRAM TO DEMONSTRATE THE
USE OF URL AND URLCONNECTION CLASS AND ITS
METHODS
import [Link];
import [Link];
public class URLRetrive
public static void main(String[] args) throws MalformedURLException {
URL url = new URL("[Link]
[Link]("Authority: "+ [Link]());
[Link]("Default Port: "+ [Link]());
[Link]("File: "+ [Link]());
[Link]("Path: "+ [Link]());
[Link]("Protocol: "+ [Link]());
[Link]("Reference: "+ [Link]());
}
PRACTICAL 18: WRITE A PROGRAM TO INSERT AND
RETRIEVE THE DATA FROM DATABASE USING JDBC
Program to insert data:
// Program to Insert Data into Database
import [Link];
import [Link];
import [Link];
import [Link];
public class InsertStaticOracle
{
public static void main(String args[])
{
Statement st = null;
Connection connection = null;
try{
[Link] driverObj = new [Link]();
String url = "jdbc:oracle:thin:@localhost:1521:XE", username = "System" ,password = "pass";
connection =[Link](url,username,password);
if(connection!=null)
[Link]("Connection established successfully");
st = [Link]();
String qry = "Insert into Student values(104 ,'Atharva Agrawal','Dhule')";
int count = [Link](qry);
[Link](count+" Statement Created Successfully ");
}
catch(Exception e)
{
[Link]();
}
finally{
try
{
if(st != null)
[Link]();
if(connection != null)
[Link]();
}
catch(SQLException e){
[Link]();
}
}
}
}
Program to retrieve data:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class SelectOracle
{
public static void main(String args[]) throws SQLException
{
[Link]("Step 1: ");
[Link] obj = new [Link]();
// [Link]([Link]);
[Link]("Driver loaded successfully");
[Link]("Step 2: ");
String url="jdbc:oracle:thin:@localhost:1521:XE",uname="SYSTEM" , password="Atharva007";
Connection connection = [Link](url,uname,password);
if(connection!=null)
[Link]("Connection Established Successfully");
else
[Link]("Connection Not Established Successfully");
[Link]("Step 3: ");
Statement st = [Link]();
[Link]("Statement Referenced ");
[Link]("Step 4: ");
[Link]("Step 5: ");
String qry = "select * from Student";
ResultSet rs = [Link](qry);
[Link]("rs: "+rs);
[Link]("Step 6: ");
[Link]("Id\tName\taddress");
while([Link]())
{
int x = [Link](1);
String y = [Link]("Name");
String s = [Link](3);
[Link](x+"\t"+y+"\t"+s);
}
[Link]("Step 7: ");
[Link]();
[Link]();
[Link]();
}
}
Database:
PRACTICAL 22: WRITE A SERVLET PROGRAM TO SEND
USERNAME AND PASSWORD USING HTML FORMS AND
AUTHENTICATE THE USER
[Link]:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MySrv extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
[Link]("<HTML>");
[Link](" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
[Link](" <BODY>");
//Getting HTML parameters from Servlet
String username=[Link]("uname");
String password=[Link]("pwd");
if(([Link]("atharva")) && ([Link]("agrawal")))
{
[Link](" <h1> Welcome to "+username);
}
else
{
[Link](" <h1> Login fails ");
[Link](" <a href='./[Link]'> Click for Home page </a>");
}
[Link](" </BODY>");
[Link]("</HTML>");
[Link]();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost( request,response);
}
[Link]:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<BODY bgcolor='#e600e6'>
<form action='./MySrv' method="post">
<center> <h1> <u> Login Page </u></h1>
<h2> Username : <input type="text" name="uname"/>
Password : <input type="password" name="pwd"/>
<input type="submit" value="click me"/>
</center>
</form>
</body>
</HTML>
[Link]:
<web-app>
<servlet>
<servlet-name>MySrv</servlet-name>
<servlet-class>MySrv</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MySrv</servlet-name>
<url-pattern>/MySrv</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>[Link]</welcome-file>
</welcome-file-list>
</web-app>