AJP Solved Practical Manual

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 24

Practical 4

Aim:

Write a program to create two-level card desk that allow the user to
select component of panel using CardLayout

Theory:

The CardLayout class manages the components in such a manner that


only one component is visible at a time. It treats each component as a
card hence known as CardLayout.

Constructors of CardLayout class :

1. CardLayout(): creates a card layout with zero horizontal and vertical gap.

2. CardLayout(int hgap, int vgap): creates a card layout with the given
horizontal and vertical gap.

Commonly used methods of CardLayout class:


1. public void next (Container parent): is used to flip to the next card of the
given container
2. public void previous (Container parent): is used to flip to the previous
card of the given container.

3. public void first (Container parent): is used to flip to the first card of
the given container.
4. public void last (Container parent): is used to flip to the last card of
the given container.

5. public void show (Container parent, String name): is used to flip to


the specified card with the given name
Program Code:

import java.awt.*;
import
java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.*;

public class CardLayoutDemo extends JFrame implements ActionListener

{ CardLayout card;
JButton b1, b2,
b3; Container c;

CardLayoutDemo()
{
c = getContentPane();
card = new CardLayout(40,
30); c.setLayout(card);

b1 = new JButton("Welcome in First Level");


b2 = new JButton("Welcome in Second Level");

b1.addActionListener(t
his);
b2.addActionListener(t
his);

c.add("a", b1);
c.add("b", b2);
}

public void actionPerformed(ActionEvent e)


{
card.next(c);
}

public static void main(String[] args)


{
CardLayoutDemo cl = new CardLayoutDemo();

cl.setSize(400,
400);
cl.setVisible(true)
;

}
}
Output:

Marks Obtained Dated Signature


of Teacher
Process Product Total (50)
Related(15) Related(35)
Practical 5

Aim:

Write a program using AWT to create a menubar where menubar


contains menu items such as File, Edit, View and create a submenu
under the File Menu: New and Open.

Theory:

 Menu Bar

In AWT, a MenuBar is used to hold one or more Menu objects, and it is


typically added to a Frame or Dialog.

Creating a Menu Bar:

 Instantiate a MenuBar.
 Create Menu objects (like "File", "Edit", etc.).
 Add Menu objects to the MenuBar.
 Set the MenuBar to your Frame or Dialog.

 Menu

A Menu is a drop-down list that can contain multiple MenuItem objects. It


is added to a MenuBar and represents a category of commands or
options.

Creating a Menu:

 Instantiate a Menu with a name.


 Add MenuItem objects to the Menu.

 Menu Item

A MenuItem represents a single option within a Menu. It can be a simple


menu item or a check box item.

Creating a Menu Item:

 Instantiate a MenuItem.
 Optionally, set properties like labels or shortcuts.
 Add an ActionListener to handle actions.
Program Code:

