SlideShare uma empresa Scribd logo
1 de 110
Presentation by Raviraj
Applet
 Introduction to Applet
Implement & Executing Applet with
Parameters
 Applet Life Cycle
Applet Class
Graphics class
AWT & swing
Presentation by Raviraj
Applet
 Applet is a special type of program that is embedded in
the webpage to generate the dynamic content.
 It runs inside the browser and works at client side.
 It works at client side so less response time.
 Secured.
 It can be executed by browsers running under many
platforms, including Linux, Windows, Mac Os etc.
 Plug-in is required at client browser to execute applet.
Presentation by Raviraj
Applets
 Applets don’t use the main() method, but when they
are load, automatically call certain methods (init,
start, paint, stop, destroy).
 They are embedded inside a web page and executed in
browsers.
 They cannot read from or write to the files on local
computer.
 They cannot communicate with other servers on the
network.
 They cannot run any programs from the local computer.
 They are restricted from using libraries from other
languages.
Presentation by Raviraj
How to run an Applet?
 There are two ways to run an applet
 By appletViewer tool

By html file.
Presentation by Raviraj
By Appletviewer
import java.applet.*;
import java.awt.*;
/*<applet code="helloA" width=200 height=500> </applet>*/
public class helloA extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.red);
g.drawString("Hello....",10,25);
}
}
Javac helloA.java
appletviewer helloA.java
Presentation by Raviraj
 Demo of Run by Appletviewer
Presentation by Raviraj
By HTML File
<html>
<applet code="helloA.class" width=200 height=500>
</applet>
</html>
Presentation by Raviraj
 Demo of Run by HTML file
Presentation by Raviraj
Applet Life Cycle
Presentation by Raviraj
1. Initialization state
 Applet enter in initialize state when it is loaded.
 In this state called init() method & overrode the code.
 This state occurs only ones in applet life cycle.
 In this state we done following task.
 Creating Object
 Load image or font
 Set text or background Color
 Setup initials values
Presentation by Raviraj
2. Running state
 Applet enter the running state when the system
call the start() method of applet class.
 This state occurs automatically after the
applet is initialization.
 Starting can also occur if the applet is already
in “stopped” state.
 We may override the start() method to execute
the statement.
Presentation by Raviraj
3. Idle or Stopped state
 Applet become idle when it is stopped from
running.
 stopping occur automatically when we leave
the current page.
 we may override the stop() method to execute
the statement.
Presentation by Raviraj
4. Dead State
 Applet said to be dead when it is removed
from memory.
 This state called destroy() automatically
when we quite from a browser.
 This state occurs only once in applet life
cycle.
 This state is used to clean up resources.
Presentation by Raviraj
5. Display State
 Applet moves to the display state whenever is
has perform some output operation on screen.
 This happens immediately after the applet enter
in to running state.
 Almost every applet will have a paint()
method.
 We must Remember Display state is not part
of Applet life cycle.
Presentation by Raviraj
 Demo of Applet Life Cycle
Presentation by Raviraj
Applet Class
Presentation by Raviraj
Applet Class Methods
Presentation by Raviraj
Graphics Class
 A Graphics object encapsulates a set of
methods that can perform graphics output.
 Specifically, it allows you to draw lines, ovals,
rectangles, strings, images, characters, and
arcs.
Presentation by Raviraj
Graphics class Methods
Presentation by Raviraj
Presentation by Raviraj
 Demo of Graphics Class
Presentation by Raviraj
Color class
 The java.awt.Color class is used to work with colors.
 Each instance of this class represents a particular
color.
 With the help of this class we can draw colored strings,
lines, and shapes in an applet.
 It is possible to fill a shape such as an oval or rectangle
with a color.
 We can also set the background and foreground
colors of an applet.
 The Color class also defines several constants that
represent specific colors.
 These are black, blue, cyan, darkGray, gray, green,
lightGray, magenta, orange pink, red, white, and
yellow.
Presentation by Raviraj
Constructors
 Color( int red, int green, int blue)
 These values should be between 0 and 255.
 Color c=new Color(0,255,0); //Green color
 Color(float red, float green, float blue)
 The value between 0.0 and 1.0
 Color(int RGB value)
 Here the RGBvalue is the mix of red, green and
blue.
 The red, blue and green stored in 8 bits.
 0-7 bits blue color.
 8-15 bits for green.
 16 to 23 bits for red.
Presentation by Raviraj
Color Class Methods
Presentation by Raviraj
 Demo of Color Class
Presentation by Raviraj
Displaying Text
 We have seen that drawstring() method of the
Graphics class is used to display text.
 We can also display strings by using different
fonts. This is necessary in many applets.
 For example, tables and graphs use various
fonts for titles, axes, data and other
information.
Presentation by Raviraj
java.awt.Font
 A font determines the size and appearance of
characters in a string. Information about a font is
encapsulated by the java.awt.Font class.
 The following is one of its constructors:
 Font (String name, int style, int ps)
 Here, name identifies the font.
 Some commonly used font names are Serif,
SansSerif, and Monospaced.
 The style may be BOLD, ITALIC, or PLAIN.
 The point size of the font is ps.
Presentation by Raviraj
 After a font has been created, you may then use it in a
graphics context.
 This is done by calling the setFont() method of the
Grahics class.
 This method has the following format:
 Void setFont (Font font)
 Here, font is a Font object. after this method is called,
any strings that are output via the drawstring() method
are displayed with that font.
 For example:
 g.setFont(new Font("serif",Font.BOLD,36));
 g.setColor(Color.black);
 g.drawString("Font example",5,100);
Presentation by Raviraj
 Demo of Font Class
Presentation by Raviraj
Applet Tag
 The applet viewer uses the HTML source
code at the beginning of a .java file to display
an applet.
 It is also possible to embed an applet in a
web page.
 The complete syntax for an applet tag is
shown in the following listing.
 Optional lines are enclosed in brackets.
Presentation by Raviraj
<applet
[codebase=url]
Code=clsName
[alt=text]
[name=appName]
Width=wpixels
Height=hpixels
[align=alignment]
[vspace=vspixels]
[hspace=hspixels]
>
[<param name=pname1 value=value1>]
[<param name=pname2 value=value2>]
…
[<param name=pnameN value=valueN>]</applet>
Presentation by Raviraj
 Demo of Applet tags of html
Presentation by Raviraj
 Demo of Using thread in Applet
Presentation by Raviraj
Presentation by Raviraj
Awt
 Abstract Windowing Toolkit (AWT) is used for GUI
programming in java.
Presentation by Raviraj
Awt hierarchy
Presentation by Raviraj
 Container:
 The Container is a component in AWT that can
contain another components like buttons,
textfields, labels etc.
 The classes that extends Container class are known as
container.
 Window:
 The window is the container that have no borders and
menubars.
 You must use frame, dialog or another window for
creating a window.
 Panel:
 The Panel is the container that doesn't contain title
bar and MenuBars.
 It can have other components like button, textfield etc.
Presentation by Raviraj
 Frame:
 The Frame is the container that contain title bar and
can have MenuBars.
 It can have other components like button, textfield
etc.
 Commonly used Methods of Component class:
 1)public void add (Component c)
 2)public void setSize (int width,int height)
 3)public void setLayout (LayoutManager m)
 4)public void setVisible (boolean)
Presentation by Raviraj
 Demo of awt Control
Presentation by Raviraj
Presentation by Raviraj
Swing
 Java swing provides the multiple platform
independent API’s interfaces for interacting
between the user and GUI components.
 All java swing classes import from the
import javax.swing.*; package.
 javax package is a java extension package.
 Java provides an interactive features for design
the GUI’s toolkit or components like: label,
button, test boxes, check boxes, combo boxes,
panels, sliders etc.
Presentation by Raviraj
 Swing components are said lightweight
components because they do not containt
platform specific code.
 Swing components have better GUI than the
AWT components.
 For example: in swing a button component
can display both the caption and also an
image on it. Which is not possible in AWT
button.
Presentation by Raviraj
Swing hierarchy
Presentation by Raviraj
JApplet Class
 The JApplet class is a subclass of the Applet
class.
 An applet must extend the JApplet class which
uses the swing component.
 The JApplet provides the support for various
panes such as the content pane, the glass
pane etc.
 To add component in JApplet, you have to call
add() method by a content pane object.
Presentation by Raviraj
 To obtain a content pane object, following
method is used.
 Container getContentPane()
 Now to add a component in this pane, the add()
method is used.
 Void add (componnentObj)
 Here, the componentObj is the component which
is to be added to the JApplet.
Presentation by Raviraj
JComponent Class
 The JComponent class is a subclass of Container
class.
 This class have many subclasses such as
 JFrame,
 JPanel
 JLabel,
 JButton,
 JRadioButton,
 JCheckBox
 JTextField, JPasswordField,
 JTextArea
 JScrollBar,
 JComboBox,
 JList
 Menus (JMenuBar, JMenu,JMenuItem)
Presentation by Raviraj
JLabel
 JLabel class is used to display a label on the window.
 This label can have a text as well as an image also.
 Constructors:
 JLabel (String label)
 JLabel (Icon i)
 JLabel (String label, Icon I, int align)
 Here, the label is the String which will be displayed
as the label.
 The align specifies the alignment which can be
JLabel.LEFT, JLabel.RIGHT, JLabel.CENTER.
 The icon is an interface which is implemented by
ImageIcon Class. This class draw an image.
Presentation by Raviraj
 Constructors are:
 ImageIcon (String filename)
 ImageIcon (URL url)
 Here, finename is the name of the imagefile
and the url is the path of the file.
Presentation by Raviraj
Methods of JLabel
 public String getText()
 Returns the text string that the label displays.
 public void setText(String text)
 Defines the single line of text this component will
display. If the value of text is null or empty string,
nothing is displayed.
 public Icon getIcon()
 Returns the graphic image that the label displays.
 public void setIcon(Icon icon)
 Defines the icon this component will display. If the
value of icon is null, nothing is displayed.
 public void setVisible(boolean b)
 Shows or hides this Window depending on the value
of parameter b.
Presentation by Raviraj
 public void setBounds(int x,int y,int width,int height)
 Moves and resizes this component. The new location of
the top-left corner is specified by x and y, and the new
size is specified by width and height.
 Parameters: x - the new x-coordinate of this component
 y - the new y-coordinate of this component
 width - the new width of this component
 height - the new height of this component
 public void setEnabled(boolean b)
 Enables or disables this component, depending on the
value of the parameter b.
 An enabled component can respond to user input and
generate events. Components are enabled initially by
default.
Presentation by Raviraj
JTextField
 The JTextField class creates a text field which can
accept the user input.
 Constructors are:
 JTextField()
 JTextField (String str)
 JTextField (int columns)
 JTextField (String str, int columns)
 The first form creates a default text field which
contains no string in it.
 The second form str specified the string which will
be displayed in the text field
Presentation by Raviraj
 The columns parameter specifies the width of
the text field. this width does not specifies the
limit of the number of characters that can be
entered in the text field.
 The JTextField class generates an ActionEcent
when user presses the enter key after input the
data in the text field.
Presentation by Raviraj
Methods of JTextField
 public int getColumns()
 Returns the number of columns in this TextField.
 Returns: the number of columns >= 0
 public void setColumns(int columns)
 Sets the number of columns in this TextField, and then
invalidate the layout.
 Parameters: columns - the number of columns >= 0
 public void setText(String text)
 Set the specified text in this JTextField.
 public String getText()
 Return the text of this JtextField.
Presentation by Raviraj
 Demo of JLabel and JTextField class
Presentation by Raviraj
JButton
 JButton is used to display a text as well as an image on it.
 The swin buttons are subclass of the AbstractButton
class.
 Constructors:
 public JButton()
 Creates a button with no set text or icon
 public JButton(Icon icon)
 Creates a button with an icon.
 Parameters: icon - the Icon image to display on the
button
 public JButton(String text)
 Creates a button with text.
 Parameters: text - the text of the button
Presentation by Raviraj
 public JButton(String text,Icon icon)
 Creates a button with initial text and an icon.
 Parameters: text - the text of the button
 icon - the Icon image to display on the button
Presentation by Raviraj
 Demo of JButton
Presentation by Raviraj
JRadioButton class
 The JRadioButton is a subclass of the AbstractButton
class.itis used to create radio buttons.
 An implementation of a radio button -- an item that can
be selected or deselected, and which displays its
state to the user.
 Used with a ButtonGroup object to create a group of
buttons in which only one button at a time can be
selected. (Create a ButtonGroup object and use its add
method to include the JRadioButton objects in the
group.)
 ButtonGroup bg = new ButtonGroup();
 bg.add(b1);
 bg.add(b2);
 bg.add(b3);
Presentation by Raviraj
 A ButtonGroup can be used with any set of
objects that inherit from AbstractButton.
Typically a button group contains instances of
JRadioButton.
 public ButtonGroup()
 Creates a new ButtonGroup.
 Void add(AbstractButton obj)
 Obj is the object of the JRadioButton to be added.
 public int getButtonCount()
 Returns the number of buttons in the group.
Presentation by Raviraj
Constructors JRadioButton
 public JRadioButton()
 public JRadioButton(Icon icon)
 public JRadioButton(Icon icon,boolean selected)
 public JRadioButton(String text)
 public JRadioButton(String text,boolean selected)
 public JRadioButton(String text,Icon icon)
 public JRadioButton
(String text,Icon icon,boolean selected)
Presentation by Raviraj
Methods
 public String getText()
 Returns the text string that the JRadioButton displays.
 public void setText(String text)
 Defines the single line of text this component will display. If the
value of text is null or empty string, nothing is displayed.
 public Icon getIcon()
 Returns the graphic image that the JRadionButton displays.
 public void setIcon(Icon icon)
 Defines the icon this component will display. If the value of icon
is null, nothing is displayed.
 public void setBounds(int x,int y,int width,int height)
 public void setVisible(boolean b)
 public void setEnabled(boolean b)
 public Boolean isSelected()
Presentation by Raviraj
 Demo of JRadioButton
Presentation by Raviraj
JTextArea class
 JTextArea class is used to create a multi line text input.
 This text does not display any scrollbars but you should
use scroll panes if need it.
 Constructors
 public JTextArea()
 public JTextArea(String text)
 public JTextArea(int rows,int columns)
 public JTextArea(String text,int rows,int columns)
Presentation by Raviraj
Methods of JTextArea
 public void setLineWrap(boolean wrap)
 Sets the line-wrapping policy of the text area.
 public boolean getLineWrap()
 Gets the line-wrapping policy of the text area.
 public int getLineCount()
 Determines the number of lines contained in the
area. Returns: the number of lines > 0
 public void insert(String str,int pos)
 Inserts the specified text at the specified position.
 public void append(String str)
 Appends the given text to the end of the document
Presentation by Raviraj
 public void replaceRange(String str,int start,int end)
 Replaces text from the indicated start to end position with
the new text specified.
 public int getRows()
 Returns the number of rows in the TextArea.
 Returns: the number of rows >= 0
 public int getColumns()
 Returns the number of columns in the TextArea.
 Returns: number of columns >= 0
 public void setFont(Font f)
 is used to set the specified font.
Presentation by Raviraj
 Demo of JTextArea class
Presentation by Raviraj
JPasswordField Class
 The JPasswordField creates a text field
which display the disks (dots) instead of the
actual characters.
 Constructors
 JPasswordField()
 JPasswordField(int length)
 JPasswordField(String str)
 JPasswordField(String str, int length)
Presentation by Raviraj
Methods of JPasswordField
 public char getEchoChar()
 Returns the character to be used for echoing. The
default is '*'.
 public void setEchoChar(char c)
 Sets the echo character for this JPasswordField.
 public boolean echoCharIsSet()
 Returns true if this JPasswordField has a character
set for echoing.
 public char[] getPassword()
 Returns the text contained in this TextComponent.
Presentation by Raviraj
 Demo of JPasswordField Class
Presentation by Raviraj
JCheckBox Class
 This class is a subclass of the AbstactButton
class.
 It is used to create a checkbox.
 An implementation of a check box -- an item
that can be selected or deselected, and which
displays its state to the user.
Presentation by Raviraj
Constructors
 public JCheckBox()
 public JCheckBox(Icon icon)
 public JCheckBox(Icon icon,boolean selected)
 public JCheckBox(String text)
 public JCheckBox(String text,boolean selected)
 public JCheckBox(String text,Icon icon)
Presentation by Raviraj
Methods of CheckBox class
 public String getText()
 Returns the text string that the JCheckBox displays.
 public void setText(String text)
 Defines the single line of text this component will display. If
the value of text is null or empty string, nothing is
displayed.
 public Icon getIcon()
 Returns the graphic image that the JCheckBox displays.
 public void setIcon(Icon icon)
 Defines the icon this component will display. If the value of
icon is null, nothing is displayed.
Presentation by Raviraj
 public void
setBounds(int x,int y,int width,int height)
 Moves and resizes this component. The new location of the
top-left corner is specified by x and y, and the new size is
specified by width and height.
 public void setVisible(boolean b)
 Shows or hides this Window depending on the value of
parameter b.
 public void setEnabled(boolean b)
 Enables or disables this component, depending on the value of
the parameter b. An enabled component can respond to user
input and generate events. Components are enabled initially by
default.
 public Boolean isSelected()
 Test for this JCheckBox is selected or not.
Presentation by Raviraj
 Demo of JCheckBox
Presentation by Raviraj
JComboBox class
 A component that combines a button or
editable field and a drop-down list.
 The user can select a value from the drop-
down list, which appears at the user's request.
 If you make the combo box editable, then the
combo box includes an editable field into which
the user can type a value.
Presentation by Raviraj
Constructors
 public JComboBox()
 Creates a JComboBox with a default data model. The default
data model is an empty list of objects. Use addItem to add
items. By default the first item in the data model becomes
selected.
 public JComboBox(Object[] items)
 Creates a JComboBox that contains the elements in the
specified array. By default the first item in the array (and
therefore the data model) becomes selected.
 public JComboBox(Vector<?> items)
 Creates a JComboBox that contains the elements in the
specified Vector. By default the first item in the vector (and
therefore the data model) becomes selected.
Presentation by Raviraj
Methods of ComboBox
 public void setEditable(boolean aFlag)
 Determines whether the JComboBox field is editable.
An editable JComboBox allows the user to type into
the field or selected an item from the list to initialize
the field, after which it can be edited.
 public void setMaximumRowCount(int count)
 Sets the maximum number of rows the JComboBox
displays. If the number of objects in the model is
greater than count, the combo box uses a scrollbar.
 count - an integer specifying the maximum number
of items to display in the list before using a scrollbar.
Presentation by Raviraj
 public int getMaximumRowCount()
 Returns the maximum number of items the combo
box can display without a scrollbar.
 Returns: an integer specifying the maximum
number of items that are displayed in the list before
using a scrollbar
 public void setSelectedItem(Object anObject)
 Sets the selected item in the combo box display
area to the object in the argument. If anObject is in
the list, the display area shows anObject selected.
 public Object getSelectedItem()
 Returns the current selected item.
 public void setSelectedIndex(int anIndex)
 Selects the item at index anIndex.
Presentation by Raviraj
 int getSelectedIndex()
 Returns the first item in the list that matches the given item. The
result is not always defined if the JComboBox allows selected
items that are not in the list. Returns -1 if there is no selected
item or if the user specified an item which is not in the list.
 public void addItem(Object anObject)
 Adds an item to the item list. This method works only if the
JComboBox uses a mutable data model.
 public void insertItemAt(Object anObject,int index)
 Inserts an item into the item list at a given index. This method
works only if the JComboBox uses a mutable data model.
 Parameters: anObject - the Object to add to the list
 index - an integer specifying the position at which to add the
item
Presentation by Raviraj
 public void removeItem(Object anObject)
 Removes an item from the item list. This method
works only if the JComboBox uses a mutable data
model.
 Parameters: anObject - the object to remove from
the item list.
 public void removeItemAt(int anIndex)
 Removes the item at anIndex This method works
only if the JComboBox uses a mutable data
model.
 Parameters: anIndex - an int specifying the index
of the item to remove, where 0 indicates the first
item in the list
Presentation by Raviraj
 public void removeAllItems()
 Removes all items from the item list.
 public void setEnabled(boolean b)
 Enables the combo box so that items can be selected.
 public int getItemCount()
 Returns the number of items in the list.
 Returns: an integer equal to the number of items in the list
 public Object getItemAt(int index)
 Returns the list item at the specified index. If index is out of
range (less than zero or greater than or equal to size) it will
return null.
 Parameters: index - an integer indicating the list position, where
the first item starts at zero
 Returns: the Object at that list position; or null if out of range
Presentation by Raviraj
 Demo of JComboBox
Presentation by Raviraj
JScrollPane Class
 A scroll pane is used to display other components or an
image in a rectangular area.
 This pane have horizontal as well as vertical scroll
bars.
 Constructors
 JScrollPane(Component obj)
 JScrollPane(int vertScrollbar,int horScrollbar)
 JScrollPane(Component obj,int vertScrollbar,int
horScrollbar
Presentation by Raviraj
Constants in JScrollPane
 HORIZONTAL_SCROLLBAR_ALWAYS
 HORIZONTAL_SCROLLBAR_AS_NEEDED
 VERICAL_SCROLLBAR_ALWAYS
 VERTICAL_SCROLLBAR_AS_NEEDED
Presentation by Raviraj
Methods of JScrollaPane
 public int getValue()
 Returns the scrollbar's value.
 public void setValue(int value)
 Sets the scrollbar's value..
 public int getMinimum()
 Returns the minimum value supported by the scrollbar (usually zero).
 public void setMinimum(int minimum)
 Sets the model's minimum property.
 public int getMaximum()
 The maximum value of the scrollbar is maximum - extent.
 public void setMaximum(int maximum)
 Sets the model's maximum property.
 public void setEnabled(boolean x)
 Enables the component so that the knob position can be changed.
When the disabled, the knob position cannot be changed.
Presentation by Raviraj
 Demo of JScrollPane
 Demo of JScrollPane2
Presentation by Raviraj
JScrollBar Class
 JScrollBar class is used to create vertical and
horizontal scrollbars.
 Constructors:
 JScrollBar()
 JScrollBar (int type)
 JScrollBar (int type, int value, int extent, int min,
int max)
Presentation by Raviraj
 JScrollBar (int type, int value, int extent,
int min, int max)
 Here, type specifies that whether the scrollbar is
horizontal or vertical.
 This is defined by two constants:
 JScrollBar.HORIZONTAL, JScollBar.VERTICAL
 The value specifies the initial value of the scrollbar
slider.
 The extent is the extent the value of the slider.
 The min and max specifies the range of the
scrollbar.
Presentation by Raviraj
 The scrollbar generates an adjustment event.
 The AdjustmentListener interface contains
the method adjustmentValueChanged()
which is called when scrollbar value is
adjusted.
 A method of JScrollBar is int getValue().
 It returns the value of the scrollbar
Presentation by Raviraj
 Demo of JScrollBar
Presentation by Raviraj
JList class
 The list class creates a list box which can
display more than one items in the combo box
at a time.
 It lets user to select more than one item from the
list.
 If the items in the list are too many to display all
in the list box, you have to add it in a scroll pane
as it does not display it by default.
Presentation by Raviraj
Constructors for JList
 public JList()
 Constructs a JList with an empty, read-only, model.
 public JList(Vector<?> listData)
 Constructs a JList that displays the elements in the
specified Vector.
 public JList(Object[] listData)
 Constructs a JList that displays the elements in the
specified array.
Presentation by Raviraj
Methods of JList
 Void setSelectionMode(int selectionMode)
 The selection mode can be specified by the following
constants defined by the ListSelectionModel.
 ListSelectionModel.SINGLE_SELECTION
 ListSelectionModel.SINGLE_INTERVAL_SELECTION
 ListSelectionModel.MULTIPLE_INTERVAL_SELECTION
 public int getVisibleRowCount()
 Returns the value of the visibleRowCount property.
 public void setVisibleRowCount(int visibleRowCount)
 It sets the number of items that can be visible in the list box
at once.
Presentation by Raviraj
 public boolean isSelectedIndex(int index)
 Returns true if the specified index is selected, else false.
 public int[] getSelectedIndices()
 Returns an array of all of the selected indices, in increasing
order.
 public void setSelectedIndices(int[] indices)
 Changes the selection to be the set of indices specified by the
given array.
 public Object[] getSelectedValues()
 Returns an array of all the selected values, in increasing order
based on their indices in the list.
 public int getSelectedIndex()
 Returns the smallest selected cell index; the selection when
only a single item is selected in the list.
Presentation by Raviraj
 Demo of JList
Presentation by Raviraj
JFrame Class
 The JFrame class creates a frame which is a window.
 This window has a border, a title, and buttons for
closing, minimizing, maximizing the window.
 Constructors
 public JFrame()
 Constructs a new frame that is initially invisible.
 public JFrame (String title)
 Creates a new, initially invisible Frame with the specified
title.
Presentation by Raviraj
Methods of JFrame
 public void setDefaultCloseOperation(int operation)
 Sets the operation that will happen by default when the user initiates a
"close" on this frame.
 You must specify one of the following choices:
 DO_NOTHING_ON_CLOSE
 It does nothing when the close button is pressed.
 HIDE_ON_CLOSE
 It hides the window when you press the close button, but still it remains
alive. This is the default operation.
 DISPOSE_ON_CLOSE
 This hides and disposes the window when you press the close button
in title bar.
 it removes the window from the screen and removes it from memory
also.
 EXIT_ON_CLOSE
 It exits the whole application on pressing the close button using
System.exit(0) method.
Presentation by Raviraj
 public void setJMenuBar(JMenuBar menubar)
 Sets the menubar for this frame.
 public void remove(Component comp)
 Removes the specified component from the container.
 public void setLayout(LayoutManager manager)
 Sets the LayoutManager.
 public Container getContentPane()
 Returns the contentPane object for this frame.
 public void setTitle(String title)
 Sets the title for this frame to the specified string.
 title - the title to be displayed in the frame's border. A null
value is treated as an empty string, "".
 - public String getTitle()
 Gets the title of the frame. The title is displayed in the frame's
border.
 Returns: the title of this frame, or an empty string ("") if this
frame doesn't have a title.
Presentation by Raviraj
 public void
setBounds(int x,int y,int width,int height)
 Moves and resizes this component. The new location of the top-
left corner is specified by x and y, and the new size is specified
by width and height.
 Parameters: x - the new x-coordinate of this component
 y - the new y-coordinate of this component
 width - the new width of this component
 height - the new height of this component
 public void setVisible (boolean b)
 Shows or hides this Window depending on the value of
parameter b.
 public void setEnabled (boolean b)
 Enables or disables this component, depending on the value of
the parameter b.
 An enabled component can respond to user input and generate
events. Components are enabled initially by default.
Presentation by Raviraj
 Demo of JFrame 1
 Demo of JFrame 2
Presentation by Raviraj
JMenu, JMenuBar and JMenuItem
 These classes provide functionalities to
create a menu bar which contains menus.
 These menus in turn have menu items.
 The menu bar is created using JMenubar
class.
 Menu is created using JMenu class and the
JMenuItem creates menu items.
Presentation by Raviraj
Steps for creating menu
1. Create a JMenuBar object.
2. Then set the menu bar using setMenuBar()
method.
3. Create menu objects of JMenu class.
4. Create menu item odjects using JMenuItem
class.
5. Add the menu items to the particular menus.
6. Add the menus to the menu bar.
Presentation by Raviraj
JMenuBar Class
 JMenuBar class creates a menu bar which
contains the menus.
 Constructor
 public JMenuBar()
 Creates a new menu bar.
Presentation by Raviraj
Methods of JMenuBar class
 public JMenu add (JMenu c)
 Appends the specified menu to the end of the menu
bar.
 Parameters: c - the JMenu component to add
 Returns: the menu component
 public int getMenuCount()
 Returns the number of items in the menu bar.
 Returns: the number of items in the menu bar.
Presentation by Raviraj
JMenu Class
 An implementation of a menu -- a popup
window containing JMenuItems that is displayed
when the user selects an item on the JMenuBar.
 The JMenu class is a container of menus.
 Constructors
 public JMenu()
 Constructs a new JMenu with no text.
 public JMenu (String s)
 Constructs a new JMenu with the supplied string as
its text.
 Parameters: s - the text for the menu label
Presentation by Raviraj
Methods of JMenu Class
 public boolean isSelected()
 Returns true if the menu is currently selected
(highlighted).
 public void setSelected (boolean b)
 Sets the selection status of the menu
 public JMenuItem add (JMenuItem menuItem)
 Appends a menu item to the end of this menu.
Returns the menu item added.
 Parameters: menuItem - the JMenuitem to be
added
 Returns: the JMenuItem added
Presentation by Raviraj
 public void addSeparator()
 Appends a new separator to the end of the menu.
 public int getItemCount()
 Returns the number of items on the menu, including
separators.
 public void remove (JMenuItem item)
 Removes the specified menu item from this menu. If
there is no popup menu, this method will have no
effect.
 Parameters: item - the JMenuItem to be removed
from the menu
 public void removeAll()
 Removes all menu items from this menu.
Presentation by Raviraj
JMenuItem Class
 The JMenuItem class is used to create
menu items.these menu items will be added
to the menu.
 An implementation of an item in a menu. A
menu item is essentially a button sitting in a
list.
 When the user selects the "button", the
action associated with the menu item is
performed.
Presentation by Raviraj
Constructors of JMenuItem
 public JMenuItem()
 Creates a JMenuItem with no set text or icon.
 public JMenuItem(String text)
 Creates a JMenuItem with the specified text.
 Parameters: text - the text of the JMenuItem
 public JMenuItem (Icon icn)
 public JMenuItem (String txt, Icon icr)
Presentation by Raviraj
 Demo of JMenu
 Demo of JMenu 2

Mais conteúdo relacionado

Mais procurados

Applet life cycle
Applet life cycleApplet life cycle
Applet life cycle
myrajendra
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
parag
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
BHUVIJAYAVELU
 

Mais procurados (20)

Applet life cycle
Applet life cycleApplet life cycle
Applet life cycle
 
Multi threading
Multi threadingMulti threading
Multi threading
 
Java threads
Java threadsJava threads
Java threads
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Java Server Pages(jsp)
Java Server Pages(jsp)Java Server Pages(jsp)
Java Server Pages(jsp)
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
JVM
JVMJVM
JVM
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
 
Java Tokens
Java  TokensJava  Tokens
Java Tokens
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
 
Features of java
Features of javaFeatures of java
Features of java
 
This keyword in java
This keyword in javaThis keyword in java
This keyword in java
 
Java multi threading
Java multi threadingJava multi threading
Java multi threading
 
Java Streams
Java StreamsJava Streams
Java Streams
 
Java Presentation For Syntax
Java Presentation For SyntaxJava Presentation For Syntax
Java Presentation For Syntax
 
Files in java
Files in javaFiles in java
Files in java
 
Vectors in Java
Vectors in JavaVectors in Java
Vectors in Java
 

Destaque

Java applets
Java appletsJava applets
Java applets
lopjuan
 
21servers And Applets
21servers And Applets21servers And Applets
21servers And Applets
Adil Jafri
 

Destaque (20)

Applet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java AppletsApplet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java Applets
 
Java applets
Java appletsJava applets
Java applets
 
Java applet
Java appletJava applet
Java applet
 
Applet java
Applet javaApplet java
Applet java
 
Java applets
Java appletsJava applets
Java applets
 
27 applet programming
27  applet programming27  applet programming
27 applet programming
 
java Applet Introduction
java Applet Introductionjava Applet Introduction
java Applet Introduction
 
ITFT- Applet in java
ITFT- Applet in javaITFT- Applet in java
ITFT- Applet in java
 
Java Applet and Graphics
Java Applet and GraphicsJava Applet and Graphics
Java Applet and Graphics
 
Applet and graphics programming
Applet and graphics programmingApplet and graphics programming
Applet and graphics programming
 
Applet progming
Applet progmingApplet progming
Applet progming
 
JAVA Servlets
JAVA ServletsJAVA Servlets
JAVA Servlets
 
Javalecture 1
Javalecture 1Javalecture 1
Javalecture 1
 
21servers And Applets
21servers And Applets21servers And Applets
21servers And Applets
 
Asif
AsifAsif
Asif
 
Applet intro
Applet introApplet intro
Applet intro
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Applet
AppletApplet
Applet
 
exception handling
exception handlingexception handling
exception handling
 
Java Servlet
Java Servlet Java Servlet
Java Servlet
 

Semelhante a Java Applet

Semelhante a Java Applet (20)

AdvancedJava.pptx
AdvancedJava.pptxAdvancedJava.pptx
AdvancedJava.pptx
 
JavaAdvUnit-1.pptx
JavaAdvUnit-1.pptxJavaAdvUnit-1.pptx
JavaAdvUnit-1.pptx
 
Java files and io streams
Java files and io streamsJava files and io streams
Java files and io streams
 
Unit 7 Java
Unit 7 JavaUnit 7 Java
Unit 7 Java
 
Applet &amp; graphics programming
Applet &amp; graphics programmingApplet &amp; graphics programming
Applet &amp; graphics programming
 
GUI Programming In Java
GUI Programming In JavaGUI Programming In Java
GUI Programming In Java
 
Applet and graphics programming
Applet and graphics programmingApplet and graphics programming
Applet and graphics programming
 
GUI.pdf
GUI.pdfGUI.pdf
GUI.pdf
 
Basic of Applet
Basic of AppletBasic of Applet
Basic of Applet
 
Applet in java
Applet in javaApplet in java
Applet in java
 
Applet in java new
Applet in java newApplet in java new
Applet in java new
 
Applets
AppletsApplets
Applets
 
L18 applets
L18 appletsL18 applets
L18 applets
 
Applets - lev' 2
Applets - lev' 2Applets - lev' 2
Applets - lev' 2
 
GUI (graphical user interface)
GUI (graphical user interface)GUI (graphical user interface)
GUI (graphical user interface)
 
Applet (1)
Applet (1)Applet (1)
Applet (1)
 
JAVA (UNIT 5)
JAVA (UNIT 5)JAVA (UNIT 5)
JAVA (UNIT 5)
 
java.pptx
java.pptxjava.pptx
java.pptx
 
Applet
AppletApplet
Applet
 
Appletjava
AppletjavaAppletjava
Appletjava
 

Mais de Shree M.L.Kakadiya MCA mahila college, Amreli

Mais de Shree M.L.Kakadiya MCA mahila college, Amreli (20)

Machine Learning by Rj
Machine Learning by RjMachine Learning by Rj
Machine Learning by Rj
 
Listeners and filters in servlet
Listeners and filters in servletListeners and filters in servlet
Listeners and filters in servlet
 
Servlet unit 2
Servlet unit 2 Servlet unit 2
Servlet unit 2
 
Servlet by Rj
Servlet by RjServlet by Rj
Servlet by Rj
 
Research paper on python by Rj
Research paper on python by RjResearch paper on python by Rj
Research paper on python by Rj
 
Networking in python by Rj
Networking in python by RjNetworking in python by Rj
Networking in python by Rj
 
Jsp in Servlet by Rj
Jsp in Servlet by RjJsp in Servlet by Rj
Jsp in Servlet by Rj
 
Motion capture by Rj
Motion capture by RjMotion capture by Rj
Motion capture by Rj
 
Research paper on big data and hadoop
Research paper on big data and hadoopResearch paper on big data and hadoop
Research paper on big data and hadoop
 
Text processing by Rj
Text processing by RjText processing by Rj
Text processing by Rj
 
Python and data analytics
Python and data analyticsPython and data analytics
Python and data analytics
 
Multithreading by rj
Multithreading by rjMultithreading by rj
Multithreading by rj
 
Django by rj
Django by rjDjango by rj
Django by rj
 
Database programming
Database programmingDatabase programming
Database programming
 
CGI by rj
CGI by rjCGI by rj
CGI by rj
 
Adv. python regular expression by Rj
Adv. python regular expression by RjAdv. python regular expression by Rj
Adv. python regular expression by Rj
 
Seminar on Project Management by Rj
Seminar on Project Management by RjSeminar on Project Management by Rj
Seminar on Project Management by Rj
 
Spring by rj
Spring by rjSpring by rj
Spring by rj
 
Python by Rj
Python by RjPython by Rj
Python by Rj
 
Leadership & Motivation
Leadership & MotivationLeadership & Motivation
Leadership & Motivation
 

Último

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Último (20)

GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 

Java Applet

  • 1. Presentation by Raviraj Applet  Introduction to Applet Implement & Executing Applet with Parameters  Applet Life Cycle Applet Class Graphics class AWT & swing
  • 2. Presentation by Raviraj Applet  Applet is a special type of program that is embedded in the webpage to generate the dynamic content.  It runs inside the browser and works at client side.  It works at client side so less response time.  Secured.  It can be executed by browsers running under many platforms, including Linux, Windows, Mac Os etc.  Plug-in is required at client browser to execute applet.
  • 3. Presentation by Raviraj Applets  Applets don’t use the main() method, but when they are load, automatically call certain methods (init, start, paint, stop, destroy).  They are embedded inside a web page and executed in browsers.  They cannot read from or write to the files on local computer.  They cannot communicate with other servers on the network.  They cannot run any programs from the local computer.  They are restricted from using libraries from other languages.
  • 4. Presentation by Raviraj How to run an Applet?  There are two ways to run an applet  By appletViewer tool  By html file.
  • 5. Presentation by Raviraj By Appletviewer import java.applet.*; import java.awt.*; /*<applet code="helloA" width=200 height=500> </applet>*/ public class helloA extends Applet { public void paint(Graphics g) { g.setColor(Color.red); g.drawString("Hello....",10,25); } } Javac helloA.java appletviewer helloA.java
  • 6. Presentation by Raviraj  Demo of Run by Appletviewer
  • 7. Presentation by Raviraj By HTML File <html> <applet code="helloA.class" width=200 height=500> </applet> </html>
  • 8. Presentation by Raviraj  Demo of Run by HTML file
  • 10. Presentation by Raviraj 1. Initialization state  Applet enter in initialize state when it is loaded.  In this state called init() method & overrode the code.  This state occurs only ones in applet life cycle.  In this state we done following task.  Creating Object  Load image or font  Set text or background Color  Setup initials values
  • 11. Presentation by Raviraj 2. Running state  Applet enter the running state when the system call the start() method of applet class.  This state occurs automatically after the applet is initialization.  Starting can also occur if the applet is already in “stopped” state.  We may override the start() method to execute the statement.
  • 12. Presentation by Raviraj 3. Idle or Stopped state  Applet become idle when it is stopped from running.  stopping occur automatically when we leave the current page.  we may override the stop() method to execute the statement.
  • 13. Presentation by Raviraj 4. Dead State  Applet said to be dead when it is removed from memory.  This state called destroy() automatically when we quite from a browser.  This state occurs only once in applet life cycle.  This state is used to clean up resources.
  • 14. Presentation by Raviraj 5. Display State  Applet moves to the display state whenever is has perform some output operation on screen.  This happens immediately after the applet enter in to running state.  Almost every applet will have a paint() method.  We must Remember Display state is not part of Applet life cycle.
  • 15. Presentation by Raviraj  Demo of Applet Life Cycle
  • 18. Presentation by Raviraj Graphics Class  A Graphics object encapsulates a set of methods that can perform graphics output.  Specifically, it allows you to draw lines, ovals, rectangles, strings, images, characters, and arcs.
  • 21. Presentation by Raviraj  Demo of Graphics Class
  • 22. Presentation by Raviraj Color class  The java.awt.Color class is used to work with colors.  Each instance of this class represents a particular color.  With the help of this class we can draw colored strings, lines, and shapes in an applet.  It is possible to fill a shape such as an oval or rectangle with a color.  We can also set the background and foreground colors of an applet.  The Color class also defines several constants that represent specific colors.  These are black, blue, cyan, darkGray, gray, green, lightGray, magenta, orange pink, red, white, and yellow.
  • 23. Presentation by Raviraj Constructors  Color( int red, int green, int blue)  These values should be between 0 and 255.  Color c=new Color(0,255,0); //Green color  Color(float red, float green, float blue)  The value between 0.0 and 1.0  Color(int RGB value)  Here the RGBvalue is the mix of red, green and blue.  The red, blue and green stored in 8 bits.  0-7 bits blue color.  8-15 bits for green.  16 to 23 bits for red.
  • 25. Presentation by Raviraj  Demo of Color Class
  • 26. Presentation by Raviraj Displaying Text  We have seen that drawstring() method of the Graphics class is used to display text.  We can also display strings by using different fonts. This is necessary in many applets.  For example, tables and graphs use various fonts for titles, axes, data and other information.
  • 27. Presentation by Raviraj java.awt.Font  A font determines the size and appearance of characters in a string. Information about a font is encapsulated by the java.awt.Font class.  The following is one of its constructors:  Font (String name, int style, int ps)  Here, name identifies the font.  Some commonly used font names are Serif, SansSerif, and Monospaced.  The style may be BOLD, ITALIC, or PLAIN.  The point size of the font is ps.
  • 28. Presentation by Raviraj  After a font has been created, you may then use it in a graphics context.  This is done by calling the setFont() method of the Grahics class.  This method has the following format:  Void setFont (Font font)  Here, font is a Font object. after this method is called, any strings that are output via the drawstring() method are displayed with that font.  For example:  g.setFont(new Font("serif",Font.BOLD,36));  g.setColor(Color.black);  g.drawString("Font example",5,100);
  • 29. Presentation by Raviraj  Demo of Font Class
  • 30. Presentation by Raviraj Applet Tag  The applet viewer uses the HTML source code at the beginning of a .java file to display an applet.  It is also possible to embed an applet in a web page.  The complete syntax for an applet tag is shown in the following listing.  Optional lines are enclosed in brackets.
  • 32. Presentation by Raviraj  Demo of Applet tags of html
  • 33. Presentation by Raviraj  Demo of Using thread in Applet
  • 35. Presentation by Raviraj Awt  Abstract Windowing Toolkit (AWT) is used for GUI programming in java.
  • 37. Presentation by Raviraj  Container:  The Container is a component in AWT that can contain another components like buttons, textfields, labels etc.  The classes that extends Container class are known as container.  Window:  The window is the container that have no borders and menubars.  You must use frame, dialog or another window for creating a window.  Panel:  The Panel is the container that doesn't contain title bar and MenuBars.  It can have other components like button, textfield etc.
  • 38. Presentation by Raviraj  Frame:  The Frame is the container that contain title bar and can have MenuBars.  It can have other components like button, textfield etc.  Commonly used Methods of Component class:  1)public void add (Component c)  2)public void setSize (int width,int height)  3)public void setLayout (LayoutManager m)  4)public void setVisible (boolean)
  • 39. Presentation by Raviraj  Demo of awt Control
  • 41. Presentation by Raviraj Swing  Java swing provides the multiple platform independent API’s interfaces for interacting between the user and GUI components.  All java swing classes import from the import javax.swing.*; package.  javax package is a java extension package.  Java provides an interactive features for design the GUI’s toolkit or components like: label, button, test boxes, check boxes, combo boxes, panels, sliders etc.
  • 42. Presentation by Raviraj  Swing components are said lightweight components because they do not containt platform specific code.  Swing components have better GUI than the AWT components.  For example: in swing a button component can display both the caption and also an image on it. Which is not possible in AWT button.
  • 44. Presentation by Raviraj JApplet Class  The JApplet class is a subclass of the Applet class.  An applet must extend the JApplet class which uses the swing component.  The JApplet provides the support for various panes such as the content pane, the glass pane etc.  To add component in JApplet, you have to call add() method by a content pane object.
  • 45. Presentation by Raviraj  To obtain a content pane object, following method is used.  Container getContentPane()  Now to add a component in this pane, the add() method is used.  Void add (componnentObj)  Here, the componentObj is the component which is to be added to the JApplet.
  • 46. Presentation by Raviraj JComponent Class  The JComponent class is a subclass of Container class.  This class have many subclasses such as  JFrame,  JPanel  JLabel,  JButton,  JRadioButton,  JCheckBox  JTextField, JPasswordField,  JTextArea  JScrollBar,  JComboBox,  JList  Menus (JMenuBar, JMenu,JMenuItem)
  • 47. Presentation by Raviraj JLabel  JLabel class is used to display a label on the window.  This label can have a text as well as an image also.  Constructors:  JLabel (String label)  JLabel (Icon i)  JLabel (String label, Icon I, int align)  Here, the label is the String which will be displayed as the label.  The align specifies the alignment which can be JLabel.LEFT, JLabel.RIGHT, JLabel.CENTER.  The icon is an interface which is implemented by ImageIcon Class. This class draw an image.
  • 48. Presentation by Raviraj  Constructors are:  ImageIcon (String filename)  ImageIcon (URL url)  Here, finename is the name of the imagefile and the url is the path of the file.
  • 49. Presentation by Raviraj Methods of JLabel  public String getText()  Returns the text string that the label displays.  public void setText(String text)  Defines the single line of text this component will display. If the value of text is null or empty string, nothing is displayed.  public Icon getIcon()  Returns the graphic image that the label displays.  public void setIcon(Icon icon)  Defines the icon this component will display. If the value of icon is null, nothing is displayed.  public void setVisible(boolean b)  Shows or hides this Window depending on the value of parameter b.
  • 50. Presentation by Raviraj  public void setBounds(int x,int y,int width,int height)  Moves and resizes this component. The new location of the top-left corner is specified by x and y, and the new size is specified by width and height.  Parameters: x - the new x-coordinate of this component  y - the new y-coordinate of this component  width - the new width of this component  height - the new height of this component  public void setEnabled(boolean b)  Enables or disables this component, depending on the value of the parameter b.  An enabled component can respond to user input and generate events. Components are enabled initially by default.
  • 51. Presentation by Raviraj JTextField  The JTextField class creates a text field which can accept the user input.  Constructors are:  JTextField()  JTextField (String str)  JTextField (int columns)  JTextField (String str, int columns)  The first form creates a default text field which contains no string in it.  The second form str specified the string which will be displayed in the text field
  • 52. Presentation by Raviraj  The columns parameter specifies the width of the text field. this width does not specifies the limit of the number of characters that can be entered in the text field.  The JTextField class generates an ActionEcent when user presses the enter key after input the data in the text field.
  • 53. Presentation by Raviraj Methods of JTextField  public int getColumns()  Returns the number of columns in this TextField.  Returns: the number of columns >= 0  public void setColumns(int columns)  Sets the number of columns in this TextField, and then invalidate the layout.  Parameters: columns - the number of columns >= 0  public void setText(String text)  Set the specified text in this JTextField.  public String getText()  Return the text of this JtextField.
  • 54. Presentation by Raviraj  Demo of JLabel and JTextField class
  • 55. Presentation by Raviraj JButton  JButton is used to display a text as well as an image on it.  The swin buttons are subclass of the AbstractButton class.  Constructors:  public JButton()  Creates a button with no set text or icon  public JButton(Icon icon)  Creates a button with an icon.  Parameters: icon - the Icon image to display on the button  public JButton(String text)  Creates a button with text.  Parameters: text - the text of the button
  • 56. Presentation by Raviraj  public JButton(String text,Icon icon)  Creates a button with initial text and an icon.  Parameters: text - the text of the button  icon - the Icon image to display on the button
  • 57. Presentation by Raviraj  Demo of JButton
  • 58. Presentation by Raviraj JRadioButton class  The JRadioButton is a subclass of the AbstractButton class.itis used to create radio buttons.  An implementation of a radio button -- an item that can be selected or deselected, and which displays its state to the user.  Used with a ButtonGroup object to create a group of buttons in which only one button at a time can be selected. (Create a ButtonGroup object and use its add method to include the JRadioButton objects in the group.)  ButtonGroup bg = new ButtonGroup();  bg.add(b1);  bg.add(b2);  bg.add(b3);
  • 59. Presentation by Raviraj  A ButtonGroup can be used with any set of objects that inherit from AbstractButton. Typically a button group contains instances of JRadioButton.  public ButtonGroup()  Creates a new ButtonGroup.  Void add(AbstractButton obj)  Obj is the object of the JRadioButton to be added.  public int getButtonCount()  Returns the number of buttons in the group.
  • 60. Presentation by Raviraj Constructors JRadioButton  public JRadioButton()  public JRadioButton(Icon icon)  public JRadioButton(Icon icon,boolean selected)  public JRadioButton(String text)  public JRadioButton(String text,boolean selected)  public JRadioButton(String text,Icon icon)  public JRadioButton (String text,Icon icon,boolean selected)
  • 61. Presentation by Raviraj Methods  public String getText()  Returns the text string that the JRadioButton displays.  public void setText(String text)  Defines the single line of text this component will display. If the value of text is null or empty string, nothing is displayed.  public Icon getIcon()  Returns the graphic image that the JRadionButton displays.  public void setIcon(Icon icon)  Defines the icon this component will display. If the value of icon is null, nothing is displayed.  public void setBounds(int x,int y,int width,int height)  public void setVisible(boolean b)  public void setEnabled(boolean b)  public Boolean isSelected()
  • 62. Presentation by Raviraj  Demo of JRadioButton
  • 63. Presentation by Raviraj JTextArea class  JTextArea class is used to create a multi line text input.  This text does not display any scrollbars but you should use scroll panes if need it.  Constructors  public JTextArea()  public JTextArea(String text)  public JTextArea(int rows,int columns)  public JTextArea(String text,int rows,int columns)
  • 64. Presentation by Raviraj Methods of JTextArea  public void setLineWrap(boolean wrap)  Sets the line-wrapping policy of the text area.  public boolean getLineWrap()  Gets the line-wrapping policy of the text area.  public int getLineCount()  Determines the number of lines contained in the area. Returns: the number of lines > 0  public void insert(String str,int pos)  Inserts the specified text at the specified position.  public void append(String str)  Appends the given text to the end of the document
  • 65. Presentation by Raviraj  public void replaceRange(String str,int start,int end)  Replaces text from the indicated start to end position with the new text specified.  public int getRows()  Returns the number of rows in the TextArea.  Returns: the number of rows >= 0  public int getColumns()  Returns the number of columns in the TextArea.  Returns: number of columns >= 0  public void setFont(Font f)  is used to set the specified font.
  • 66. Presentation by Raviraj  Demo of JTextArea class
  • 67. Presentation by Raviraj JPasswordField Class  The JPasswordField creates a text field which display the disks (dots) instead of the actual characters.  Constructors  JPasswordField()  JPasswordField(int length)  JPasswordField(String str)  JPasswordField(String str, int length)
  • 68. Presentation by Raviraj Methods of JPasswordField  public char getEchoChar()  Returns the character to be used for echoing. The default is '*'.  public void setEchoChar(char c)  Sets the echo character for this JPasswordField.  public boolean echoCharIsSet()  Returns true if this JPasswordField has a character set for echoing.  public char[] getPassword()  Returns the text contained in this TextComponent.
  • 69. Presentation by Raviraj  Demo of JPasswordField Class
  • 70. Presentation by Raviraj JCheckBox Class  This class is a subclass of the AbstactButton class.  It is used to create a checkbox.  An implementation of a check box -- an item that can be selected or deselected, and which displays its state to the user.
  • 71. Presentation by Raviraj Constructors  public JCheckBox()  public JCheckBox(Icon icon)  public JCheckBox(Icon icon,boolean selected)  public JCheckBox(String text)  public JCheckBox(String text,boolean selected)  public JCheckBox(String text,Icon icon)
  • 72. Presentation by Raviraj Methods of CheckBox class  public String getText()  Returns the text string that the JCheckBox displays.  public void setText(String text)  Defines the single line of text this component will display. If the value of text is null or empty string, nothing is displayed.  public Icon getIcon()  Returns the graphic image that the JCheckBox displays.  public void setIcon(Icon icon)  Defines the icon this component will display. If the value of icon is null, nothing is displayed.
  • 73. Presentation by Raviraj  public void setBounds(int x,int y,int width,int height)  Moves and resizes this component. The new location of the top-left corner is specified by x and y, and the new size is specified by width and height.  public void setVisible(boolean b)  Shows or hides this Window depending on the value of parameter b.  public void setEnabled(boolean b)  Enables or disables this component, depending on the value of the parameter b. An enabled component can respond to user input and generate events. Components are enabled initially by default.  public Boolean isSelected()  Test for this JCheckBox is selected or not.
  • 74. Presentation by Raviraj  Demo of JCheckBox
  • 75. Presentation by Raviraj JComboBox class  A component that combines a button or editable field and a drop-down list.  The user can select a value from the drop- down list, which appears at the user's request.  If you make the combo box editable, then the combo box includes an editable field into which the user can type a value.
  • 76. Presentation by Raviraj Constructors  public JComboBox()  Creates a JComboBox with a default data model. The default data model is an empty list of objects. Use addItem to add items. By default the first item in the data model becomes selected.  public JComboBox(Object[] items)  Creates a JComboBox that contains the elements in the specified array. By default the first item in the array (and therefore the data model) becomes selected.  public JComboBox(Vector<?> items)  Creates a JComboBox that contains the elements in the specified Vector. By default the first item in the vector (and therefore the data model) becomes selected.
  • 77. Presentation by Raviraj Methods of ComboBox  public void setEditable(boolean aFlag)  Determines whether the JComboBox field is editable. An editable JComboBox allows the user to type into the field or selected an item from the list to initialize the field, after which it can be edited.  public void setMaximumRowCount(int count)  Sets the maximum number of rows the JComboBox displays. If the number of objects in the model is greater than count, the combo box uses a scrollbar.  count - an integer specifying the maximum number of items to display in the list before using a scrollbar.
  • 78. Presentation by Raviraj  public int getMaximumRowCount()  Returns the maximum number of items the combo box can display without a scrollbar.  Returns: an integer specifying the maximum number of items that are displayed in the list before using a scrollbar  public void setSelectedItem(Object anObject)  Sets the selected item in the combo box display area to the object in the argument. If anObject is in the list, the display area shows anObject selected.  public Object getSelectedItem()  Returns the current selected item.  public void setSelectedIndex(int anIndex)  Selects the item at index anIndex.
  • 79. Presentation by Raviraj  int getSelectedIndex()  Returns the first item in the list that matches the given item. The result is not always defined if the JComboBox allows selected items that are not in the list. Returns -1 if there is no selected item or if the user specified an item which is not in the list.  public void addItem(Object anObject)  Adds an item to the item list. This method works only if the JComboBox uses a mutable data model.  public void insertItemAt(Object anObject,int index)  Inserts an item into the item list at a given index. This method works only if the JComboBox uses a mutable data model.  Parameters: anObject - the Object to add to the list  index - an integer specifying the position at which to add the item
  • 80. Presentation by Raviraj  public void removeItem(Object anObject)  Removes an item from the item list. This method works only if the JComboBox uses a mutable data model.  Parameters: anObject - the object to remove from the item list.  public void removeItemAt(int anIndex)  Removes the item at anIndex This method works only if the JComboBox uses a mutable data model.  Parameters: anIndex - an int specifying the index of the item to remove, where 0 indicates the first item in the list
  • 81. Presentation by Raviraj  public void removeAllItems()  Removes all items from the item list.  public void setEnabled(boolean b)  Enables the combo box so that items can be selected.  public int getItemCount()  Returns the number of items in the list.  Returns: an integer equal to the number of items in the list  public Object getItemAt(int index)  Returns the list item at the specified index. If index is out of range (less than zero or greater than or equal to size) it will return null.  Parameters: index - an integer indicating the list position, where the first item starts at zero  Returns: the Object at that list position; or null if out of range
  • 82. Presentation by Raviraj  Demo of JComboBox
  • 83. Presentation by Raviraj JScrollPane Class  A scroll pane is used to display other components or an image in a rectangular area.  This pane have horizontal as well as vertical scroll bars.  Constructors  JScrollPane(Component obj)  JScrollPane(int vertScrollbar,int horScrollbar)  JScrollPane(Component obj,int vertScrollbar,int horScrollbar
  • 84. Presentation by Raviraj Constants in JScrollPane  HORIZONTAL_SCROLLBAR_ALWAYS  HORIZONTAL_SCROLLBAR_AS_NEEDED  VERICAL_SCROLLBAR_ALWAYS  VERTICAL_SCROLLBAR_AS_NEEDED
  • 85. Presentation by Raviraj Methods of JScrollaPane  public int getValue()  Returns the scrollbar's value.  public void setValue(int value)  Sets the scrollbar's value..  public int getMinimum()  Returns the minimum value supported by the scrollbar (usually zero).  public void setMinimum(int minimum)  Sets the model's minimum property.  public int getMaximum()  The maximum value of the scrollbar is maximum - extent.  public void setMaximum(int maximum)  Sets the model's maximum property.  public void setEnabled(boolean x)  Enables the component so that the knob position can be changed. When the disabled, the knob position cannot be changed.
  • 86. Presentation by Raviraj  Demo of JScrollPane  Demo of JScrollPane2
  • 87. Presentation by Raviraj JScrollBar Class  JScrollBar class is used to create vertical and horizontal scrollbars.  Constructors:  JScrollBar()  JScrollBar (int type)  JScrollBar (int type, int value, int extent, int min, int max)
  • 88. Presentation by Raviraj  JScrollBar (int type, int value, int extent, int min, int max)  Here, type specifies that whether the scrollbar is horizontal or vertical.  This is defined by two constants:  JScrollBar.HORIZONTAL, JScollBar.VERTICAL  The value specifies the initial value of the scrollbar slider.  The extent is the extent the value of the slider.  The min and max specifies the range of the scrollbar.
  • 89. Presentation by Raviraj  The scrollbar generates an adjustment event.  The AdjustmentListener interface contains the method adjustmentValueChanged() which is called when scrollbar value is adjusted.  A method of JScrollBar is int getValue().  It returns the value of the scrollbar
  • 90. Presentation by Raviraj  Demo of JScrollBar
  • 91. Presentation by Raviraj JList class  The list class creates a list box which can display more than one items in the combo box at a time.  It lets user to select more than one item from the list.  If the items in the list are too many to display all in the list box, you have to add it in a scroll pane as it does not display it by default.
  • 92. Presentation by Raviraj Constructors for JList  public JList()  Constructs a JList with an empty, read-only, model.  public JList(Vector<?> listData)  Constructs a JList that displays the elements in the specified Vector.  public JList(Object[] listData)  Constructs a JList that displays the elements in the specified array.
  • 93. Presentation by Raviraj Methods of JList  Void setSelectionMode(int selectionMode)  The selection mode can be specified by the following constants defined by the ListSelectionModel.  ListSelectionModel.SINGLE_SELECTION  ListSelectionModel.SINGLE_INTERVAL_SELECTION  ListSelectionModel.MULTIPLE_INTERVAL_SELECTION  public int getVisibleRowCount()  Returns the value of the visibleRowCount property.  public void setVisibleRowCount(int visibleRowCount)  It sets the number of items that can be visible in the list box at once.
  • 94. Presentation by Raviraj  public boolean isSelectedIndex(int index)  Returns true if the specified index is selected, else false.  public int[] getSelectedIndices()  Returns an array of all of the selected indices, in increasing order.  public void setSelectedIndices(int[] indices)  Changes the selection to be the set of indices specified by the given array.  public Object[] getSelectedValues()  Returns an array of all the selected values, in increasing order based on their indices in the list.  public int getSelectedIndex()  Returns the smallest selected cell index; the selection when only a single item is selected in the list.
  • 96. Presentation by Raviraj JFrame Class  The JFrame class creates a frame which is a window.  This window has a border, a title, and buttons for closing, minimizing, maximizing the window.  Constructors  public JFrame()  Constructs a new frame that is initially invisible.  public JFrame (String title)  Creates a new, initially invisible Frame with the specified title.
  • 97. Presentation by Raviraj Methods of JFrame  public void setDefaultCloseOperation(int operation)  Sets the operation that will happen by default when the user initiates a "close" on this frame.  You must specify one of the following choices:  DO_NOTHING_ON_CLOSE  It does nothing when the close button is pressed.  HIDE_ON_CLOSE  It hides the window when you press the close button, but still it remains alive. This is the default operation.  DISPOSE_ON_CLOSE  This hides and disposes the window when you press the close button in title bar.  it removes the window from the screen and removes it from memory also.  EXIT_ON_CLOSE  It exits the whole application on pressing the close button using System.exit(0) method.
  • 98. Presentation by Raviraj  public void setJMenuBar(JMenuBar menubar)  Sets the menubar for this frame.  public void remove(Component comp)  Removes the specified component from the container.  public void setLayout(LayoutManager manager)  Sets the LayoutManager.  public Container getContentPane()  Returns the contentPane object for this frame.  public void setTitle(String title)  Sets the title for this frame to the specified string.  title - the title to be displayed in the frame's border. A null value is treated as an empty string, "".  - public String getTitle()  Gets the title of the frame. The title is displayed in the frame's border.  Returns: the title of this frame, or an empty string ("") if this frame doesn't have a title.
  • 99. Presentation by Raviraj  public void setBounds(int x,int y,int width,int height)  Moves and resizes this component. The new location of the top- left corner is specified by x and y, and the new size is specified by width and height.  Parameters: x - the new x-coordinate of this component  y - the new y-coordinate of this component  width - the new width of this component  height - the new height of this component  public void setVisible (boolean b)  Shows or hides this Window depending on the value of parameter b.  public void setEnabled (boolean b)  Enables or disables this component, depending on the value of the parameter b.  An enabled component can respond to user input and generate events. Components are enabled initially by default.
  • 100. Presentation by Raviraj  Demo of JFrame 1  Demo of JFrame 2
  • 101. Presentation by Raviraj JMenu, JMenuBar and JMenuItem  These classes provide functionalities to create a menu bar which contains menus.  These menus in turn have menu items.  The menu bar is created using JMenubar class.  Menu is created using JMenu class and the JMenuItem creates menu items.
  • 102. Presentation by Raviraj Steps for creating menu 1. Create a JMenuBar object. 2. Then set the menu bar using setMenuBar() method. 3. Create menu objects of JMenu class. 4. Create menu item odjects using JMenuItem class. 5. Add the menu items to the particular menus. 6. Add the menus to the menu bar.
  • 103. Presentation by Raviraj JMenuBar Class  JMenuBar class creates a menu bar which contains the menus.  Constructor  public JMenuBar()  Creates a new menu bar.
  • 104. Presentation by Raviraj Methods of JMenuBar class  public JMenu add (JMenu c)  Appends the specified menu to the end of the menu bar.  Parameters: c - the JMenu component to add  Returns: the menu component  public int getMenuCount()  Returns the number of items in the menu bar.  Returns: the number of items in the menu bar.
  • 105. Presentation by Raviraj JMenu Class  An implementation of a menu -- a popup window containing JMenuItems that is displayed when the user selects an item on the JMenuBar.  The JMenu class is a container of menus.  Constructors  public JMenu()  Constructs a new JMenu with no text.  public JMenu (String s)  Constructs a new JMenu with the supplied string as its text.  Parameters: s - the text for the menu label
  • 106. Presentation by Raviraj Methods of JMenu Class  public boolean isSelected()  Returns true if the menu is currently selected (highlighted).  public void setSelected (boolean b)  Sets the selection status of the menu  public JMenuItem add (JMenuItem menuItem)  Appends a menu item to the end of this menu. Returns the menu item added.  Parameters: menuItem - the JMenuitem to be added  Returns: the JMenuItem added
  • 107. Presentation by Raviraj  public void addSeparator()  Appends a new separator to the end of the menu.  public int getItemCount()  Returns the number of items on the menu, including separators.  public void remove (JMenuItem item)  Removes the specified menu item from this menu. If there is no popup menu, this method will have no effect.  Parameters: item - the JMenuItem to be removed from the menu  public void removeAll()  Removes all menu items from this menu.
  • 108. Presentation by Raviraj JMenuItem Class  The JMenuItem class is used to create menu items.these menu items will be added to the menu.  An implementation of an item in a menu. A menu item is essentially a button sitting in a list.  When the user selects the "button", the action associated with the menu item is performed.
  • 109. Presentation by Raviraj Constructors of JMenuItem  public JMenuItem()  Creates a JMenuItem with no set text or icon.  public JMenuItem(String text)  Creates a JMenuItem with the specified text.  Parameters: text - the text of the JMenuItem  public JMenuItem (Icon icn)  public JMenuItem (String txt, Icon icr)
  • 110. Presentation by Raviraj  Demo of JMenu  Demo of JMenu 2