SlideShare uma empresa Scribd logo
1 de 12
Baixar para ler offline
TREES –

Many tree structures arise in everyday life,such as the
hierarchy
of countries ,states & cities.The Swing library has a JTree
class for this purpose.The JTree class takes care of laying
out the tree and processing user requests for expanding
and collapsing
nodes. A tree is composed of nodes.Every node is either a
leaf or it has child nodes.Everynode,with the exception of
the root node, has exactly one parent.A tree has exactly
one root node. Sometimes you have a collection of trees,
each of which has its own root node.Such a collection is
called a forest.

           (draw diagram on page no 358 Fig 6.7 )

Simple Trees -

The JTree component follows the model view-controller
pattern. You provide a model of the hierarchical data, and
the component
displays it for you. To construct a JTree, you supply the
tree model in the constructor :
      TreeModel model=. . . ;
      JTreee tree = new JTree(model) ;
To construct a default tree model, you must supply a root
node.
   TreeNode root = . . . ;
   DefaultTreeModel model=
newDefaultTreeModel(root) ;

TreeNode is another interface.
you can simply pass the root node to the JTree
constructor.
Then the tree automatically constructs a default tree
model :
   JTree tree =new JTree(root) ;

Editing Trees and Tree Paths -
The JTree class has a surprising way of identifying nodes in
a tree. It doesnot deal with tree nodes, but with paths of
objects,called tree paths. A tree path starts at the root &
consists of a sequence of child nodes.

           (draw diagram on page no 366 Fig 6-18)

The JTree class knows nothing about the TreeNode
interface.That interace is never used by the TreeModel
interface.it is only used by the DefaultTreeModel
implementation.You can have other tree models in which
the nodes do not implement the TreeNode interface at
all.If you use a tree model that manages other types of
objects, then those objects may not have getParent &
getChild methods.
The TreePath class manages a sequence of Object
references.
A number of JTree methods return Treepath objects.When
you have a tree path,you usually just need to Know
the terminal node,which you get with the
getLastPathComponent method.For eg, to find out the
currently selected node in a tree, you use the
getSelectionPath method of the JTree
class.You get a TreePath object back, from which you can
retrieve the actual node.
   TreePath selectionPath= tree.getSelectionPath() ;
   DefaultMutableTreeNode selectedNode =
  (DefaultMutableTreeNode)
  selectionPath.getLastPathComponent() ;

If you use the insertNodeInto method of the
DefaultTreeModel class, the model class takes care of that.
For eg, the
following call appends a new node as the last child of the
selected node & notifies the tree view.
model.insertNodelnto(newNode, selectedNode ,
selectedNode.getchildCount()) ;
The analogous call removeNodeFromParent removes a
node and notifies the view:
  model.removeNodeFromParent (selectedNode) ;

Node Enumeration -
Sometimes you need to find a node in a tree by starting at
the root & visiting all children until you have found a
match.The DefaultMutableTreeNode class has several
methods for iterating through nodes.
The breadthFirstEnumeration & depthFirstEnumeration
methods return enumeration objects whose nextElement
method visits all children of the current node,using either
a breadth-first or depth-first traversal.
Breadth-first enumeration is the easiest to visualize.The
tree is traversed in layers.The root is visited first,followed
by all of its children, then followed by the grandchildren,
and so
on.To visualize depth-first enumeration,imagine a rat
trapped in a tree-shaped maze. It rushes along the first
path until it comes to a leaf.Then, it backtracks and turns
around to
the next path, and so on.
The postOrderTraversal method is a synonym for
depthFirstTraversal because the search process visits the
childen before visiting the parents.
Here is the typical usage pattern:

Enumeration breadthFirst =
node.breadthFirstEnumeration() ;
while (breadthFirst.hasMoreElements())
do something with breadthFirst. nextElement();
Finally,a method, pathFromAncestorEnumeration, finds a
path from an ancestor to a given node and then
enumerates
the nodes along that path.

          (draw diagram on page no 373 Fig 6-22)

Rendering Nodes-
 you will often need to change the way in which a tree
component
draws the nodes.The most common change is to choose
different icons for nodes and leaves.Other changes might
involve changing the font of the node labels or drawing
images at the nodes. All these changes are made possible
by installing a new tree
cell renderer into the tree.By default, the JTree class uses
DefaultTreeCellRenderer objects to draw each node.The
DefaultTreeCellRenderer class extends the JLabelclass. The
label contains the node icon and the node label.

