Download small Media library Project in java

Media library Project

User Interface Componect Classes
A awt package contains a number of component classes that are typical elements of any
interactive user interface.it is also called UI componect classes.

Methods
setSize(Dimension d) Resizes the corresponding component so that it has width d.width
and height d.height
setSize(int width,int height) Resizes the corresponding component so that it has widht and height
setFont(font f) sets the font of allcorresponding component
setenabled(boolean b) Sets the font of the corresponding component
setEnabled(boolean b) Enables or disables the corresponding component depending on the value of the parameter b
setVisible(boolean b) Shows or hides the corresponding component depending on the values of parameter b
setForeground(Color c) Sets the foreground color of the corresponding component
setBounds(int x,int y, int width,int height) Moves and resizes the corresponding component
setBounds(int Rectangle) Moves and resizes the corresponding component to conform to the new bounding rectangle r
setBackground(Color c) Sets the background color of the corresponding component
getBackground() Gets the background color of the corresponding component
getBounds() Gets teh bounds of the corresponding component in the form of Recatangle Object
getFont() Gets the font of the corresponding component
getForeground() Gets the foreground color of the corresponding component
getSize() Returns the size of the corresponding component in the form of Dimension object.

To use the UI component following steps should be followed
1) First the user interface component has to be created using the constructor of the corresponding class
eg Button mybutton=new button(“Ok”);

2) Teh component created is added to container using add() method of the container class For eg
add(mybotton)

3)Depending on the user interface component it is necessary to handle the events grenerated by it
there are 2 ways of handaling events the beast one is listner

To determine the component in which an even has occured it is necessary to use inner classes.
each object has an inner class defined for it implements their respective event listners.

UI component
Label
Button
Checkbox
TextComponent
TextArea & TextField
Choice
List
Scrollbars

1)Buttons
Constructors
Button() Construsts a button with no label
Button(String label) Construct a button with the label

Methods
addActionListner(ActionListener l) Adds the specified actoion listner to receive action events
from the corresponding button
getActionCommand() Returns teh command name from the action event fired by the corresponding button
getLabel() Retruns teh label of the corresponding button
paramString() Returns the parameter string representing the state of the corresponding button
setLabel(String label) sets the label of the button to the value specified

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class buttontest extends Applet
{
Label lb1=new Label(“Button1”);
Button b1=new Button(“OK”);
Label lb2=new Label(“Button2”);
Button b2=new Button(“CANCEL”);

public void init()
{
setLayout(new FlowLayout());
b1.addActionListener(new b1());
b2.addActionListener(new b2());
add(lb1);
add(b1);
add(lb2);
add(b2);
}
class b1 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
getAppletContext().showStatus(“button1 click”);
}
}

class b2 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
getAppletContext().showStatus(“button2 click”);
}
}

}

2)ChecBox
Constructor
CheckBox() Creates a checkbox with no label
CheckBox(String label) Creates a checkbox with the specified label
Checkbox(String label,boolean state) Creates a checkbox with the specified label and set the specifies state
Checkbox(String label,boolean state,CheckboxGroup group) Creates a checkbox with the specified label and sets the
Specified label and sets the specified state and places it in the specified group

Method
getCheckboxGroup() Determines the group of the corresponding checkbox
getLabel() Gets the name of the corresponding checkbox
getSelectedObjects() Returns an aray(lengths 1) containing the checkbox label or null if the check box is not selected
getState() Determines if the checkbox is in the on or off state
setCheckboxGroup(CheckboxGroup g) sets the corresponding checkbox’s group to the specified one
setLabel(String label) Sets the label of the corresponding checkbox to the value specified
setState(boolean state) sets the state of the corresponding checkbox to the value specified

3)Choice
Constructior
Choice() Creates a new choice menu

