0% found this document useful (0 votes)
40 views

2.introduction To Swing

The document introduces Java Swing and its components, explaining that Swing provides platform-independent GUI components that follow the MVC architecture. It describes common Swing components like JLabel, buttons, text fields, and discusses how to add components to containers and close windows. The document also outlines the Swing class hierarchy and differences between AWT and Swing.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

2.introduction To Swing

The document introduces Java Swing and its components, explaining that Swing provides platform-independent GUI components that follow the MVC architecture. It describes common Swing components like JLabel, buttons, text fields, and discusses how to add components to containers and close windows. The document also outlines the Swing class hierarchy and differences between AWT and Swing.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 102

INTRODUCTION TO SWING

Mr.Ghanawajeer D.J.

1
POINTS TO BE COVERED
 What is Swing?

 Swing Features & MVC Architecture

 Java Swing Components:

JApplet Tabbed Panes


Icons & Label Scroll Panes
JButton ProgressBar
JTextField Tool Tips
JPassword Seperator
JCheckbox Tables
JRadioButton Trees
2
Combo Boxes Toggle Button
SWING
 In Java Swing is a part of Java Foundation Classes
(JFC) that is used to create window-based applications.
It is built on the top of AWT (Abstract Windowing
Toolkit) API and entirely written in java.
 Unlike AWT, Java Swing provides platform-
independent and lightweight components.
 The javax.swing package provides classes for java
swing such as JButton, JTextField, JTextArea,
3
JRadioButton, JCheckbox, JMenu, JColorChooser etc.
SWING
 Earlier, the concept of Swing did not exist in Java and
the user interfaces were built by using the Java's
original GUI system, AWT.

 Because of the limitations of the AWT, Swing was


introduced in 1997 by the Sun Microsystems. It
provides new and improved components that enhance
the look and functionality of GUIs.

4
DIFFERENCE BETWEEN AWT AND
SWING
AWT SWING
AWT components are platform- Java swing components are
dependent. platform-independent.
AWT components are heavyweight. Swing components are lightweight.
AWT doesn't support pluggable look Swing supports pluggable look and
and feel. feel.
AWT provides less components than Swing provides more powerful
Swing. components such as Tables, Lists,
ScrollPanes, Trees,TabbedPanes etc.
AWT doesn't follows MVC(Model Swing follows MVC.
View Controller) where model
represents data, view represents
presentation and controller acts as an 5
interface between model and view.
FEATURES OF SWING
 Light Weight − Swing components are independent of
native Operating System's API as Swing API controls
are rendered mostly using pure JAVA code instead of
underlying operating system calls.

 Rich Controls − Swing provides a rich set of advanced


controls like Tree, TabbedPane, slider, colorpicker, and
table controls.

6
FEATURES OF SWING
 Highly Customizable − Swing controls can be
customized in a very easy way as visual appearance is
independent of internal representation.

 Pluggable look-and-feel − SWING based GUI


Application look and feel can be changed at run-time,
based on available values.

7
MVC ARCHITECTURE
 The Model-View-Controller Architecture:

 Swing uses the model-view-controller


architecture (MVC) as the fundamental design
behind each of its components.

 Essentially, MVC breaks GUI components into three


elements. Each of these elements plays a crucial role
in how the component behaves.
8
MODEL
 The model encompasses the state data for each
component. There are different models for different
types of components.

 For example, the model of a scrollbar component


might contain information about the current position
of its adjustable “thumb,” its minimum and
maximum values, and the thumb’s width (relative to
the range of values). 9
VIEW
 The view refers to how you see the component on the
screen. For a good example of how views can differ, look at
an application window on two different GUI platforms.
Almost all window frames will have a titlebar spanning the
top of the window. However, the titlebar may have a close
box on the left side (like the older MacOS platform), or it
may have the close box on the right side (as in the Windows
95 platform). These are examples of different types of views
10
for the same window object.
CONTROLLER
 The controller is the portion of the user interface that

dictates how the component interacts with events.

Events come in many forms — a mouse click, gaining

or losing focus, a keyboard event that triggers a

specific menu command, or even a directive to repaint

part of the screen. The controller decides how each

component will react to the event—if it reacts at all.


11
MVC ARCHITECTURE

This shows how the model, view, and controller work


together to create a scrollbar component. 12
MVC ARCHITECTURE
MVC Interaction:
With MVC, each of the three elements—the model, the
view, and the controller—requires the services of another
element to keep itself continually updated.

13
MVC ARCHITECTURE
MVC in Swing:
Swing actually makes use of a simplified variant of the
MVC design called the model-delegate . This design
combines the view and the controller object into a single
element that draws the component to the screen and
handles GUI events known as the UI delegate.

14
CONTAINERS
 Swing defines two types of containers.
 The first are top-level containers: JFrame, JApplet,
JWindow, and JDialog. These containers do not
inherit JComponent. They do, however, inherit the
AWT classes Component and Container.
 The second type of containers supported by Swing are
lightweight containers.
 Lightweight containers do inherit JComponent. An
example of a lightweight container is JPanel, which is15
a general-purpose container.
THE TOP-LEVEL CONTAINER PANES
 Each top-level container defines a set of panes.
At the top of the hierarchy is an instance of
JRootPane.

 JRootPane is a lightweight container whose


purpose is to manage the other panes.

 The panes that comprise the root pane are


called the glass pane, the content pane, and the16
layered pane.
THE TOP-LEVEL CONTAINER PANES
 The glass pane is the top-level pane. It sits
above and completely covers all other panes. By
default, it is a transparent instance of JPanel.

 The pane with which your application will


interact the most is the content pane, because
this is the pane to which you will add visual
components.
17
THE TOP-LEVEL CONTAINER PANES
 The layered pane is an instance of
JLayeredPane. The layered pane allows
components to be given a depth value.

18
THE TOP-LEVEL CONTAINER PANES

19
ADDING COMPONENTS
 All top-level containers have a content pane in which
components are stored. Thus, to add a component to a
frame or applet, you must add it to the frame’s content
pane or applet’s content pane. This is accomplished by
calling add( );
Syntax:
Component add(Component comp)
 The content pane can be obtained by calling
getContentPane( ) on a JFrame or JApplet instance.
Syntax:
Container getContentPane( )
 It returns a Container reference to the content pane. 20
CLOSING WINDOW
 You will usually want the entire application to terminate when its

top-level window is closed. The easiest way is to call

setDefaultCloseOperation( ).

jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Syntax: void setDefaultCloseOperation(int what)

 The value passed in what determines what happens when the

window is closed.

 They are shown here:

 JFrame.DISPOSE_ON_CLOSE
21
 JFrame.HIDE_ON_CLOSE

 JFrame.DO_NOTHING_ON_CLOSE
SWING COMPONENT:
CLASS HIERARCHY

22
JFrame
SWING COMPONENTS

23
JLABEL AND IMAGEICON
 JLabel is Swing’s easiest-to-use component.

 JLabel can be used to display text and/or an icon. It


is a passive component in that it does not respond to
user input.

 Constructors:
 JLabel(Icon icon)

 JLabel(String str)

 JLabel(String str, Icon icon, int align)


24
JLABEL AND IMAGEICON
 Here, str and icon are the text and icon used for the
label. The align argument specifies the horizontal
alignment of the text and/or icon within the
dimensions of the label.

 It must be one of the following values: LEFT,


RIGHT, CENTER, LEADING, or TRAILING.

 These constants are defined in the SwingConstants


interface, along with several others used by the
25

Swing classes.
JLABEL AND IMAGEICON
 The easiest way to obtain an icon is to use the
ImageIcon class.

 ImageIcon implements Icon and encapsulates an


image. Thus, an object of type ImageIcon can be
passed as an argument to the Icon parameter of
JLabel’s constructor.

 The ImageIcon Constructor:

ImageIcon(String filename) 26

 It obtains the image in the file named filename.


JLABEL AND IMAGEICON
 The icon and text associated with the label can be
obtained by the following methods:
Icon getIcon( )

String getText( )

 The icon and text associated with a label can be set by


these methods:
void setIcon(Icon icon)

void setText(String str)


27
JLABEL AND IMAGEICON

ImageIcon icon = new ImageIcon("india.png");


JLabel jl = new JLabel("Indian Flag",icon,JLabel.CENTER);
28
JTEXTFIELD
 JTextField is the simplest Swing text component. It is
also probably its most widely used text component.

 JTextField allows you to edit one line of text. It is


derived from JTextComponent, which provides the
basic functionality common to Swing text components.

 Constructors:
 JTextField(int cols)
29
 JTextField(String str, int cols)

 JTextField(String str)
JTEXTFIELD
 Here, str is the string to be initially presented, and cols is
the number of columns in the text field. If no string is
specified, the text field is initially empty. If the number
of columns is not specified, the text field is sized to fit
the specified string.

 JTextField generates events in response to user


interaction. For example, an ActionEvent is fired
when the user presses ENTER.
30
JTEXTFIELD
 To obtain the text currently in the text field, call
getText( ).
 String getText()

 To set the text in the text field use setText().


 void setText(String str)

 To set the number of columns in the text filed, call


setColumn().
 void setColumns(int columns)
31
JTEXTFIELD

32
JPASSWORDFIELD
 The object of a JPasswordField class is a text component
specialized for password entry.

 It allows the editing of a single line of text. It inherits


JTextField class.

 Constructors:

 JPasswordField()

 JPasswordField(int columns)

 JPasswordField(String text) 33

 JPasswordField(String text, int columns)


JPASSWORDFIELD
 It is having the same methods as the JTextField.

 char getEchoChar()-Returns the character to be used


for echoing.

 void setEchoChar(char c)- Sets the echo character for


this JPasswordField.

34
JTEXTAREA
 The JTextArea class is used to create a text area. It is a
multiline area that displays the plain text only.
 Constructors:

 JTextArea(): creates a text area that displays no


text initially.
 JTextArea(String s): creates a text area that
displays specified text initially.
 JTextArea(int row, int column): creates a text
area with the specified number of rows and columns
that displays no text initially..
 JTextArea(String s, int row, int column): creates
a text area with the specified number of rows and 35
columns that displays specified text.
JTEXTAREA
 Methods:
 public void setRows(int rows): is used to set
specified number of rows.
 public void setColumns(int cols):: is used to set
specified number of columns.
 public void setFont(Font f): is used to set the
specified font.
 public void insert(String s, int position): is used
to insert the specified text on the specified position.
 public void append(String s): is used to append
the given text to the end of the document.
36
JTEXTAREA

37
JCOMBOBOX
 Swing provides a combo box (a combination of a text field
and a drop-down list) through the JComboBox class.

 A combo box normally displays one entry, but it will also


display a drop-down list that allows a user to select a
different entry.

 Constructors:

 JComboBox()

 JComboBox(Object[] items) 38

 JComboBox(Vector items)
JCOMBOBOX
 Methods:

 public void addItem(Object anObject): is used to add an


item to the item list.

 public void removeItem(Object anObject): is used to


delete an item to the item list.

 public void removeAllItems(): is used to remove all the


items from the list.

 public void setEditable(boolean b): is used to determine


39

whether the JComboBox is editable.


JCOMBOBOX
 Methods:

 Object getSelectedItem( ): is used to obtain the item


selected in the list.

 public void addActionListener(ActionListener a): is used


to add the ActionListener.

 public void addItemListener(ItemListener i): is used to


add the ItemListener.

40
JCOMBOBOX
 JComboBox generates an action event when the user
selects an item from the list.

 JComboBox also generates an item event when the


state of selection changes, which occurs when an item is
selected or deselected.

41
JCOMBOBOX EXAMPLE

42
THE SWING BUTTONS
 Swing defines four types of buttons: JButton,
JToggleButton, JCheckBox, and JRadioButton.
 All are subclasses of the AbstractButton class, which
extends JComponent. Thus, all buttons share a set of
common traits.
 AbstractButton contains many methods that allow
you to control the behavior of buttons.
 For example, you can define different icons that are
displayed for the button when it is disabled, pressed,
or selected. Another icon can be used as a rollover icon,
which is displayed when the mouse is positioned over a
button. 43
THE SWING BUTTONS

44
ABSTRACTBUTTON COMMON METHODS

 The following methods set these icons:

 void setDisabledIcon(Icon di)

 void setPressedIcon(Icon pi)

 void setSelectedIcon(Icon si)

 void setRolloverIcon(Icon ri)

 Here, di, pi, si, and ri are the icons to be used for the
indicated purpose.
45
ABSTRACTBUTTON COMMON METHODS

 public void setText(String s): is used to set specified


text on button.

 public String getText(): is used to return the text of the


button.

 public void setEnabled(boolean b): is used to enable or


disable the button.

 public void setIcon(Icon b): is used to set the specified


Icon on the button. 46
ABSTRACTBUTTON COMMON METHODS

 public Icon getIcon(): is used to get the Icon of the


button.

 public void setMnemonic(int a): is used to set the


mnemonic on the button.

 public void addActionListener(ActionListener a): is


used to add the action listener to this object.

47
JBUTTON
 The JButton class is used to create a button that have
platform-independent implementation.

 Constructors:

 JButton(): creates a button with no text and icon.

 JButton(String str): creates a button with the


specified text.

 JButton(Icon icon): creates a button with the


specified icon object.
48
 JButton(String str, Icon icon):Here, str and icon are
the string and icon used for the button.
JBUTTON
 When the button is pressed, an ActionEvent is
generated. Using the ActionEvent object passed to the
actionPerformed( ) method of the registered
ActionListener.

 You can obtain the action command string associated


with the button.

 However, you can set the action command by calling


setActionCommand( ) on the button.

 You can obtain the action command by calling 49

getActionCommand( ) on the event object.


JBUTTON EXAMPLE

50
JTOGGLEBUTTON
 A useful variation on the push button is called a toggle
button. A toggle button looks just like a push button,
but it acts differently because it has two states: pushed
and released.
 That is, when you press a toggle button, it stays
pressed rather than popping back up as a regular push
button does.
 When you press the toggle button a second time, it
releases (pops up).
 Therefore, each time a toggle button is pushed, it
toggles between its two states.
51
JTOGGLEBUTTON
 JToggleButton implements AbstractButton.

 JToggleButton is a superclass for two other Swing


components that also represent two-state controls.
These are JCheckBox and JRadioButton.

 Constructors:

 JToggleButton()-It creates an initially unselected


toggle button without setting the text or image.

 JToggleButton(Icon icon)-It creates an initially


52
unselected toggle button with the specified image but
no text.
JTOGGLEBUTTON
 JToggleButton(Icon icon, boolean selected)-It
creates a toggle button with the specified image and
selection state, but no text.

 JToggleButton(String text)-It creates an unselected


toggle button with the specified text.

 JToggleButton(String text, boolean selected)-It


creates a toggle button with the specified text and
selection state.
53
JTOGGLEBUTTON
 JToggleButton(String text, Icon icon)-It creates a
toggle button that has the specified text and image,
and that is initially unselected.

 JToggleButton(String text, Icon icon, boolean


selected)-It creates a toggle button with the specified
text, image, and selection state.

54
JTOGGLEBUTTON
 Like JButton, JToggleButton generates an action
event each time it is pressed.

 Unlike JButton, however, JToggleButton also


generates an item event.

 Inside itemStateChanged( ), the getItem( ) method


can be called on the ItemEvent object to obtain a
reference to the JToggleButton instance that
generated the event.
55
Object getItem( )
JTOGGLEBUTTON
 The easiest way to determine a toggle button’s state is
by calling the isSelected( ) method (inherited from
AbstractButton) on the button that generated the
event.
boolean isSelected( )

 It returns true if the button is selected and false


otherwise.

56
JTOGGLEBUTTON EXAMPLE

57
JCHECKBOX
 The JCheckBox class is used to create a checkbox. It is
used to turn an option on (true) or off (false). Clicking on
a CheckBox changes its state from "on" to "off" or from
"off" to "on ".

 The JCheckBox class provides the functionality of a


check box. Its immediate superclass is JToggleButton.

58
JCHECKBOX
 Constructors:

 JCheckBox()-Creates an initially unselected check


box button with no text, no icon.

 JChechBox(String s)-Creates an initially


unselected check box with text.

 JCheckBox(String text, boolean selected)-


Creates a check box with text and specifies whether
or not it is initially selected.
59
JCHECKBOX
 JCheckBox(Icon icon)-Creates an initially
unselected check box with an icon.

 JCheckBox(Icon icon, boolean selected)-Creates


a check box with an icon and specifies whether or not
it is initially selected.

 JCheckBox(String text, Icon icon)-Creates an


initially unselected check box with the specified text
and icon.
60
JCHECKBOX
 JCheckBox(String text, Icon icon, boolean
selected)-Creates a check box with text and icon, and
specifies whether or not it is initially selected.

61
JCHECKBOX
 When the user selects or deselects a check box, an
ItemEvent is generated.

 You can obtain a reference to the JCheckBox that


generated the event by calling getItem( ) on the
ItemEvent passed to the itemStateChanged( )
method defined by ItemListener.

 The easiest way to determine the selected state of a


check box is to call isSelected( ) on the JCheckBox
62
instance.
JCHECKBOX
 Methods:

 AccessibleContext getAccessibleContext()-It is
used to get the Accessible Context associated with
this JCheckBox.

 protected String paramString()-It returns a string


representation of this JCheckBox.

63
JCHECKBOX EXAMPLE

64
JRADIOBUTTON
 The JRadioButton class is used to create a radio
button.

 It is used to choose one option from multiple options. It


is widely used in exam systems or quiz.

 It should be added in ButtonGroup to select one radio


button only.

 Radio buttons are a group of mutually exclusive


buttons, in which only one button can be selected at
65

any one time.


JRADIOBUTTON
 They are supported by the JRadioButton class, which
extends JToggleButton.

 In order for their mutually exclusive nature to be


activated, radio buttons must be configured into a
group. Only one of the buttons in the group can be
selected at any time.

66
JRADIOBUTTON
 Constructors:

 JRadioButton(): creates an unselected radio


button with no text.

 JRadioButton(String text): creates an unselected


radio button with specified text.

 JRadioButton(String text, boolean


selected): creates a radio button with the specified
text and selected status.
67
JRADIOBUTTON
 Constructors:

 JRadioButton(Icon icon):Creates an initially


unselected radio button with the specified image but
no text

 JRadioButton(Icon icon, boolean


selected):Creates a radio button with the specified
image and selection state, but no text.

68
JRADIOBUTTON
 Constructors:

 JRadioButton(String text, Icon icon):Creates a


radio button that has the specified text and image,
and which is initially unselected.

 JRadioButton(String text, Icon icon, boolean


selected):Creates a radio button that has the
specified text, image, and selection state.

69
BUTTONGROUP
 The ButtonGroup class can be used to group multiple
buttons so that at a time only one button can be
selected.

 Its default constructor is invoked for this purpose.


Elements are then added to the button group using
add();
void add(AbstractButton ab)

70
JRADIOBUTTON
 A JRadioButton generates action events, item events,
and change events each time the button selection
changes.

 Most often, it is the action event that is handled, which


means that you will normally implement the
ActionListener interface.

 Recall that the only method defined by ActionListener


is actionPerformed( ).
71
JRADIOBUTTON EXAMPLE

72
JPROGRESSBAR
 The JProgressBar class is used to display the progress
of the task.

 Constructors:

 JProgressBar(): is used to create a horizontal


progress bar but no string text.

 JProgressBar(int min, int max): is used to create a


horizontal progress bar with the specified minimum
and maximum value.
73
JPROGRESSBAR
 JProgressBar(int orient): is used to create a
progress bar with the specified orientation, it can be
either Vertical or Horizontal by using
SwingConstants.VERTICAL and
SwingConstants.HORIZONTAL constants.

 JProgressBar(int orient, int min, int max): is