import java.awt.*;
public class
MenuDialog
{
public static void main(String[] args)
{
Frame f=new Frame("Welcome in Practical 5");
MenuBar mBar = new MenuBar();

Menu File = new Menu("File");


Menu Edit = new Menu("Edit");
Menu View = new Menu("View");
Menu New = new Menu("New");
Menu Zoom = new Menu("Zoom");
MenuItem i2 = new MenuItem("OpenCtrl+O");
MenuItem i3 = new MenuItem("SaveCtrl+S");
MenuItem i4 = new MenuItem("Exit");
MenuItem i5 = new MenuItem("UndoCtrl+Z");
MenuItem i6 = new MenuItem("CutCtrl+X");
MenuItem i7 = new MenuItem("CopyCtrl+C");
MenuItem i8 = new MenuItem("PasteCtrl+V");
MenuItem i9 = new MenuItem("New TabCtrl+N");
MenuItem i10 = new MenuItem("New
WindowCtrl+Shift+N"); MenuItem i12 = new
MenuItem("Status Bar");
MenuItem i13 = new MenuItem("Word wrap");
MenuItem i14 = new MenuItem("Zoom In");
MenuItem i15 = new MenuItem("Zoom Out");

File.add(New)
New.add(i9);
New.add(i10);
File.add(i2);
File.add(i3);
File.add(i4);
mBar.add(File);

Edit.add(i5);
Edit.add(i6);
Edit.add(i7);
Edit.add(i8);

View.add(Zoom);
Zoom.add(i14);
Zoom.add(i15);
View.add(i12);
View.add(i13);
mBar.add(Edit);

mBar.add(View);
f.setMenuBar(mBar);
f.setVisible(true);
f.setSize(400,400);
f.setLayout(null);
}

Output:

Marks Obtained Dated Signature


of Teacher
Process Product Total (50)
Related(15) Related(35)
Practical 6

Aim:

Write a program using swing to display a scrollpane and JCombobox in an


Japplet with the items: English, Marathi, Hindi, Sanskrit.

Theory:

 JComboBox

 JComboBox is a component that provides a drop-down list of


items for the user to choose from. It’s useful for selecting from a
limited set of options.

Key Points:

 Model: JComboBox uses a model to manage its list of items. The


default model is DefaultComboBoxModel, but you can use custom
models if needed.

 Editable vs. Non-Editable: By default, a JComboBox is non-


editable, meaning users can only select from the list. You can
make it editable by calling setEditable(true), allowing users to
type their own entries.

 JScrollPane:

JScrollPane is a key component in Java Swing used to provide scrolling


capabilities for other components when their content exceeds the visible
area.

Primary Function:

JScrollPane allows for the inclusion of a scrollable view for another


component, making it useful when the content is too large to fit within a
fixed-size area.
Program Code:

import java.awt.*;
import javax.swing.*;
public class Practical6 extends JFrame
{
public Practical6()
{

Container ct= getContentPane();

ct.setLayout(null);

JLabel jl = new JLabel("Select Branch :");

JComboBox<String> jc = new JComboBox<>();

jc.addItem("Computer Engineering");
jc.addItem("Mechanical Engineering");
jc.addItem("Civil Engineering");
jc.addItem("Electronics & Tele. Engineering");
jc.addItem("Electrical Engineering");
jc.addItem("Chemical Engineering");

ct.add(jl);
ct.add(jc);

jl.setBounds(60,50,100,30);
jc.setBounds(170,50,200,30);
}

public static void main(String ar[])


{
Practical6 fr = new Practical6();

fr.setTitle("Practical 6 SSB");

fr.setSize(300,400);

fr.setVisible(true);

}
}
Output:

Marks Obtained Dated Signature


of Teacher

Process Product Total (50)


Related(15) Related(35)
Practical 7

Aim:

Write a Program to create a JTree.

Theory:

 JTree:

JTree is a Swing component in Java used to display a hierarchical tree of


data. It is a powerful way to represent and interact with hierarchical
information, such as file systems, organizational structures, or nested data.
Here's a comprehensive overview of JTree, including its components,
configuration, and best practices.

 Purpose

 Hierarchical Data Representation: JTree is designed to display and


manage hierarchical data structures where each item (node) can
have zero or more child nodes, creating a tree-like structure.

 Interactivity: It provides a way for users to navigate and interact


with hierarchical data through expanding and collapsing nodes,
selecting items, and customizing the tree’s appearance.

 Key Components

1. Tree Model:
o TreeModel: The data model used by JTree to manage and
provide data for the tree. The TreeModel interface defines
methods for interacting with the tree’s data structure.
o DefaultTreeModel: A commonly used implementation of
TreeModel that works with DefaultMutableTreeNode to
create and manage the tree structure.

2. Tree Nodes:
o TreeNode: The interface representing each node in the tree. It
includes methods for managing child nodes and querying the
node's information.
o DefaultMutableTreeNode: A concrete implementation of
TreeNode that allows nodes to be mutable and supports
adding, removing, and accessing children.
Program Code :

import javax.swing.*;
import
javax.swing.tree.DefaultMutableTreeNode;
public class TreeExample
{
JFrame f;
TreeExample
()
{
f=new JFrame();
DefaultMutableTreeNode Year=new
DefaultMutableTreeNode("Year"); DefaultMutableTreeNode
Months=new DefaultMutableTreeNode("Months");
DefaultMutableTreeNode Days=new
DefaultMutableTreeNode("Days"); Year.add(Months);
Year.add(Days);
DefaultMutableTreeNode January=new DefaultMutableTreeNode("January");
DefaultMutableTreeNode February=new
DefaultMutableTreeNode("February"); DefaultMutableTreeNode March=new
DefaultMutableTreeNode("March"); DefaultMutableTreeNode April=new
DefaultMutableTreeNode("April"); DefaultMutableTreeNode May=new
DefaultMutableTreeNode("May"); DefaultMutableTreeNode June=new
DefaultMutableTreeNode("June"); DefaultMutableTreeNode July=new
DefaultMutableTreeNode("July"); DefaultMutableTreeNode August=new
DefaultMutableTreeNode("August"); DefaultMutableTreeNode Sapember=new
DefaultMutableTreeNode("Sapember"); DefaultMutableTreeNode
Octomber=new DefaultMutableTreeNode("Octomber");
DefaultMutableTreeNode November=new
DefaultMutableTreeNode("November"); DefaultMutableTreeNode
December=new DefaultMutableTreeNode("December"); Months.add(January);
Months.add(February
);
Months.add(March);
Months.add(April);
Months.add(May);
Months.add(June);
Months.add(July);
Months.add(August);
Months.add(Sapemb
er);
Months.add(Octomb
er);
Months.add(Novemb
er);
Months.add(Decemb
er);

JTree jt=new
JTree(Year); f.add(jt);
f.setSize(300,300);
f.setVisible(true);
f.setTitle("Practical no
7");
}

public static void main(String[] args)


{
new TreeExample();
}
}
Output :

Marks Obtained Dated Signature


of Teacher

Process Product Total (50)


Related(15) Related(35)
Practical 8

Aim:

Write a program to Demonstrate status of key on Applet window such as


KeyPressed, Key Released, KeyUp, KeyDown.

Theory :

 KeyListener Interface :

The KeyListener interface is a part of the java.awt.event package and is


used to handle keyboard events in AWT-based components. It has three
primary methods:

 keyPressed(KeyEvent e)
 keyReleased(KeyEvent e)
 keyTyped(KeyEvent e)

Each of these methods is invoked in response to a specific type of keyboard


event.

Method Details

1. keyPressed(KeyEvent e)

Purpose:

o Called when a key is pressed down.

Parameters:

o KeyEvent e: Contains information about the key event,


including the key code, key character, and other event
details.

Usage:

o This method is typically used to handle actions that should


occur immediately when a key is pressed. For example,
detecting when a user presses a shortcut key or handling
input that should be processed as soon as the key is pressed.
2. keyReleased(KeyEvent e)

Purpose:

 Called when a key is released after being pressed.

Parameters:

 KeyEvent e: Contains information about the key release event,


including the key code and other event details.

Usage:

 This method is used to handle actions that should occur after a


key is released. It’s useful for actions that should be finalized or
updated only when the user completes a key press.

3. keyTyped(KeyEvent

e) Purpose:

 Called when a key is typed, which means the key is pressed


and then released.

Parameters:

 KeyEvent e: Contains information about the key typed event,


including the character representation of the key.

Usage:

 This method is used for handling character input, such as


processing typed characters or implementing text field
validation. Unlike keyPressed and keyReleased, which handle
low-level key codes, keyTyped deals with the actual character
input.
Program Code :

import java.awt.*;
import
java.applet.*;
import
java.awt.event.*;
public class Practical extends Applet implements KeyListener
{
String msg = "";

public void init()


{
addKeyListener(this);
}

public void keyReleased(KeyEvent k)


{
showStatus("Key Up / Key Released");
repaint();
}

public void keyTyped(KeyEvent k)


{

showStatus("Key Down / Key


Typed"); repaint();
}

public void keyPressed(KeyEvent k)


{
showStatus("Key Pressed");

repaint();
}

public void paint(Graphics g)


{
g.drawString(msg, 10, 10);
}
}
/* <applet code="VSPractical8.class" height="400" width="400">
</applet>*/
Output :

Marks Obtained Dated Signature


of Teacher

Process Product Total (50)


Related(15) Related(35)
Practical 9

Aim:

Write a program to Demonstrate various mouse event using MouseListener


and MouseMotionlistener.

Theory :

 MouseListener

MouseListener is an interface used for handling mouse events. It is part


of the java.awt.event package and provides methods to respond to
various mouse actions, such as clicks, presses, releases, and entering or
exiting a component.

 MouseListener Interface

The MouseListener interface defines the following five methods:

 mouseClicked(MouseEvent e): Invoked when the user clicks the mouse


button on a component.

 mousePressed(MouseEvent e): Invoked when the user presses a mouse


button on a component.

 mouseReleased(MouseEvent e): Invoked when the user releases a mouse


button on a component.

 mouseEntered(MouseEvent e): Invoked when the mouse enters a component.

 mouseExited(MouseEvent e): Invoked when the mouse exits a component.

 MouseMotionListener

The MouseMotionListener interface in Java is used to handle mouse


motion events, such as when the mouse is moved or dragged over a
component.

It's part of the java.awt.event package and is often used in conjunction


with MouseListener to provide a more comprehensive mouse interaction
experience.
 MouseMotionListener Interface :

The MouseMotionListener interface defines two methods:

 mouseDragged(MouseEvent e): Called when the user drags the mouse


while holding down a button.
 mouseMoved(MouseEvent e): Called when the mouse is moved over
a component.

Program Code for MouseListener Methods() :

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class VSPractical9 extends Applet implements MouseListener
{
String s="See your event here";

public void init()


{
this.addMouseListener(this);
}
public void paint(Graphics g)
{
g.drawString(s,100,100);
}
public void mouseEntered(MouseEvent me)
{
s="Mouse Entered";
repaint();
}
public void mouseExited(MouseEvent me)
{
s="Mouse Exited"; repaint();
}
public void mouseClicked(MouseEvent me)
{
s="Mouse Clicked";
repaint();
}
public void mousePressed(MouseEvent me)
{
s="Mouse Pressed";
repaint();
}
public void mouseReleased(MouseEvent me)
{
s = "Mouse Released";
repaint();
}}

/*
<applet code="VSPractical9.class" width="500" heigth="300">
</applet>*/
Output :
Program Code for MouseMotionListener Methods() :

import java.awt.*;

import java.awt.event.*;

import java.applet.Applet;

/*<applet code="Practical9" width="300" height="300">

</applet>*/

public class Practical9 extends Applet

public void init()

{ this.addMouseMotionListener(new Innerl());

class Innerl extends MouseMotionAdapter

public void mouseMoved(MouseEvent me)

Graphics g=getGraphics();
g.setColor(Color.RED);
g.fillRect(me.getX(),me.getY(),20,20);
}
public void mouseDragged(MouseEvent me)

{
Graphics g=getGraphics();
g.setColor(Color.BLUE);
g.fillRect(me.getX(),me.getY(),20,20);
}

}
}
Output :

Marks Obtained Dated Signature


of Teacher
Process Product Total (50)
Related(15) Related(35)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy