Unit I
Unit I
Container
Window Panel
Frame Applet
Component
• All user interface elements that are displayed on the screen and that
interact with the user are subclasses of Component.
• Responsible for managing events, such as mouse and keyboard input,
positioning and sizing the window, and repainting.
Container
• subclass of Component
• Responsible for laying out(i.e. positioning) any components that it
contain.
• It does this through the use of various layout managers.
Panel
• Concrete subclass of Container.
• No new methods; it simply implements Container.
• Panel is the superclass for Applet.
• Panel is a window that does not contain a title bar, menu bar or
border. (AppletViewer provides this; browser won’t)
• Components can be added to a Panel object by its add( )
method.
• Once added, they can be positioned and resized manually using the
setLocation( ), setSize( ), or setBounds( ) methods defined by
Component.
Window
• creates top-level window.
• Window objects generally are not created directly; instead we will use a
subclass of Window called Frame.
Frame
• subclass of Window and has a title bar, menu bar, border and resizing
corners.
• Frame object can be created from within an applet window
• it will contain a message “Java Applet Window”
• When a Frame window is created by a program ; a normal window is
created.
Canvas
• not part of hierarchy.
• Canvas encapsulates a blank window upon which you can draw.
Commonly Used Methods of Component Class
• setBackground(): used to change the background color of a component.
public void setBackground ( Color c)
Note: color is a helper class.
Methods:
first() / last()/ next()/ previous(): is used to make the first/
last/ next/ previous card visible.
• Dialog boxes are primarily used to obtain user input and are
often child windows of a top-level window.
• Dialog boxes don’t have menu bars, but in other respects,
they function like frame windows.
• You can add controls to them, for example, in the same way
that you add controls to a frame window.
• Dialog boxes are of type Dialog
• Dialog(Frame parentWindow, boolean mode)
• Dialog(Frame parentWindow, String title, boolean mode)
• parentWindow is the owner of the dialog box.
Continue..
• Dialog boxes may be modal or modeless.
• When a modal dialog box is active, all input is directed to it
until it is closed. This means that you cannot access other
parts of your program until you have closed the dialog box.
• When a modeless dialog box is active, input focus can be
directed to another window in your program.
• Thus, other parts of your program remain active and
accessible.
Example
Methods Description
void addItem(Object anObject) It is used to add an item to the item list.
void removeItem(Object anObject) It is used to delete an item to the item list.
void removeAllItems() It is used to remove all the items from the list.
Methods Description
int getSelectedIndex() It is used to return the smallest selected cell index.
ListModel getModel() It is used to return the data model that holds a list of items
displayed by the JList component.
The JTabbedPane class in Java Swing has a default, no-argument constructor called
JTabbedPane() JTabbedPane(). This constructor initializes an empty tabbed pane with no tabs and no initial
content when a JTabbedPane is created.
Constructors Descriptions
This constructor creates an empty JScrollPane with the specified vertical and
JScrollPane(int vertical, int horizontal) horizontal scrollbar.
JScrollPane(LayoutManager layout) This constructor creates a JScrollPane with the specified layout manager.
Methods Description
void setColumnHeaderView(Component comp) sets the column header for the JScrollPane
frame.add(scrollPane);
frame.setVisible(true);
}
}
JTree
• The JTree class is used to display the tree structured data or
hierarchical data. JTree is a complex component.
• It has a 'root node' at the top most which is a parent for all nodes in
the tree. It inherits JComponent class.
Constructor Description
This constructor creates a JTree with a sample model. It serves as a quick way to
JTree() initialize a tree structure without specifying a custom model.
JTree is created with each element of the specified array becoming a child of a new
JTree(Object[] value) root node. This constructor is useful when you want to build a tree structure based on
an array of values.
JTree is created with the specified TreeNode as its root. This allows you to define a
JTree(TreeNode root) custom structure for your tree by providing a root node explicitly.
Constructor Description
JMenuBar
JMenu
JMenuItem
JSeparator
• The object of JSeparator class is used to provide a general purpose
component for implementing divider lines.
• It is used to draw a line to separate widgets in a Layout.
menu.addSeparator();
JSeparator() Creates a new horizontal separator.
JSeparator(int orientation) Creates a new separator with the specified horizontal or vertical orientation.
method explanation
setOrientation(int o) Sets the orientation of the separator.