Methods
add(String item) Adds an item to the corresponding choice menu
addItem(String item) Adds an item to the corresponding choice
getItem(int index) Gets the string at the specified index of the corresponding choice menu
getItemCount() Returns the number of items in the corresponding choice menu
getSelectedIndex() Returns the index of the currently select item
getSelectedItem() Gets a representation of the current selected item
getSelectedObjects() Returns an array(length 1) containing the currently selected item
insert(String item, int index) Inserts the item into the corresponding chice at the specified position)
remove(int position) Removes an item from the corresponding choice menu at the specified position
remove(Stirng item) remove the first occurence of item from the corresponding choice menu
removeAll() removes the first occurence of item from the corresponding choice menu
select(int pos) sets the selectd item in the corresponding choice menu to be the item at the specified position
select(String str) Sets the selected item in the corresponding choice menu to be the item whose name is equal to the specified string

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class radiotest extends Applet
{
public void init()
{
CheckboxGroup c=new CheckboxGroup();
Checkbox c1=new Checkbox(“black and white”,c,true);
Checkbox c2=new Checkbox(“Color”,c,false);
c1.addMouseListener(new check1());
c2.addMouseListener(new check2());
add(c1);
add(c2);
Choice abc=new Choice();
abc.add(“Onida”);
abc.add(“BPL”);
abc.add(“Samsung”);
abc.add(“Philips”);
abc.add(“Videcon”);
abc.addItemListener(new ch());
add(abc);
}
class check1 extends MouseAdapter
{
public void mouseClicked(MouseEvent e)
{
showStatus(“you have selectd:black and white”);
}
}
class check2 extends MouseAdapter
{
public void mouseClicked(MouseEvent e)
{
showStatus(“you have selected color tv”);
}
}

class ch implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
String s=(String)e.getItem();
showStatus(“you have selecte “+s+” brand”);
}
}
}

4)Label
Construstor
Label() Construct an empty label
Label(String text) Construct a string with the corresponding text
Label(String text,int alignment) Construct a string with the text with the specified aligiment CENTER,LEFT,RIGHT

Method
getText() gets the text of the corresponding label
paramString() returns the parameter string representing the state of the corresponding label
setText(String text) Sets the text for the corresponding label to the specified text

5)List
List() creates a new scrolling list of items
List(int rows) creates a new scrolling list of items with the specified number of visible lines
List(int rows,boolean multiplemode) Creates a new scrolling list of items to display the specified number of rows

Method
add(string item) Adds the specified item at the end of the scrolling list
add(String item,index) adds the specified item at the position specified
deselect(int index) deselects the item at the specified index
getItem(int index) gets the item at the specified index
getItemCount() Gets the number of items in the list
getItems() gets the item in the list
getMinimumSize() Determines the minimum size of the scrolling list
getMinimumSize(int rows) gets teh minimun dimensions for a list with specified number of rows
getPreferredSize() Gets the preferred size of the corresponding scrolling list
getPreferredSize(int rows) Gets the minimum dimension for a list with the specified number of rows
getRows() gets the number of visible lines in hte corresponding list
getSelectedIndex() gets the index of the selected item in hte list
getSelectItem() gets the selected item in the corresponding list
select(int index ) selects the item at the specified index in the corresponding list
setMultipleMode(boolean b) Sets the flag that allows multiple selection in the corresponding list

6)Scrollbar
constructor
Scrollbar() constructs a vertical scroll bar
Scrollbar(int orientation) Construct a new scrollbar with the specified orientation
Scrollbar(int orientation,int maxvalue,int visible,int minimum,int maximum)
constructs a new scroll bar with the specified orientation initial value page size,minimum and maximum values

Method
getBlockIncrement() gets teh block increment of the corresponding scroll bar
getMaximum() gets the maximum value of the corresponding scroll bar
getMinimum() gets the minimum value of the corresponding scroll bar
getOrientation() determines the orientation of the corresponding scroll bar
getValue() gets teh current value of the corresponding scroll bar
setBlockIncrement(int v) sets the block increment for the corresponding scroll bar
setMaximum(int newMaximum) sets the maximum value for the corresponding scroll bar
setMinimum(int newminimum) sets the minimum value for the corresponding scroll bar
setVale(int newvalue) sets the value of the corresponding scroll bar to the specified value

7)TextField
Constructor
TextField() constructs a new text field
TextField(int columns) creates a new text field with the specified number of columns
TextField(String text) Constructs a new text field initialized with the specified text
TextField (String text,int columns) Constructs a new text field initialized with the specified text with the specified number of columns

Methods
getColumns() gets the number of columns in the corresponding text field
getEchoChat() get the character that is to be used for echoing
setColumns(int columns) sets the number of columns in the text field
setEchoChar(char c) sets the echo character for the text field
setText(String t) sets the test that is presented by hte corresponding text component to be the specified text

8)TextArea
constructors
TextArea() Constructs a new text area
TextArea(int rows, int columns) Constructs a text area with the specified number of rows and columns
TextAra(String text) Constructs a new new text area with the specified text
TextArea(String text int rows,int columns) constructs a new text area with the specified text, and specified number of row and columns
TextArea(String text int rows int cols, int scrollbar) Constructs a new text area with the specified text and specified text and specified number of rows and columns and visibility of the scrollbar

Methods
append(string str) appends the given text to the xtext area’s contents
getColumns() gets the number of the text area
getMinimumSize() determines the minimum area of the text area
getRows() gets the number of rows in the text area
insert(String str, int pos) inserts the specified string at the specified position
setColumns(int columns) set the number of columns for the text area
setRows(int rows) sets the number of rows for the text area

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class listtext extends Applet
{
List acts=new List();
TextField tx=new TextField(10);
Button add=new Button(“add”);
String stringlist[]={“one”,”two”,”three”};
public void start()
{
add(new Label(“text”));
add(tx);
for(int i=0;i<stringlist.length;i++)
acts.addItem(stringlist[i]);
add(acts);
add.addActionListener(new Add());
add(add);
}
class Add implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
acts.addItem(tx.getText());
}
}
}

===============================================================================================
Panel
The panel object can contain other ui components is unvisible window in applet
Panel p1=new Panel();
setLayout(p1);
p1.add()

================================================================================================
Container
the Component(UI) is drawn inside the Container. A container represents the positioning the ui

1)Canvas
A canvas component represents a blank rectangular area of the scren onto which the application
can draw or from whickh the application can trap input events from the user
canvas are good for paintaing a graphics.

Constructor
Canva() Constructs a new canvas
Methods
addNotify() Creates a peer of the canvas
paint(Graphics g) this method is called to repaint the corresponding canvas

2) Window
Window is displayed in desktop to to draw the sub window you have to draw Frames or dialog

1) Frames
it is same like window

Constructor
Frame()
Frames(String title)

Methods
dispose() To close frame window
getTitle(0 to get the title of the frame window
isResizeable() it takes a boolean value
resize(horizontal size,vertical size) it sets a new dimension to the frame according to the pixcel
setTitle(String str) to set the title of frame window
show() to show the frame
setVisible(boolean) makes the frame or window visible
setSize(Dimension d) set the size
setLocation(int,int) set the location of window
getLocation() returs the point objects of window

import java.awt.*;
import java.awt.events.*;
class frameapp extens Frames
{
boolean a;
frameapp()
{
a=false;
setTitle(“stand-alone application”);
addWindowListener(new w());
}
class w extends wWindowAdapter
{
public void windowClosing(WindowEvent e)
{
if(a)
dispose();
else
System.exit(0);
}
}

public static void main(String args[])
{
frameapp fn=new frameapp();
fn.setSize(300,300);
fn.setVisible(true);
}
}

2) FileDialog
Constructor
FileDialog(Frame parent) creates file dialog for loading a file
FileDialog(Frame parent),string title) creates a file fialog window with the specified title for loading a file
FileDialog(Frame parent, String title,int mode) Creates a file dialog window with the specified title for loading or saving file.

Methods
getDirectory() gets the directory of the corresponding file dialog
getFile() gets the file of the corresponding file dialog
getFilenameFilter() Determines the file dialog’s filename filter
getMode() Indicates whether the file dialog is in the save mode or open mode
setDirectory(string str) sets the directory of the file dialog window to the one specified
setFile(String file) set the selected file for the corresponding fiel dialog window to the one specified
setFilenameFilter(filenamefilter filter) set the filename filter for the specified dialog window to the specified value
setMode(int Mode) set the mode of the file dialog .

3)ScrollPane
is container class with implements auomatic horizontal ad or vertical scrolling for a single
child component.

Constructor
ScrollPane() Create a new scroll pane container with the scroll bar policy of “as needed”
ScrollPane(int Scrollbardisplaypolicy) creates a new scrolpane container
getScrollbarDisplayPolicy() returns teh display policy for the corresponding scroll bars
setScrollPositionint x,int y) Scrolls to the specified position within the child component

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Layout Manager
1) Flow layout
2)BorderLayout
3) gridlayout

1) like word processor. the flow layout lays out components linewise from left to right when the line of
components is filled , flow layout creates a new line and continues laying out components on the next line

constructor
FlowLayout() Constructs a new flow layout with centered alignment leaving a vertical and horizontal gap of 5 pixels
FlowLayout(int align) Constructs a new flow layout with the alignment specified leaving a vertical and horizontal gap of 5 pixels
FlowLayout(int align,int vgap,int hgap) constructs a new flow layout with the alignment specified leaving a vertical and horizontal pag as specified

methods
getAlignment() gets the alignment for the layout
getHgap() get the horizontal gap between components
getVgap(0 gets the vertical gap between components
setAlignment(int align) set the alignment for the specified layout
setHgap(int hgap) set the horizontal gap for the specified layout
setVgap(int vgap) set the vertical gap for the specified layout

2)Grid Layout
The gridlayout class is like spreadsheet int rows and columns. specifying number of row and columns
in the grid creates the gridlayout.the components in a grid are resized to fit their cell. all the
components in a grid are the same size.

Constructor
GridLayout() creates a gridlayout with a default of one column per component in single row
GridLayout(int rows,int cols) Creates a grid layout with the specified rows and columns
GridLayout(int rows,int cols,int hgap,int vgap) Creates a grid layout with the specified row and columns
and specified horizontal and vertical gap

Methods
getColumns() gets the number of columns in coresponding layout
getRows() gets the number of rows in the corresponding layout
getHgap(0 get s the horizontal gap for the corresponding layout
setColumns(int cols) set the number of columns to the specified number in the corresponding layout
setHgap(int hgap) sets the horizontal gap to the value specified in the corresponding layout
stVgap(int vgap) sets the vertical gap to the value specified in the corresponding layout
setRows(int rows) sets the number of rows in the corresponding layout to the specified value

3)Border layout
with border layout the placement the component is specified as being north south east and west the boreer
alyout resizes the crenter component to fill the remaining center spaces

BorderLayout() creates a new border layout with no gap between the components
BorderLayout(int hgap,int vgap) creates a border layout with the specified horizontal and vertical gap
between components

Methods
getHgap() returns teh horizontal gap between components
getVgap() returns teh vertical gap between components
setHgap() sets teh horizontal gap between components to the value specified
setVgap(int vgap) sets the vertical gap between components to the value specified

add(“East”,mybutton);

import java.awt.*;
public class bordertest extends java.applet.Applet
{
public void init()
{
setLayout(new BorderLayout(5,5));
ad(“South”,new Button(“Buttom of the applet”));
add(“North”,new Button(“Top of the applet”));
add(“East”,new Button(“Right “));
add(“West”,new Button(“Left”));
add(“Center”,new TextArea(“Appears at the center”));
}
public Insets getInsets()
{
return new(Insets(20,20,10,10);
}
}

==============================================================================================
Menus
Menubar class
is the top most element displayed along the top of the window to which it belongs
Menubar mb=new Menubar();
myframe.setMenubar(mb);

Menu class
the menu class is used to implement a pull down menu that provides a number of items to select from.
Menu m=new Menu(“File”);
m.add(“open”);
m.add(“close”);
m.add(“exit”);

mb.add(m);

methods of menu
add(MenuItem) Adds the specified Menu item to the menu
add(String) Adds the specified string to menu.
addSeparator() adds a separator to the menu
getItem(int) returns teh item at the specified index as a Stirng
remove() deletes the item index from menu

MenuItem class
Teh menu Item class encapsulates the functionallity of the lower most components of the menu system
MenuItem mitem1=new MenuItem(“open”);
m.add(mitem);

Methods
getLabel() returns the label of the menuItem as a string
setLabel(String) change teh label of the menuitem to specified string
setEnabled(boolean) makes the menu item selectable /deselectable depending on whether the parameter is true or false

The ChecboxMenuItem classs
the CheckboxMenuItem creates a dual state menu item. it can be toggles on and off by clicking it

CheckboxMenuItem(string)
CheckboxMenuItem cmi=new CheckboxMenuItem(“view text”)

methods
getState() returns the state of the menu item as boolean value a value of true implies that the checkbox
menu item is selected
setState(boolean) Sets teh state of the menuitem if the value passed is true the menu item is set to selected state

import java.awt.*;
import java.awt.event.*;
public class MenuApplication extends Frame implements ActionListener,ItemListener
{
boolean inAnApplet=true;
TextArea output;
PopupMenu popup;
String newline;
public MenuApplication()
{
MenuBar mb;
Menu m1,m2,m3,m4,m4_1,m5;
MenuItem m1_1,m1_2,m3_1,m3_2,m3_3,m3_4,m4_1_1,m5_1,m5_2,pm1,pm2,m5_1_duplicate;
CheckboxMenuItem m2_1;
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
if(inAnApplet)
{
dispose();
}
else
{
System.exit(0);
}
}
});
newline=System.getProperty(“line.separator”);
setLayout(new BorderLayout());
output=new TextArea(5,30);
output.setEditable(false);
add(“Center”,output);
Label label=new Label(“Try bringing up”+” a popup menu!”);
add(“North”,label);
mb=new MenuBar();
setMenuBar(mb);