used to create aprogress bar with the specified
orientation, minimum and maximum value.
74
JPROGRESSBAR
 Methods:

 public void setStringPainted(boolean b): is used to


determine whether string should be displayed.

 public void setString(String s): is used to set value to


the progress string.

 public void setOrientation(int orientation): is used


to set the orientation, it may be either vertical or
horizontal by using SwingConstants.VERTICAL and
75
SwingConstants.HORIZONTAL constants..
JPROGRESSBAR
 public void setValue(int value): is used to set the
current value on the progress bar.

76
JSCROLLPANE
 JScrollPane is a lightweight container that automatically
handles the scrolling of another component.

 The component being scrolled can either be an individual


component, such as a JTable, or a group of components
contained within another lightweight container, such as a
JPanel.

 In either case, if the object being scrolled is larger than the


viewable area, horizontal and/or vertical scroll bars are
automatically provided, and the component can be scrolled
77
through the pane.
JSCROLLPANE
 The viewable area of a scroll pane is called the viewport.
It is a window in which the component being scrolled is
displayed.

 Constructors:

 JScrollPane()

 JScrollPane(Component comp)

 JScrollPane(int vsb, int hsb)

 JScrollPane(Component comp, int vsb, int hsb)


78
JSCROLLPANE
 Here comp is the Component to be added to
scroll pane, vsb & hsb are the constants that
define when vertical & horizontal scrollbars for
this scroll pane are shown.

 These constants are defined by


ScrollPaneConstants interface.

79
JSCROLLPANE
 vsb defines:
 ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS

 ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEED
ED

 hsb defines:
 ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWA
YS

 ScrollPaneConstants.
HORIZONTAL_SCROLLBAR_AS_NEEDED
80
JSCROLLPANE EXAMPLE

81
JTABLE
 The JTable class is used to display the data on two
dimensional in tabular form.
 It is composed of rows and columns.
 You can drag the cursor on column boundaries to resize
columns.
 Constructors:

 JTable(): creates a table with empty cells.

 JTable(Object[][] data, Object[] colHeads):creates


a table with the specified data. 82
JTABLE
 A one-dimensional array of strings called colHeads is
created for the column headings.

 A two-dimensional array of strings called data is created


for the table cells.

 steps required to set up a simple JTable that can be


used to display data:
1. Create an instance of JTable.
2. Create a JScrollPane object, specifying the table as the
object to scroll.
3. Add the table to the scroll pane. 83
4. Add the scroll pane to the content pane.
JTABLE EXAMPLE

84
JTABBEDPANE
 JTabbedPane encapsulates a tabbed pane.

 It manages a set of components by linking them with


tabs. Selecting a tab causes the component associated
with that tab to come to the forefront.

 Constructors:

 JTabbedPane()-Creates an empty TabbedPane with a


default tab placement of JTabbedPane.Top.

 JTabbedPane(int tabPlacement)-Creates an empty


85

TabbedPane with a specified tab placement.


JTABBEDPANE
 The placement is done when the direction is specified
using JTabbedPane.TOP, JTabbedPane.BOTTOM,
JTabbedPane.LEFT, or JTabbedPane.RIGHT.

 JTabbedPane(int tabPlacement, int tabLayoutPolicy)-


Creates an empty TabbedPane with a specified tab
placement and tab layout policy.

 It also lets the programmer decide the tab layout policy.


This allows the programmer to control how the tabs will
86
be displayed.
JTABBEDPANE
 The tab layout policy is either set to
JTabbedPane.SCROLL_TAB_LAYOUT or
JTabbedPane.WRAP_TAB_LAYOUT.

 By default, the policy is set to WRAP_TAB_LAYOUT.

 With the policy set to SCROLL_TAB_LAYOUT, the tabs


become scrollable and a button for scrolling the tabs, left-
right or up-down, is displayed in your tabbed pane.

87
JTABBEDPANE
 Tabs are added by calling addTab( ).

void addTab(String name, Component comp)

 Here, name is the name for the tab, and comp is the
