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
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.
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.
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.
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);
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.
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)
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.
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
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()
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.
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.
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.
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
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.
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
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.
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)