You can customize the display in three ways :
1. You can change the icons, font, and background color
used
    by a DefaultTreeCellRenderer.These settings are used
for
    all nodes in the tree.
2. You can install a renderer that extends the
    DefaultTreeCellRenderer class and vary the icons,
fonts,
    and background color for each node.
3. You can install a renderer that implements the
    TreeCellRenderer interface, to draw a custom
image for each node.
To change the appearance of individual nodes,you install a
tree cell renderer.The TreeCellRenderer interface has a
single method:
Component getTreeCellRendererComponent(JTree tree,
Object value, boolean selected,
boolean expanded, boolean leaf, int row, boolean
hasFocus)

Custom Tree Models -

The TreeModel interface has only a handful of
methods.The first group of methods enables the JTree to
find the tree nodes by first getting the root, then the
children.The JTree class calls these methods only when the
user actually expands a node.
      Object getRoot ()
      int getChildCount(Object parent)
      Object getChild(Object parent, int index)

TheTreeModel is responsible for telling the JTree how they
are connected.The next method of the TreeModel
interface is the reverse of getChild:
     int getlndexOfChild(Object parent,Object child)

The tree model tells the JTree which nodes should be
displayed as leaves:
   boolean isLeaf(Object node)
If your code changes the tree model, then the tree needs
to be notified so that it can redraw itself. The tree adds
itself as a TreeModelListener to the model.Thus, the
model must support
the usual listener management methods:
     void addTreeModelListener(TreeModelListener l)
     void removeTreeModelListener(TreeModelListener l)

When the model modifies the tree contents, it calls one of
the four methods of the TreeModelListener interface:
   void treeNodesChanged(TreeModelEvent e)
   void treeNodeslnserted(TreeModelEvent e)
   void treeNodesRemoved (TreeModelEvent e )
   void treeStructureChanged(TreeModelEvent e)

The TreeModelEvent object describes the location of the
change.
Finally if the user edits a tree node, your model is called
with the change.
    Void valueForPathChanged(TreePath path ,Object
newValue)
If you don’t allow editing, this method is never called.

Progress Indicators -

A JProgressBar is a Swing component that indicates
progress. A ProgressMonitor is a dialog box that contains a
progress bar. A ProqressMonitorInputStream displays a
progress monitor
dialog box while the stream is read.

Progress Bars
A progressbar is a simple component-just a rectangle that
is partially filled with color to indicate the progress of an
operation. By default, progress is indicated by a string “n%
“.
You construct a progress bar much as you construct a
slider, by supplying the minimum and maximum value and
an optional orientation:
 progressBar = newJProgressBar(0, 1000);
 progressBar= newJProgressBar(SwingConstants.
VERTICAL, 0, 1000);

You can also set the minimum and maximum with the
setMinimum and setMaximum methods.
Unlike a slider,the progress bar cannot be adjusted by the
user. Your program needs to call setValueto update it.
If you call
     progressBar. setStringPainted(true);
the progress bar computes the completion percentage and
displays a string ”n% “ .If you want to show a different
string, you can supply it with the setString method:
    if (progressBar.getValue() > 900)
    progressBar.setString(" AlmostDone ");

      (draw diagram on page no 436 Fig 6-41)

Progress Monitor
A ProgressMonitor is a complete dialog box that contains a
progress bar.The dialog box contains a Cancel button.If
you click it,the monitor dialog box is closed.In
addition,your program can query whether the user has
canceled the dialog box & terminate the monitored action.
You construct a progress monitor by supplying the
following:
    The parent component over which the dialog box
     should pop up.
    An object that is displayed on the dialog box.
    An optional note to display below the object.
    The minimum and maximum values. .
As you update the progress value, you should also call the
isCanceled method to see if the program user has clicked
the
Cancel button.

       (draw diagram on page no 441 fig 6-42)

When the monitored activity has concluded, call the close
method to dismiss the dialog box.You can reuse the same
dialog box by calling start again.
There are two conditions for termination. The activity
might have completed, or the user might have canceled it.
In each of these cases, we close down :
    The timer that monitored the activity.
    The progress dialog box.
    The activity itself (by interrupting the thread).
Component Organizers –

These include the split pane, a mechanism for splitting an
area into multiple parts whose boundaries canbe
adjusted,the tabbed pane,which uses tab dividers to allow
a user to flip through multiple panels,& the desktop
pane.,which can be used to implement applications that
display multiple internal frames.

Split Pane -
Split panes split a component into two parts,with an
adjustable boundary in.The outer pane is split
vertically,with a text area on the bottom & another split
pane on the top.That pane is split horizontally,with a list
on the left & a label containing an image on the right.
You construct a split pane by specifying the orientation,
one of JSplitPane.HORIZONTAL_SPLIT or
JSplitPane.VERTICAL_SPLIT followed by the two
components.For eg-
JSplitPane innerPane = new JSplitPane(
JSplitPane.HORIZONTAl_SPLIT, planetList, planetImage) ;

If you like, you can add "one-touch expand" icons to the
splitter
bar.
              (draw diagram on page no 450 )
In the Metal look and feel, they are small triangles. If you
click one of them, the splitter moves all the way in the
direction to
which the triangle is pointing, expanding one of the panes
completely.
To add this capability, call
   innerPane. setOneTouchExpandable(true) ;
The "continuous layout" feature continuously repaints the
contents of both components as the user adjusts the
splitter.
You turn on that feature with the call
  innerPane.setContinuousLayout(true) ;
In the example program, we left the bottom splitter at the
default (no continuous layout).When you drag it, you only
move a black outline. When you release the mouse, the
components are repainted.

Tabbed Panes –

To remove a tab from the tab collection,use
     tabPane.removeTabAt(index);
When you add a new tab to the tab collection.It is not
automatically displayed.You must select it with the
setSelectedIndex method. For eg - here is how you show a
tab that you just added to the end:
  tabbed
Pane.setSelectedIndex(tabbedPane.getTabCount() -1) ;
 You set the tab layout to wrapped or scrolling mode by
calling
tabbedPane.setTabLayoutPolicy(JTabbedPane.WRAP_
 TAB_LAYOUT) ;
or
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_
TAB_LAYOUT);

To be notified whenever the user clicks on a new tab,you
install a ChangeListener with the tabbed pane.
 tabbedPane.addChangeListener(listener) ;

when the user selects a tab, the stateChanged method of
the change listener is called.You retrieve the tabbed pane
as the source of the event.Call the getSelectedIndex
method to find out which pane is about to be displayed.

 Public void stateChanged(ChangeEvent event)
 {
   int n=tabbedPane.getSelectedIndex() ;
   loadTab( n) ;
 }

           (draw diagram on page no 454 fig 6-46)

Mais conteúdo relacionado

Mais procurados

IJCSI-10-6-1-288-292
IJCSI-10-6-1-288-292IJCSI-10-6-1-288-292
IJCSI-10-6-1-288-292
HARDIK SINGH
 
MC0083 – Object Oriented Analysis &. Design using UML - Master of Computer Sc...
MC0083 – Object Oriented Analysis &. Design using UML - Master of Computer Sc...MC0083 – Object Oriented Analysis &. Design using UML - Master of Computer Sc...
MC0083 – Object Oriented Analysis &. Design using UML - Master of Computer Sc...
Aravind NC
 
UML Diagram @ Software engineering discussion
UML Diagram @ Software engineering discussionUML Diagram @ Software engineering discussion
UML Diagram @ Software engineering discussion
CherryBerry2
 
03 Object Relational Mapping
03 Object Relational Mapping03 Object Relational Mapping
03 Object Relational Mapping
Ranjan Kumar
 

Mais procurados (20)

Java swing
Java swingJava swing
Java swing
 
IJCSI-10-6-1-288-292
IJCSI-10-6-1-288-292IJCSI-10-6-1-288-292
IJCSI-10-6-1-288-292
 
Java awt
Java awtJava awt
Java awt
 
Evaluation of a hybrid method for constructing multiple SVM kernels
Evaluation of a hybrid method for constructing multiple SVM kernelsEvaluation of a hybrid method for constructing multiple SVM kernels
Evaluation of a hybrid method for constructing multiple SVM kernels
 
Java swing and events
Java swing and eventsJava swing and events
Java swing and events
 
The Fuzzy Logical Databases
The Fuzzy Logical DatabasesThe Fuzzy Logical Databases
The Fuzzy Logical Databases
 
Intro Uml
Intro UmlIntro Uml
Intro Uml
 
UML
UMLUML
UML
 
Fundamentals of Software Engineering
Fundamentals of Software Engineering Fundamentals of Software Engineering
Fundamentals of Software Engineering
 
Creating classes and applications in java
Creating classes and applications in javaCreating classes and applications in java
Creating classes and applications in java
 
Chapter 3
Chapter 3Chapter 3
Chapter 3
 
MC0083 – Object Oriented Analysis &. Design using UML - Master of Computer Sc...
MC0083 – Object Oriented Analysis &. Design using UML - Master of Computer Sc...MC0083 – Object Oriented Analysis &. Design using UML - Master of Computer Sc...
MC0083 – Object Oriented Analysis &. Design using UML - Master of Computer Sc...
 
Petals Count and Flower Image Classification using computer vision
Petals Count and Flower Image Classification using computer visionPetals Count and Flower Image Classification using computer vision
Petals Count and Flower Image Classification using computer vision
 
ITS-16163-Module 8-Graphic User Interface (GUI)
ITS-16163-Module 8-Graphic User Interface (GUI)ITS-16163-Module 8-Graphic User Interface (GUI)
ITS-16163-Module 8-Graphic User Interface (GUI)
 
PATTERNS03 - Behavioural Design Patterns
PATTERNS03 - Behavioural Design PatternsPATTERNS03 - Behavioural Design Patterns
PATTERNS03 - Behavioural Design Patterns
 
Java swing
Java swingJava swing
Java swing
 
Advanced c#
Advanced c#Advanced c#
Advanced c#
 
UML Diagram @ Software engineering discussion
UML Diagram @ Software engineering discussionUML Diagram @ Software engineering discussion
UML Diagram @ Software engineering discussion
 
class diagram
class diagramclass diagram
class diagram
 
03 Object Relational Mapping
03 Object Relational Mapping03 Object Relational Mapping
03 Object Relational Mapping
 

Semelhante a Unit 5 swing & TREES

Js info vis_toolkit
Js info vis_toolkitJs info vis_toolkit
Js info vis_toolkit
nikhilyagnic
 
Home Page Live(Www2007)
Home Page Live(Www2007)Home Page Live(Www2007)
Home Page Live(Www2007)
tomelf2007
 
Machine Learning Unit-5 Decesion Trees & Random Forest.pdf
Machine Learning Unit-5 Decesion Trees & Random Forest.pdfMachine Learning Unit-5 Decesion Trees & Random Forest.pdf
Machine Learning Unit-5 Decesion Trees & Random Forest.pdf
AdityaSoraut
 
Decision treeinductionmethodsandtheirapplicationtobigdatafinal 5
Decision treeinductionmethodsandtheirapplicationtobigdatafinal 5Decision treeinductionmethodsandtheirapplicationtobigdatafinal 5
Decision treeinductionmethodsandtheirapplicationtobigdatafinal 5
ssuser33da69
 
Java căn bản - Chapter7
Java căn bản - Chapter7Java căn bản - Chapter7
Java căn bản - Chapter7
Vince Vo
 
computer notes - Linked list inside computer memory
computer notes - Linked list inside computer memorycomputer notes - Linked list inside computer memory
computer notes - Linked list inside computer memory
ecomputernotes
 
Ch14 Ancestor Tree
Ch14 Ancestor TreeCh14 Ancestor Tree
Ch14 Ancestor Tree
leminhvuong
 

Semelhante a Unit 5 swing & TREES (20)

TrackBar and TreeView
TrackBar and TreeViewTrackBar and TreeView
TrackBar and TreeView
 
Desicion tree and neural networks
Desicion tree and neural networksDesicion tree and neural networks
Desicion tree and neural networks
 
Js info vis_toolkit
Js info vis_toolkitJs info vis_toolkit
Js info vis_toolkit
 
Know How to Create and Visualize a Decision Tree with Python.pdf
Know How to Create and Visualize a Decision Tree with Python.pdfKnow How to Create and Visualize a Decision Tree with Python.pdf
Know How to Create and Visualize a Decision Tree with Python.pdf
 
Home Page Live(Www2007)
Home Page Live(Www2007)Home Page Live(Www2007)
Home Page Live(Www2007)
 
Decision tree
Decision tree Decision tree
Decision tree
 
Classes1
Classes1Classes1
Classes1
 
Machine Learning Unit-5 Decesion Trees & Random Forest.pdf
Machine Learning Unit-5 Decesion Trees & Random Forest.pdfMachine Learning Unit-5 Decesion Trees & Random Forest.pdf
Machine Learning Unit-5 Decesion Trees & Random Forest.pdf
 
Decision treeinductionmethodsandtheirapplicationtobigdatafinal 5
Decision treeinductionmethodsandtheirapplicationtobigdatafinal 5Decision treeinductionmethodsandtheirapplicationtobigdatafinal 5
Decision treeinductionmethodsandtheirapplicationtobigdatafinal 5
 
Chapter 1 Introduction to Data Structures and Algorithms.pdf
Chapter 1 Introduction to Data Structures and Algorithms.pdfChapter 1 Introduction to Data Structures and Algorithms.pdf
Chapter 1 Introduction to Data Structures and Algorithms.pdf
 
Chapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part IIChapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part II
 
Java căn bản - Chapter7
Java căn bản - Chapter7Java căn bản - Chapter7
Java căn bản - Chapter7
 
User Defined Datatypes in C++ (Union, enum, class)
User Defined Datatypes in C++  (Union, enum, class)User Defined Datatypes in C++  (Union, enum, class)
User Defined Datatypes in C++ (Union, enum, class)
 
computer notes - Linked list inside computer memory
computer notes - Linked list inside computer memorycomputer notes - Linked list inside computer memory
computer notes - Linked list inside computer memory
 
Ch14 Ancestor Tree
Ch14 Ancestor TreeCh14 Ancestor Tree
Ch14 Ancestor Tree
 
Machine learning session 10
Machine learning session 10Machine learning session 10
Machine learning session 10
 
Machine Learning Algorithm - Decision Trees
Machine Learning Algorithm - Decision Trees Machine Learning Algorithm - Decision Trees
Machine Learning Algorithm - Decision Trees
 
JFree chart
JFree chartJFree chart
JFree chart
 
Sharbani bhattacharya VB Structures
Sharbani bhattacharya VB StructuresSharbani bhattacharya VB Structures
Sharbani bhattacharya VB Structures
 
Vb.net iv
Vb.net ivVb.net iv
Vb.net iv
 

Último

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Último (20)

FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 

Unit 5 swing & TREES

  • 1. TREES – Many tree structures arise in everyday life,such as the hierarchy of countries ,states & cities.The Swing library has a JTree class for this purpose.The JTree class takes care of laying out the tree and processing user requests for expanding and collapsing nodes. A tree is composed of nodes.Every node is either a leaf or it has child nodes.Everynode,with the exception of the root node, has exactly one parent.A tree has exactly one root node. Sometimes you have a collection of trees, each of which has its own root node.Such a collection is called a forest. (draw diagram on page no 358 Fig 6.7 ) Simple Trees - The JTree component follows the model view-controller pattern. You provide a model of the hierarchical data, and the component displays it for you. To construct a JTree, you supply the tree model in the constructor : TreeModel model=. . . ; JTreee tree = new JTree(model) ;
  • 2. To construct a default tree model, you must supply a root node. TreeNode root = . . . ; DefaultTreeModel model= newDefaultTreeModel(root) ; TreeNode is another interface. you can simply pass the root node to the JTree constructor. Then the tree automatically constructs a default tree model : JTree tree =new JTree(root) ; Editing Trees and Tree Paths - The JTree class has a surprising way of identifying nodes in a tree. It doesnot deal with tree nodes, but with paths of objects,called tree paths. A tree path starts at the root & consists of a sequence of child nodes. (draw diagram on page no 366 Fig 6-18) The JTree class knows nothing about the TreeNode interface.That interace is never used by the TreeModel interface.it is only used by the DefaultTreeModel implementation.You can have other tree models in which the nodes do not implement the TreeNode interface at all.If you use a tree model that manages other types of objects, then those objects may not have getParent & getChild methods.
  • 3. The TreePath class manages a sequence of Object references. A number of JTree methods return Treepath objects.When you have a tree path,you usually just need to Know the terminal node,which you get with the getLastPathComponent method.For eg, to find out the currently selected node in a tree, you use the getSelectionPath method of the JTree class.You get a TreePath object back, from which you can retrieve the actual node. TreePath selectionPath= tree.getSelectionPath() ; DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) selectionPath.getLastPathComponent() ; If you use the insertNodeInto method of the DefaultTreeModel class, the model class takes care of that. For eg, the following call appends a new node as the last child of the selected node & notifies the tree view. model.insertNodelnto(newNode, selectedNode , selectedNode.getchildCount()) ; The analogous call removeNodeFromParent removes a node and notifies the view: model.removeNodeFromParent (selectedNode) ; Node Enumeration -
  • 4. Sometimes you need to find a node in a tree by starting at the root & visiting all children until you have found a match.The DefaultMutableTreeNode class has several methods for iterating through nodes. The breadthFirstEnumeration & depthFirstEnumeration methods return enumeration objects whose nextElement method visits all children of the current node,using either a breadth-first or depth-first traversal. Breadth-first enumeration is the easiest to visualize.The tree is traversed in layers.The root is visited first,followed by all of its children, then followed by the grandchildren, and so on.To visualize depth-first enumeration,imagine a rat trapped in a tree-shaped maze. It rushes along the first path until it comes to a leaf.Then, it backtracks and turns around to the next path, and so on. The postOrderTraversal method is a synonym for depthFirstTraversal because the search process visits the childen before visiting the parents. Here is the typical usage pattern: Enumeration breadthFirst = node.breadthFirstEnumeration() ; while (breadthFirst.hasMoreElements()) do something with breadthFirst. nextElement(); Finally,a method, pathFromAncestorEnumeration, finds a path from an ancestor to a given node and then enumerates
  • 5. the nodes along that path. (draw diagram on page no 373 Fig 6-22) Rendering Nodes- you will often need to change the way in which a tree component draws the nodes.The most common change is to choose different icons for nodes and leaves.Other changes might involve changing the font of the node labels or drawing images at the nodes. All these changes are made possible by installing a new tree cell renderer into the tree.By default, the JTree class uses DefaultTreeCellRenderer objects to draw each node.The DefaultTreeCellRenderer class extends the JLabelclass. The label contains the node icon and the node label. You can customize the display in three ways : 1. You can change the icons, font, and background color used by a DefaultTreeCellRenderer.These settings are used for all nodes in the tree. 2. You can install a renderer that extends the DefaultTreeCellRenderer class and vary the icons, fonts, and background color for each node. 3. You can install a renderer that implements the TreeCellRenderer interface, to draw a custom
  • 6. image for each node. To change the appearance of individual nodes,you install a tree cell renderer.The TreeCellRenderer interface has a single method: Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) Custom Tree Models - The TreeModel interface has only a handful of methods.The first group of methods enables the JTree to find the tree nodes by first getting the root, then the children.The JTree class calls these methods only when the user actually expands a node. Object getRoot () int getChildCount(Object parent) Object getChild(Object parent, int index) TheTreeModel is responsible for telling the JTree how they are connected.The next method of the TreeModel interface is the reverse of getChild: int getlndexOfChild(Object parent,Object child) The tree model tells the JTree which nodes should be displayed as leaves: boolean isLeaf(Object node)
  • 7. If your code changes the tree model, then the tree needs to be notified so that it can redraw itself. The tree adds itself as a TreeModelListener to the model.Thus, the model must support the usual listener management methods: void addTreeModelListener(TreeModelListener l) void removeTreeModelListener(TreeModelListener l) When the model modifies the tree contents, it calls one of the four methods of the TreeModelListener interface: void treeNodesChanged(TreeModelEvent e) void treeNodeslnserted(TreeModelEvent e) void treeNodesRemoved (TreeModelEvent e ) void treeStructureChanged(TreeModelEvent e) The TreeModelEvent object describes the location of the change. Finally if the user edits a tree node, your model is called with the change. Void valueForPathChanged(TreePath path ,Object newValue) If you don’t allow editing, this method is never called. Progress Indicators - A JProgressBar is a Swing component that indicates progress. A ProgressMonitor is a dialog box that contains a progress bar. A ProqressMonitorInputStream displays a progress monitor
  • 8. dialog box while the stream is read. Progress Bars A progressbar is a simple component-just a rectangle that is partially filled with color to indicate the progress of an operation. By default, progress is indicated by a string “n% “. You construct a progress bar much as you construct a slider, by supplying the minimum and maximum value and an optional orientation: progressBar = newJProgressBar(0, 1000); progressBar= newJProgressBar(SwingConstants. VERTICAL, 0, 1000); You can also set the minimum and maximum with the setMinimum and setMaximum methods. Unlike a slider,the progress bar cannot be adjusted by the user. Your program needs to call setValueto update it. If you call progressBar. setStringPainted(true); the progress bar computes the completion percentage and displays a string ”n% “ .If you want to show a different string, you can supply it with the setString method: if (progressBar.getValue() > 900) progressBar.setString(" AlmostDone "); (draw diagram on page no 436 Fig 6-41) Progress Monitor
  • 9. A ProgressMonitor is a complete dialog box that contains a progress bar.The dialog box contains a Cancel button.If you click it,the monitor dialog box is closed.In addition,your program can query whether the user has canceled the dialog box & terminate the monitored action. You construct a progress monitor by supplying the following:  The parent component over which the dialog box should pop up.  An object that is displayed on the dialog box.  An optional note to display below the object.  The minimum and maximum values. . As you update the progress value, you should also call the isCanceled method to see if the program user has clicked the Cancel button. (draw diagram on page no 441 fig 6-42) When the monitored activity has concluded, call the close method to dismiss the dialog box.You can reuse the same dialog box by calling start again. There are two conditions for termination. The activity might have completed, or the user might have canceled it. In each of these cases, we close down :  The timer that monitored the activity.  The progress dialog box.  The activity itself (by interrupting the thread).
  • 10. Component Organizers – These include the split pane, a mechanism for splitting an area into multiple parts whose boundaries canbe adjusted,the tabbed pane,which uses tab dividers to allow a user to flip through multiple panels,& the desktop pane.,which can be used to implement applications that display multiple internal frames. Split Pane - Split panes split a component into two parts,with an adjustable boundary in.The outer pane is split vertically,with a text area on the bottom & another split pane on the top.That pane is split horizontally,with a list on the left & a label containing an image on the right. You construct a split pane by specifying the orientation, one of JSplitPane.HORIZONTAL_SPLIT or JSplitPane.VERTICAL_SPLIT followed by the two components.For eg- JSplitPane innerPane = new JSplitPane( JSplitPane.HORIZONTAl_SPLIT, planetList, planetImage) ; If you like, you can add "one-touch expand" icons to the splitter bar. (draw diagram on page no 450 )
  • 11. In the Metal look and feel, they are small triangles. If you click one of them, the splitter moves all the way in the direction to which the triangle is pointing, expanding one of the panes completely. To add this capability, call innerPane. setOneTouchExpandable(true) ; The "continuous layout" feature continuously repaints the contents of both components as the user adjusts the splitter. You turn on that feature with the call innerPane.setContinuousLayout(true) ; In the example program, we left the bottom splitter at the default (no continuous layout).When you drag it, you only move a black outline. When you release the mouse, the components are repainted. Tabbed Panes – To remove a tab from the tab collection,use tabPane.removeTabAt(index); When you add a new tab to the tab collection.It is not automatically displayed.You must select it with the setSelectedIndex method. For eg - here is how you show a tab that you just added to the end: tabbed Pane.setSelectedIndex(tabbedPane.getTabCount() -1) ; You set the tab layout to wrapped or scrolling mode by calling
  • 12. tabbedPane.setTabLayoutPolicy(JTabbedPane.WRAP_ TAB_LAYOUT) ; or tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_ TAB_LAYOUT); To be notified whenever the user clicks on a new tab,you install a ChangeListener with the tabbed pane. tabbedPane.addChangeListener(listener) ; when the user selects a tab, the stateChanged method of the change listener is called.You retrieve the tabbed pane as the source of the event.Call the getSelectedIndex method to find out which pane is about to be displayed. Public void stateChanged(ChangeEvent event) { int n=tabbedPane.getSelectedIndex() ; loadTab( n) ; } (draw diagram on page no 454 fig 6-46)