m1=new Menu(“Menu1”,true);
mb.add(m1);
m1_1=new MenuItem(“Menu item 1_1”);
m1.add(m1_1);
m1_2=new MenuItem(“Menu item 1_2”);
m1.add(m1_2);

m5=new Menu(“Help”);
mb.setHelpMenu(m5);
m5_1=new MenuItem(“menu item 5_1”);
m5_1.setShortcut(new MenuShortcut(KeyEvent.VK_5));
m5.add(m5_1);
m5_2=new MenuItem(“Menu item 5_2”);
m5.add(m5_2);
popup=new PopupMenu(“A popup menu”);
add(popup);
pm1=new MenuItem(“a popup menu item”);
popup.add(pm1);
m5_1_duplicate=new MenuItem(“Duplicate the menu item 5_1”,new MenuShortcut(KeyEvent.VK_5));
popup.add(m5_1_duplicate);
pm2=new MenuItem(“An item with a shortcust”,new MenuShortcut(KeyEvent.VK_6));
popup.add(pm2);
m2=new Menu(“Menu 2”);
mb.add(m2);
m2_1=new CheckboxMenuItem(“Menu item 2_1”);
m2.add(m2_1);

m3=new Menu(“Menu3”);
mb.add(m3);
m3_1=new MenuItem(“menu item 3_1”);
m3.add(m3_1);
m3_2=new MenuItem(“menu item 3_2”);
m3.add(m3_2);
m3.addSeparator();
m3_3=new MenuItem(“menu item 3_3”);
m3.add(m3_3);
m3_4=new MenuItem(“menu item 3_4”);
m3_4.setEnabled(false);
m3.add(m3_4);

m4=new Menu(“Menu4”);
mb.add(m4);

m4_1=new Menu(“Sub Menu 4_1”);
m4.add(m4_1);
m4_1_1=new MenuItem(“Menu item 4_1_1”);
m4_1.add(m4_1_1);

m1.addActionListener(this);
m2.addActionListener(this);
m3.addActionListener(this);
m4.addActionListener(this);
m4_1_1.addActionListener(this);
m5.addActionListener(this);
popup.addActionListener(this);
m1_1.setActionCommand(“1_1”);
m1_2.setActionCommand(“1_2”);
m5_1.setActionCommand(“5_1”);
m5_2.setActionCommand(“5_2”);
pm1.setActionCommand(“Pop item #1”);
m5_1_duplicate.setActionCommand(“5_1”);
pm2.setActionCommand(“popup item #2”);

m2_1.addItemListener(this);

MouseListener listener=new PopupListener();
addMouseListener(listener);
output.addMouseListener(listener);
label.addMouseListener(listener);
}

class PopupListener extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
maybeShowPopup(e);
}
public void mouseReleased(MouseEvent e)
{
maybeShowPopup(e);
}

private void maybeShowPopup(MouseEvent e)
{
if(e.isPopupTrigger())
{
popup.show(e.getComponent(),e.getX(),e.getY());
}
}
}

public void actionPerformed(ActionEvent e)
{
output.append(“\””+e.getActionCommand()+”\” action detected in menu labeled \””+((MenuItem)(e.getSource())).getLabel()+”\”.”+newline);
}

public void itemStateChanged(ItemEvent e)
{
output.append(“Item state change detected on item \””+e.getItem()+”\”(state is “+((e.getStateChange()==ItemEvent.SELECTED)?”selected).”:”deselected).”)+newline);
}
public static void main(String arg[])
{
MenuApplication m=new MenuApplication();
m.inAnApplet=false;
m.setTitle(“Menu Application”);
m.setSize(450,200);
m.setVisible(true);
}
}

==============================================================================================

Leave a Reply