component that should be added to the tab.

 Often, the component added to a tab is a JPanel that


contains a group of related components.
 The general procedure to use a tabbed pane is:
1. Create an instance of JTabbedPane.
2. Add each tab by calling addTab( ). 88

3. Add the tabbed pane to the content pane.


JTABBEDPANE EXAMPLE

89
JTREE
 A tree is a component that presents a hierarchical
view of data.

 A user has the ability to expand or collapse individual


sub-trees in this display.

 Trees are implemented in Swing by the JTree class,


which extends JComponent.

 JTree is a complex component. It has a 'root node' at


the top most which is a parent for all nodes in the
tree.
JTREE
 Constructors:
 JTree(Hashtable ht)
 JTree(Object obj[ ])
 JTree(TreeNode tn)
 JTree(Vector v)
 The first form creates a tree in which each element of
the hash table ht is a child node.
 Each element of the array obj is a child node in the
second form.
 The tree node tn is the root of the tree in the third
form.
 Finally, the last form uses the elements of vector v as
child nodes
JTREE
 The TreeNode interface declares methods that obtain
information about a tree node.
 For example, it is possible to obtain a reference to the
parent node or an enumeration of the child nodes.
 The MutableTreeNode interface extends TreeNode.
 It declares methods that can insert and remove child
nodes or change the parent node.
 The DefaultMutableTreeNode class implements the
MutableTreeNode interface. It represents a node in a
tree.
JTREE
 Constructor:
DefaultMutableTreeNode(Object obj)
 Here, obj is the object to be enclosed in this tree node.
 The new tree node doesn’t have a parent or children.
 To create a hierarchy of tree nodes, the add( ) method of
DefaultMutableTreeNode can be used.
void add(MutableTreeNode child)
 Here, child is a mutable tree node that is to be added as
a child to the current node.
JTREE
 The getPathForLocation( ) method is used to translate a
mouse click on a specific point of the tree to a tree path.
TreePath getPathForLocation(int x, int y)
 Here, x and y are the coordinates at which the mouse is
clicked.
 The return value is a TreePath object that
encapsulates information about the tree node that was
selected by the user.
 The TreePath class encapsulates information about a
path to a particular node in a tree. It provides several
constructors and methods.
JTREE
 Tree expansion events are described by the class
TreeExpansionEvent in the javax.swing.event
package.
 The getPath( ) method of this class returns a TreePath
object that describes the path to the changed node.
TreePath getPath( )
 The TreeExpansionListener interface provides the
following two methods:
void treeCollapsed(TreeExpansionEvent tee)
void treeExpanded(TreeExpansionEvent tee)
 The first method is called when a sub-tree is hidden,
and the second method is called when a sub-tree
becomes visible.
JTREE
 A JTree object generates events when a node is
expanded or collapsed.
 The addTreeExpansionListener( ) and
removeTreeExpansionListener( ) methods allow
listeners to register and unregister for these
notifications.
 void addTreeExpansionListener(TreeExpansionListener tel)

 void removeTreeExpansionListener(TreeExpansionListener tel)

 Here, tel is the listener object.


JTREE EXAMPLE
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.
 It inherits JComponent class.
 Constructors:
 JSeparator()-Creates a new horizontal separator.
 JSeparator(int orientation)-Creates a new separator
with the specified horizontal or vertical orientation.
 SwingConstants.VERTICAL
 SwingConstants.HORIZONTAL
JSEPARATOR
 Methods:
 void setOrientation(int orientation)-It is used to
set the orientation of the separator.
 int getOrientation()-It is used to return the
orientation of the separator.
 void addSeparator() – It adds a separator in JMenu
or JPopupMenu.
JSEPARATOR EXAMPLE
TOOLTIP
 A tooltip is a context-sensitive text string that is
displayed in a popup window when the mouse rests over
a particular object on the screen.

 Swing provides the JToolTip class to support this;


however, you will rarely use it directly.

 To create a tooltip, you only need to call the


setToolTipText() method of JComponent.

 This method is used to set up a tool tip for the


component.
TOOLTIP EXAMPLE

You might also like