SlideShare ist ein Scribd-Unternehmen logo
1 von 21
Next: Practical Usage of Tress Up: Trees JTree Previous: Trees JTree




List v. Trees
Lists are good for displaying simple lists of information from which the user can make
single or multiple selections.

A list is inadequate for more complex selections, for example:

   •   Selecting items in a structured hierarchy,
   •   If you were writing a program containing several general selection topics
   •   Each of these topics could be subdivided into many specific subtopics,
   •   You would require a list of subtopics for each general topic.
   •   These numerous list boxes consume vast amounts of GUI space and would be
       hard to read and assimilate.
   •   Huge amount of code you would be needed to support this also -- For four or five
       separate lists, you could still manage a scenario like this, but what if you had a
       hundred general topics?

A better solution to this problem is to implement a single control containing all of the
lists:

   •   Simplified Interface
   •   Reduced amount code YOU write.
Example of a Tree Structure

A hierarchical tree realises this structure:

    •   It can nest lists of lists to any depth required.
    •   Tree control via a series of collapsible/ expandable branches it can contain.
    •   Each of the tree's branches can be divided into successively more specific
        branches
    •   There is no practical limit to the number of items that each branch can hold, nor to
        the nesting of subbranches.

The JTree class is the Swing component that provides these facilities.


Next: Practical Usage of Tress Up: Trees JTree Previous: Trees JTree
Dave Marshall
4/14/1999

Next: A basic JTree example Up: Trees JTree Previous: List v. Trees




Practical Usage of Tress in Interfaces
Novice software users may experience problems working with hierarchical data
structures:

   •   If you are designing for a more sophisticated user, this is not an issue.
   •   For the average user, try to avoid using a tree control that doesn't already strongly
       conform to an existing UI metaphor, Windows/Motif users should be familiar
       with Trees for managing file directories:
           o This may mean using a tree in combination with a list to display lowest
               level tree nodes (for example, Windows Explorer).
   •   Trees have the unfortunate side effect of encouraging the creation of deep nested
       structures that become difficult for the user to navigate and manage.


Dave Marshall
4/14/1999

Next: Adding new tree items Up: Trees JTree Previous: Practical Usage of Tress




A basic JTree example
Let us demonstrate a simple creation of a Tree application, TreeExample.java:

   •   we will create a simple tree (Fig ) containing only a few items and having no
       ability to add or remove items.
   •   we will include a scrollable pane, which will become more important as we add
       features to our application.
   •   The icons shown in this example are defaults provided by Swing.
   •   We do not actually insert an items in the tree. When no specific data model has
       been specified, JTree reverts to a default example model which is impractical but
       useful for this demonstration.
   •   When the program is executed, notice that the tree is initially collapsed down to
       the root level.
   •   Opening the root expands it, showing the next level of indentation -- Notice the
       change in icon for the lowest element of the tree.
   •   This is the default operation for the simple tree.

The full code listing for TreeExample.java is as follows:

// Imports
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class TreeExample
               extends                JFrame
{
         // Instance attributes used in this example
         private JPanel         topPanel;
         private JTree          tree;
         private JScrollPane scrollPane;

         // Constructor of main frame
         public TreeExample()
         {
                 // Set the frame characteristics
                 setTitle( "Simple Tree Application" );
                 setSize( 300, 100 );
                 setBackground( Color.gray );

                   // Create a panel to hold all other components
                   topPanel = new JPanel();
                   topPanel.setLayout( new BorderLayout() );
                   getContentPane().add( topPanel );

                   // Create a new tree control
                   tree = new JTree();

                   // Add the listbox to a scrolling pane
                   scrollPane = new JScrollPane();
                   scrollPane.getViewport().add( tree );
                   topPanel.add( scrollPane, BorderLayout.CENTER );
         }

         // Main entry point for this example
         public static void main( String args[] )
         {
                 // Create an instance of the test application
                 TreeExample mainFrame = new TreeExample();
                 mainFrame.setVisible( true );
         }
}




Dave Marshall
4/14/1999

Next: Custom data models Up: Trees JTree Previous: A basic JTree example




Adding new tree items
Let us now look at some code, AddTreeExample.java that creates our own Tree items:

Adding branches and items to a tree instance involves a bit of additional code.
AddTreeExample.java generates a JTree instance containing all of the cards in a deck
of playing cards:




AddTreeExample.java Program Output

   •   The key feature in this application is the use of the DefaultMutableTreeNode
       class.
   •   The DefaultMutableTreeNode is a general-purpose node item compatible with
       the Swing graphical tree component.
   •   Each child node is inserted into its parent node using the add() method
   •   any child node can itself act as a parent for subsequent child nodes.
   •   The root is a special node because it is the ultimate parent of all of the other tree
       nodes -- inserted into the tree's data model, an instance of the DefaultTreeModel
       class.
   •   This is the model that a JTree instance should use, unless a custom data model is
       used.

The full code listing for AddTreeExample.java is as follows:

// Imports
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;

class AddTreeExample
                extends        JFrame
 {
        // Instance attributes used in this example
        private JPanel         topPanel;
        private JTree          tree;
        private JScrollPane scrollPane;

         // Constructor of main frame
         public AddTreeExample()
{
               // Set the frame characteristics
               setTitle( "More Advanced Tree Application" );
               setSize( 300, 200 );
               setBackground( Color.gray );

               // Create a panel to hold all other components
               topPanel = new JPanel();
               topPanel.setLayout( new BorderLayout() );
               getContentPane().add( topPanel );

               // Create data for the tree
               DefaultMutableTreeNode root
                                      = new
DefaultMutableTreeNode( "Deck" );

               DefaultMutableTreeNode itemClubs
                                      = new
DefaultMutableTreeNode( "Clubs" );
               addAllCard( itemClubs );
               root.add( itemClubs );

               DefaultMutableTreeNode itemDiamonds
                                      = new
DefaultMutableTreeNode( "Diamonds" );
               addAllCard( itemDiamonds );
               root.add( itemDiamonds );

               DefaultMutableTreeNode itemSpades
                                      = new
DefaultMutableTreeNode( "Spades" );
               addAllCard( itemSpades );
               root.add( itemSpades );

               DefaultMutableTreeNode itemHearts
                                      = new
DefaultMutableTreeNode( "Hearts" );
               addAllCard( itemHearts );
               root.add( itemHearts );

               // Create a new tree control
               DefaultTreeModel treeModel = new
DefaultTreeModel( root );
               tree = new JTree( treeModel );


               // Add the listbox to a scrolling pane
               scrollPane = new JScrollPane();
               scrollPane.getViewport().add( tree );
               topPanel.add( scrollPane, BorderLayout.CENTER );
       }

       // Helper method to write an enitre suit of cards to the
       // current tree node
       public void addAllCard( DefaultMutableTreeNode suit )
       {
               suit.add( new DefaultMutableTreeNode( "Ace" ) );
suit.add(    new   DefaultMutableTreeNode(      "Two" ) );
                   suit.add(    new   DefaultMutableTreeNode(      "Three" ) );
                   suit.add(    new   DefaultMutableTreeNode(      "Four" ) );
                   suit.add(    new   DefaultMutableTreeNode(      "Five" ) );
                   suit.add(    new   DefaultMutableTreeNode(      "Six" ) );
                   suit.add(    new   DefaultMutableTreeNode(      "Seven" ) );
                   suit.add(    new   DefaultMutableTreeNode(      "Eight" ) );
                   suit.add(    new   DefaultMutableTreeNode(      "Nine" ) );
                   suit.add(    new   DefaultMutableTreeNode(      "Ten" ) );
                   suit.add(    new   DefaultMutableTreeNode(      "Jack" ) );
                   suit.add(    new   DefaultMutableTreeNode(      "Queen" ) );
                   suit.add(    new   DefaultMutableTreeNode(      "King" ) );
          }

          // Main entry point for this example
          public static void main( String args[] )
          {
                  // Create an instance of the test application
                  AddTreeExample mainFrame       = new AddTreeExample();
                  mainFrame.setVisible( true );
          }
}




Dave Marshall
4/14/1999

Next: Custom rendering Up: Trees JTree Previous: Adding new tree items




Custom data models
The JTree class (like Jlist) supports custom data modeling:

    •   slightly more complex here due to hierarchical nature of a tree

The CustomTreeExample.java example, we will recreate the tree containing the deck of
cards. However,

    •   a custom data model is used to manufacture the cards within each suit.
    •   The result is an application consisting of approximately the same number of code
        lines, but with much better performance, particularly for large data sets.
    •   Graphical Appearance to User is the same.

As in the Custom Data Model List example we have two classes:

    •   CustomTreeExample -- the main class
•   MyDataModel -- which creates the custom data model by extending the
       DefaultTreModel.

The code listing for CustomTreeExample.java is:

// Imports
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;

class CustomTreeExample
                extends        JFrame
 {
        // Instance attributes used in this example
        private JPanel         topPanel;
        private JTree          tree;
        private JScrollPane scrollPane;

        // Constructor of main frame
        public CustomTreeExample()
        {
                // Set the frame characteristics
                setTitle( "Custom Data Model Tree Application" );
                setSize( 300, 200 );
                setBackground( Color.gray );

                 // Create a panel to hold all other components
                 topPanel = new JPanel();
                 topPanel.setLayout( new BorderLayout() );
                 getContentPane().add( topPanel );

               // Create data for the tree
               DefaultMutableTreeNode root
                                      = new
DefaultMutableTreeNode( "Deck" );

                 CreateSuit(    root,   "Clubs" );
                 CreateSuit(    root,   "Diamonds" );
                 CreateSuit(    root,   "Spades" );
                 CreateSuit(    root,   "Hearts" );

                 // Create a new tree control
                 MyDataModel treeModel = new MyDataModel( root );
                 tree = new JTree( treeModel );

                 // Add the listbox to a scrolling pane
                 scrollPane = new JScrollPane();
                 scrollPane.getViewport().add( tree );
                 topPanel.add( scrollPane, BorderLayout.CENTER );
        }

        public void CreateSuit( DefaultMutableTreeNode root, String
suitName )
        {
DefaultMutableTreeNode itemSuit
                                              = new
DefaultMutableTreeNode( suitName );
                       for( int iCtr = 0; iCtr < 13; iCtr++ )
                               itemSuit.add( new DefaultMutableTreeNode(
"" ) );

                   root.add( itemSuit );
         }

         // Main entry point for this example
         public static void main( String args[] )
         {
                 // Create an instance of the test application
                 CustomTreeExample mainFrame    = new CustomTreeExample();
                 mainFrame.setVisible( true );
         }
}

The code listing for MyDataModel.java is:

// Imports
import java.util.*;
import javax.swing.*;
import javax.swing.tree.*;

class MyDataModel
        extends         DefaultTreeModel
{
        private DefaultMutableTreeNode root;
        private String rootName = "";
        private String cardArray[] = {
                                                 "Ace", "Two", "Three",
         "Four",
                                                 "Five", "Six", "Seven",
         "Eight",
                                                 "Nine", "Ten", "Jack",
         "Queen",
                                                 "King"
                                            };

        public MyDataModel( TreeNode root )
        {
                super( root );
                DefaultMutableTreeNode parentNode =
(DefaultMutableTreeNode)root;
                rootName = (String)parentNode.getUserObject();
        }

        public Object getChild( Object parent, int index )
        {
                DefaultMutableTreeNode parentNode =
(DefaultMutableTreeNode)parent;
                String parentName = (String)parentNode.getUserObject();

                   if( parentName.equals( rootName ) )
return super.getChild( parent, index );
                   else
                       return new
DefaultMutableTreeNode( cardArray[index] );
        }
}




Dave Marshall
4/14/1999

Next: Editing tree nodes Up: Trees JTree Previous: Custom data models




Custom rendering
In addition to handling custom data modeling, the JTree class also supports custom
rendering of individual items within the list.

This is useful for adding graphics or changing the font of specific items in the tree.

The example extends the playing card tree example by implementing a custom renderer
class to handle some special features:

   •   The top-level items (the suits) include graphics for each of the suits in a deck of
       cards,
   •   The individual cards also include graphics.




Custom Tree Rendering Example
As in the JList rendering example we have two classes:

   •   RenderTreeExample is almost identical to the main Tree classes shown in
       previous examples -- it t creates a tree instance and inserts all of the pertinent
       data. It has one additional line to notify the tree that we will provide a custom
       renderer.
   •   CustomCellRenderer -- contains the rendering code for this application.
           o To save time during execution, the class implements a constructor to load
                the graphics.
           o The getTreeCellRendererComponent() method must be provided as a
                requirement of the TreeCellRender implementation used by this class
                is responsible for the actual drawing of tree cells.

In this example, getTreeCellRendererComponent()

   •   determines what it is drawing (based on the text in the specified tree node) and
       then assigns the correct image to the displayed items.
   •   then sets the correct foreground color, depending on the selection state of the
       item.
   •   A paint() method is required within the custom rendering class to work around a
       peculiar feature (yes a BUG) in Swing to set the item background.

The code listing for RenderTreeExample.java is:

// Imports
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;

class RenderTreeExample
                extends        JFrame
 {
        // Instance attributes used in this example
        private JPanel         topPanel;
        private JTree          tree;
        private JScrollPane scrollPane;

         // Constructor of main frame
         public RenderTreeExample()
         {
                 // Set the frame characteristics
                 setTitle( "Custom Rendered Tree Application" );
                 setSize( 300, 200 );
                 setBackground( Color.gray );

                  // Create a panel to hold all other components
                  topPanel = new JPanel();
                  topPanel.setLayout( new BorderLayout() );
                  getContentPane().add( topPanel );
// Create data for the tree
               DefaultMutableTreeNode root
                                      = new
DefaultMutableTreeNode( "Deck" );

               DefaultMutableTreeNode itemClubs
                                      = new
DefaultMutableTreeNode( "Clubs" );
               addAllCard( itemClubs );
               root.add( itemClubs );

               DefaultMutableTreeNode itemDiamonds
                                      = new
DefaultMutableTreeNode( "Diamonds" );
               addAllCard( itemDiamonds );
               root.add( itemDiamonds );

               DefaultMutableTreeNode itemSpades
                                      = new
DefaultMutableTreeNode( "Spades" );
               addAllCard( itemSpades );
               root.add( itemSpades );

               DefaultMutableTreeNode itemHearts
                                      = new
DefaultMutableTreeNode( "Hearts" );
               addAllCard( itemHearts );
               root.add( itemHearts );

               // Create a new tree control
               DefaultTreeModel treeModel = new
DefaultTreeModel( root );
               tree = new JTree( treeModel );

       // Tell the tree it is being rendered by our application
       tree.setCellRenderer( new CustomCellRenderer() );

               // Add the listbox to a scrolling pane
               scrollPane = new JScrollPane();
               scrollPane.getViewport().add( tree );
               topPanel.add( scrollPane, BorderLayout.CENTER );
       }

       // Helper method to write an enitre suit of cards to the
       // current tree node
       public void addAllCard( DefaultMutableTreeNode suit )
       {
               suit.add( new DefaultMutableTreeNode( "Ace" ) );
               suit.add( new DefaultMutableTreeNode( "Two" ) );
               suit.add( new DefaultMutableTreeNode( "Three" ) );
               suit.add( new DefaultMutableTreeNode( "Four" ) );
               suit.add( new DefaultMutableTreeNode( "Five" ) );
               suit.add( new DefaultMutableTreeNode( "Six" ) );
               suit.add( new DefaultMutableTreeNode( "Seven" ) );
               suit.add( new DefaultMutableTreeNode( "Eight" ) );
               suit.add( new DefaultMutableTreeNode( "Nine" ) );
               suit.add( new DefaultMutableTreeNode( "Ten" ) );
suit.add( new DefaultMutableTreeNode( "Jack" ) );
                 suit.add( new DefaultMutableTreeNode( "Queen" ) );
                 suit.add( new DefaultMutableTreeNode( "King" ) );
        }

        // Main entry point for this example
        public static void main( String args[] )
        {
                // Create an instance of the test application
                RenderTreeExample mainFrame    = new RenderTreeExample();
                mainFrame.setVisible( true );
        }
}

The code listing for CustomCellRenderer.java is:

// Imports
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;

public class CustomCellRenderer
                extends        JLabel
                implements     TreeCellRenderer
{
   private ImageIcon           deckImage;
   private ImageIcon[]         suitImages;
   private ImageIcon[]         cardImages;
        private boolean                bSelected;


        public CustomCellRenderer()
        {
                // Load the images
                deckImage = new ImageIcon( "deck.gif" );

                 suitImages = new ImageIcon[4];
                 suitImages[0] = new ImageIcon(    "clubs.gif" );
                 suitImages[1] = new ImageIcon(    "diamonds.gif" );
                 suitImages[2] = new ImageIcon(    "spades.gif" );
                 suitImages[3] = new ImageIcon(    "hearts.gif" );

                 cardImages = new ImageIcon[13];
                 cardImages[0] = new ImageIcon( "ace.gif" );
                 cardImages[1] = new ImageIcon( "two.gif" );
                 cardImages[2] = new ImageIcon( "three.gif" );
                 cardImages[3] = new ImageIcon( "four.gif" );
                 cardImages[4] = new ImageIcon( "five.gif" );
                 cardImages[5] = new ImageIcon( "six.gif" );
                 cardImages[6] = new ImageIcon( "seven.gif" );
                 cardImages[7] = new ImageIcon( "eight.gif" );
                 cardImages[8] = new ImageIcon( "nine.gif" );
                 cardImages[9] = new ImageIcon( "ten.gif" );
                 cardImages[10] = new ImageIcon( "jack.gif" );
                 cardImages[11] = new ImageIcon( "queen.gif" );
                 cardImages[12] = new ImageIcon( "king.gif" );
}

        public Component getTreeCellRendererComponent( JTree tree,
                                       Object value, boolean bSelected,
boolean bExpanded,
                                                      boolean bLeaf, int
iRow, boolean bHasFocus )
        {
                // Find out which node we are rendering and get its text
                DefaultMutableTreeNode node =
(DefaultMutableTreeNode)value;
                String labelText = (String)node.getUserObject();

               this.bSelected = bSelected;

               // Set the correct foreground color
               if( !bSelected )
                       setForeground( Color.black );
               else
                       setForeground( Color.white );

               // Determine the correct icon to display
               if( labelText.equals( "Deck" ) )
                       setIcon( deckImage );
               else if( labelText.equals( "Clubs" ) )
                       setIcon( suitImages[0] );
               else if( labelText.equals( "Diamonds" ) )
                       setIcon( suitImages[1] );
               else if( labelText.equals( "Spades" ) )
                       setIcon( suitImages[2] );
               else if( labelText.equals( "Hearts" ) )
                       setIcon( suitImages[3] );
               else if( labelText.equals( "Ace" ) )
                       setIcon( cardImages[0] );
               else if( labelText.equals( "Two" ) )
                       setIcon( cardImages[1] );
               else if( labelText.equals( "Three" ) )
                       setIcon( cardImages[2] );
               else if( labelText.equals( "Four" ) )
                       setIcon( cardImages[3] );
               else if( labelText.equals( "Five" ) )
                       setIcon( cardImages[4] );
               else if( labelText.equals( "Six" ) )
                       setIcon( cardImages[5] );
               else if( labelText.equals( "Seven" ) )
                       setIcon( cardImages[6] );
               else if( labelText.equals( "Eight" ) )
                       setIcon( cardImages[7] );
               else if( labelText.equals( "Nine" ) )
                       setIcon( cardImages[8] );
               else if( labelText.equals( "Ten" ) )
                       setIcon( cardImages[9] );
               else if( labelText.equals( "Jack" ) )
                       setIcon( cardImages[10] );
               else if( labelText.equals( "Queen" ) )
                       setIcon( cardImages[11] );
               else if( labelText.equals( "King" ) )
setIcon( cardImages[12] );

                    // Add the text to the cell
                    setText( labelText );

                    return this;
          }

          // This is a hack to paint the background.               Normally a JLabel
can
          // paint its own background, but due to an apparent bug or
          // limitation in the TreeCellRenderer, the paint method is
          // required to handle this.
          public void paint( Graphics g )
          {
                  Color          bColor;
                  Icon           currentI = getIcon();

                    // Set the correct background color
                    bColor = bSelected ? SystemColor.textHighlight :
Color.white;
                    g.setColor( bColor );

                    // Draw a rectangle in the background of the cell
                    g.fillRect( 0, 0, getWidth() - 1, getHeight() - 1 );

                    super.paint( g );
          }

}


Next: Editing tree nodes Up: Trees JTree Previous: Custom data models
Dave Marshall
4/14/1999

Next: Listening for tree actions Up: Trees JTree Previous: Custom rendering




Editing tree nodes
In a manner similar to custom rendering, we can also perform custom editing of any item
in a tree control. JTree supports three techniques for editing tree nodes.

    •   The easiest is to simply call the setEditable(value) method and set the value
        to be true

        In this mode, the tree values are editable using a simple text editor.
•   The second technique is to create a real custom editor for the tree, and this is what
       we will do in the EditTreeExample.java example below.
   •   The third technique for tree editing is to replace the DefaultCellEditor()
       reference in EditTreeExample.java with a class that implements CellEditor.
       Refer to the online Swing documentation for details about the CellEditor
       implementation.

Let us now look at the EditTreeExample.java example where we create a tree instance
and set the default cell editor to point to a combo box which allows node changes only
from the list of selections as shown below:




EditTreeExample.java Output

The full code listing for EditTreeExample.java is given below:

// Imports
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

class EditTreeExample
                extends        JFrame
 {
        // Instance attributes used in this example
        private JPanel         topPanel;
        private JTree          tree;
        private JScrollPane scrollPane;

         // Constructor of main frame
         public EditTreeExample()
         {
// Set the frame characteristics
                  setTitle( "Editable Tree Application" );
                  setSize( 300, 100 );
                  setBackground( Color.gray );

                  // Create a panel to hold all other components
                  topPanel = new JPanel();
                  topPanel.setLayout( new BorderLayout() );
                  getContentPane().add( topPanel );

                  // Create a new tree control
                  tree = new JTree();
                  tree.setEditable( true );
                  tree.setShowsRootHandles( false );

         // Create a combo box of editing options
                JComboBox box = new JComboBox();
                box.setMinimumSize( new Dimension( 100, 40 ) );
                box.addItem( "Swing" );
                box.addItem( "Java" );
                box.addItem( "neat" );
                box.addItem( "funky" );
                box.addItem( "life" );
                box.addItem( "awesome" );
                box.addItem( "cool!" );

                // Add a cell editor to the tree
         tree.setCellEditor( new DefaultCellEditor( box ) );

                  // Add the listbox to a scrolling pane
                  scrollPane = new JScrollPane();
                  scrollPane.getViewport().add( tree );
                  topPanel.add( scrollPane, BorderLayout.CENTER );
         }

         // Main entry point for this example
         public static void main( String args[] )
         {
                 // Create an instance of the test application
                 EditTreeExample mainFrame      = new EditTreeExample();
                 mainFrame.setVisible( true );
         }
}




Dave Marshall
4/14/1999

Next: Listening for tree selections Up: Trees JTree Previous: Editing tree nodes
Listening for tree actions
When the user actually makes a selection or performs some other significant action on a
JTree instance the application may need to detect this event

There are two basic tree events

   •   Tree Selections -- selection of items at any tree level
   •   Tree Expansions -- Expanding or collapsing sub-branches of a tree




   •   Listening for tree selections
   •   Listening for tree expansions


Dave Marshall
4/14/1999

Next: Listening for tree expansions Up: Listening for tree actions Previous: Listening
for tree actions



Listening for tree selections

The TreeSelectionListener is responsible for handling events generated as a result of
the user making item selections.

The ActionTreeExample.java program:

   •   creates a list which uses can select items from




       ActionTreeExample.java Output
•   The program handles user selection events through the provision of the
       valueChanged() method -- the application extracts the current selection from the
       user and displays this information in the Java console display
   •   The valueChanged() method contains another Swing class named TreePath.
   •   The TreePath class is used to contain and describe all aspects of a path, including
       the string it contains and details about its parent and child relationships. For
       details on the TreePath class, see the Swing online documentation.

The code listing for ActionTreeExample.java is:

// Imports
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;

class ActionTreeExample
                extends        JFrame
                implements     TreeSelectionListener
 {
        // Instance attributes used in this example
        private JPanel         topPanel;
        private JTree          tree;
        private JScrollPane scrollPane;

         // Constructor of main frame
         public ActionTreeExample()
         {
                 // Set the frame characteristics
                 setTitle( "TreeSelectionListener Application" );
                 setSize( 300, 300 );
                 setBackground( Color.gray );

                  // Create a panel to hold all other components
                  topPanel = new JPanel();
                  topPanel.setLayout( new BorderLayout() );
                  getContentPane().add( topPanel );

                  // Create a new tree control
                  tree = new JTree();

                  // Add a selection listener
                  tree.addTreeSelectionListener( this );

                  // Add the listbox to a scrolling pane
                  scrollPane = new JScrollPane();
                  scrollPane.getViewport().add( tree );
                  topPanel.add( scrollPane, BorderLayout.CENTER );
         }

         // Handle tree item selections
         public void valueChanged( TreeSelectionEvent event )
         {
if( event.getSource() == tree )
               {
                       // Display the full selection path
                       TreePath path = tree.getSelectionPath();
                       System.out.println( "Selection path=" +
path.toString() );

                       // Get the text for the last component
                       System.out.println( "Selection="
                                                      +
path.getLastPathComponent() );
               }
        }

         // Main entry point for this example
         public static void main( String args[] )
         {
                 // Create an instance of the test application
                 ActionTreeExample mainFrame    = new ActionTreeExample();
                 mainFrame.setVisible( true );
         }
}




Dave Marshall
4/14/1999

Next: Practical Programming of JTrees Up: Listening for tree actions Previous:
Listening for tree selections



Listening for tree expansions

Events caused by the user expanding or collapsing a node can be intercepted in a tree
object.

The ExpandTreeExample.java example:

    •   implements the same default tree object as in the previous example with an
        attached expansion listener.
    •   When the user expands a tree branch, the treeExpanded() method is called,
        which displays the full path of the expanded node.
    •   In a similar fashion, when a node is collapsed, the treeCollapsed() method
        handles the event.

The full code listing for ExpandTreeExample.java is as follows:

// Imports
import   java.awt.*;
import   java.awt.event.*;
import   javax.swing.*;
import   javax.swing.event.*;
import   javax.swing.tree.*;

class ExpandTreeExample
                extends        JFrame
                implements     TreeExpansionListener
 {
        // Instance attributes used in this example
        private JPanel         topPanel;
        private JTree          tree;
        private JScrollPane scrollPane;

         // Constructor of main frame
         public ExpandTreeExample()
         {
                 // Set the frame charac


Next: Hiding the root node Up: Trees JTree Previous: Listening for tree expansions




Practical Programming of JTrees
There are many, more subtle aspects of the JTree component that we have not yet
discussed.

These features involve adding simple, one line or two line extensions to your code to
effect changes (in some cases, dramatic changes) in the character of the tree control.




   •   Hiding the root node
   •   Expanding and collapsing the tree
   •   Selecting and deselecting nodes


Dave Marshall
4/14/1999
http://www.cs.cf.ac.uk/Dave/HCI/HCI_Handout_CALLER/node160.html

Weitere ähnliche Inhalte

Was ist angesagt?

Drupal II: The SQL
Drupal II: The SQLDrupal II: The SQL
Drupal II: The SQLddiers
 
Xml generation and extraction using XMLDB
Xml generation and extraction using XMLDBXml generation and extraction using XMLDB
Xml generation and extraction using XMLDBpallavi kasibhotla
 
High performance JPA with Oracle Coherence
High performance JPA with Oracle CoherenceHigh performance JPA with Oracle Coherence
High performance JPA with Oracle CoherenceMarkus Eisele
 
PostgreSQL Modules Tutorial - chkpass, hstore, fuzzystrmach, isn
PostgreSQL Modules Tutorial - chkpass, hstore, fuzzystrmach, isn PostgreSQL Modules Tutorial - chkpass, hstore, fuzzystrmach, isn
PostgreSQL Modules Tutorial - chkpass, hstore, fuzzystrmach, isn Sagar Arlekar
 
Dependency Injection in Laravel
Dependency Injection in LaravelDependency Injection in Laravel
Dependency Injection in LaravelHAO-WEN ZHANG
 
Database & Technology 1 _ Tom Kyte _ SQL Techniques.pdf
Database & Technology 1 _ Tom Kyte _ SQL Techniques.pdfDatabase & Technology 1 _ Tom Kyte _ SQL Techniques.pdf
Database & Technology 1 _ Tom Kyte _ SQL Techniques.pdfInSync2011
 
Using grouping sets in sql server 2008 tech republic
Using grouping sets in sql server 2008   tech republicUsing grouping sets in sql server 2008   tech republic
Using grouping sets in sql server 2008 tech republicKaing Menglieng
 

Was ist angesagt? (9)

B13 Investigating oracle by Julian Dyke
B13 Investigating oracle by Julian DykeB13 Investigating oracle by Julian Dyke
B13 Investigating oracle by Julian Dyke
 
Drupal II: The SQL
Drupal II: The SQLDrupal II: The SQL
Drupal II: The SQL
 
R workshop
R workshopR workshop
R workshop
 
Xml generation and extraction using XMLDB
Xml generation and extraction using XMLDBXml generation and extraction using XMLDB
Xml generation and extraction using XMLDB
 
High performance JPA with Oracle Coherence
High performance JPA with Oracle CoherenceHigh performance JPA with Oracle Coherence
High performance JPA with Oracle Coherence
 
PostgreSQL Modules Tutorial - chkpass, hstore, fuzzystrmach, isn
PostgreSQL Modules Tutorial - chkpass, hstore, fuzzystrmach, isn PostgreSQL Modules Tutorial - chkpass, hstore, fuzzystrmach, isn
PostgreSQL Modules Tutorial - chkpass, hstore, fuzzystrmach, isn
 
Dependency Injection in Laravel
Dependency Injection in LaravelDependency Injection in Laravel
Dependency Injection in Laravel
 
Database & Technology 1 _ Tom Kyte _ SQL Techniques.pdf
Database & Technology 1 _ Tom Kyte _ SQL Techniques.pdfDatabase & Technology 1 _ Tom Kyte _ SQL Techniques.pdf
Database & Technology 1 _ Tom Kyte _ SQL Techniques.pdf
 
Using grouping sets in sql server 2008 tech republic
Using grouping sets in sql server 2008   tech republicUsing grouping sets in sql server 2008   tech republic
Using grouping sets in sql server 2008 tech republic
 

Andere mochten auch

Andere mochten auch (10)

AJP
AJPAJP
AJP
 
exception handling in java
exception handling in java exception handling in java
exception handling in java
 
Module 4
Module 4Module 4
Module 4
 
Unit 5 swing & TREES
Unit 5 swing & TREESUnit 5 swing & TREES
Unit 5 swing & TREES
 
sravan java Session1
sravan java Session1sravan java Session1
sravan java Session1
 
SRAVANByCPP
SRAVANByCPPSRAVANByCPP
SRAVANByCPP
 
Util
UtilUtil
Util
 
Iostreams
IostreamsIostreams
Iostreams
 
java packages
java packagesjava packages
java packages
 
Classroom Management Tips for Kids and Adolescents
Classroom Management Tips for Kids and AdolescentsClassroom Management Tips for Kids and Adolescents
Classroom Management Tips for Kids and Adolescents
 

Ähnlich wie Jtreenotes

Create an implementation of a binary tree using the recursive appr.pdf
Create an implementation of a binary tree using the recursive appr.pdfCreate an implementation of a binary tree using the recursive appr.pdf
Create an implementation of a binary tree using the recursive appr.pdffederaleyecare
 
Keep getting a null pointer exception for some odd reasonim creati.pdf
Keep getting a null pointer exception for some odd reasonim creati.pdfKeep getting a null pointer exception for some odd reasonim creati.pdf
Keep getting a null pointer exception for some odd reasonim creati.pdfAroraRajinder1
 
Advance Java Programs skeleton
Advance Java Programs skeletonAdvance Java Programs skeleton
Advance Java Programs skeletonIram Ramrajkar
 
javaFix in the program belowhandle incomplete data for text fil.pdf
javaFix in the program belowhandle incomplete data for text fil.pdfjavaFix in the program belowhandle incomplete data for text fil.pdf
javaFix in the program belowhandle incomplete data for text fil.pdfbirajdar2
 
4. The size of instructions can be fixed or variable. What are advant.pdf
4. The size of instructions can be fixed or variable. What are advant.pdf4. The size of instructions can be fixed or variable. What are advant.pdf
4. The size of instructions can be fixed or variable. What are advant.pdfmumnesh
 
.Net Project Portfolio for Roger Loving
.Net Project Portfolio for Roger Loving.Net Project Portfolio for Roger Loving
.Net Project Portfolio for Roger Lovingrloving10
 
import javaawt import javaxswing import javaxswing.pdf
import javaawt import javaxswing import javaxswing.pdfimport javaawt import javaxswing import javaxswing.pdf
import javaawt import javaxswing import javaxswing.pdfADITIEYEWEAR
 
Using Twig with Drupal 7
Using Twig with Drupal 7Using Twig with Drupal 7
Using Twig with Drupal 7Exove
 
Java Generics
Java GenericsJava Generics
Java Genericsjeslie
 
On the code which has a class in which I implementing a binary tree .pdf
On the code which has a class in which I implementing a binary tree .pdfOn the code which has a class in which I implementing a binary tree .pdf
On the code which has a class in which I implementing a binary tree .pdfwasemanivytreenrco51
 
Below binary program is usefell to you#include iostreamtem.pdf
Below binary program is usefell to you#include iostreamtem.pdfBelow binary program is usefell to you#include iostreamtem.pdf
Below binary program is usefell to you#include iostreamtem.pdfanandinternational01
 
Implementing a basic directory-tree structure that is derived from a.pdf
Implementing a basic directory-tree structure that is derived from a.pdfImplementing a basic directory-tree structure that is derived from a.pdf
Implementing a basic directory-tree structure that is derived from a.pdffunkybabyindia
 
We will be making 4 classes Main - for testing the code Hi.pdf
 We will be making 4 classes Main - for testing the code Hi.pdf We will be making 4 classes Main - for testing the code Hi.pdf
We will be making 4 classes Main - for testing the code Hi.pdfanithareadymade
 

Ähnlich wie Jtreenotes (20)

JavaAssignment6
JavaAssignment6JavaAssignment6
JavaAssignment6
 
Create an implementation of a binary tree using the recursive appr.pdf
Create an implementation of a binary tree using the recursive appr.pdfCreate an implementation of a binary tree using the recursive appr.pdf
Create an implementation of a binary tree using the recursive appr.pdf
 
Keep getting a null pointer exception for some odd reasonim creati.pdf
Keep getting a null pointer exception for some odd reasonim creati.pdfKeep getting a null pointer exception for some odd reasonim creati.pdf
Keep getting a null pointer exception for some odd reasonim creati.pdf
 
Advance Java Programs skeleton
Advance Java Programs skeletonAdvance Java Programs skeleton
Advance Java Programs skeleton
 
javaFix in the program belowhandle incomplete data for text fil.pdf
javaFix in the program belowhandle incomplete data for text fil.pdfjavaFix in the program belowhandle incomplete data for text fil.pdf
javaFix in the program belowhandle incomplete data for text fil.pdf
 
4. The size of instructions can be fixed or variable. What are advant.pdf
4. The size of instructions can be fixed or variable. What are advant.pdf4. The size of instructions can be fixed or variable. What are advant.pdf
4. The size of instructions can be fixed or variable. What are advant.pdf
 
Unittests für Dummies
Unittests für DummiesUnittests für Dummies
Unittests für Dummies
 
.Net Project Portfolio for Roger Loving
.Net Project Portfolio for Roger Loving.Net Project Portfolio for Roger Loving
.Net Project Portfolio for Roger Loving
 
import javaawt import javaxswing import javaxswing.pdf
import javaawt import javaxswing import javaxswing.pdfimport javaawt import javaxswing import javaxswing.pdf
import javaawt import javaxswing import javaxswing.pdf
 
Using Twig with Drupal 7
Using Twig with Drupal 7Using Twig with Drupal 7
Using Twig with Drupal 7
 
Java Generics
Java GenericsJava Generics
Java Generics
 
On the code which has a class in which I implementing a binary tree .pdf
On the code which has a class in which I implementing a binary tree .pdfOn the code which has a class in which I implementing a binary tree .pdf
On the code which has a class in which I implementing a binary tree .pdf
 
Below binary program is usefell to you#include iostreamtem.pdf
Below binary program is usefell to you#include iostreamtem.pdfBelow binary program is usefell to you#include iostreamtem.pdf
Below binary program is usefell to you#include iostreamtem.pdf
 
Maze
MazeMaze
Maze
 
Implementing a basic directory-tree structure that is derived from a.pdf
Implementing a basic directory-tree structure that is derived from a.pdfImplementing a basic directory-tree structure that is derived from a.pdf
Implementing a basic directory-tree structure that is derived from a.pdf
 
Xml & Java
Xml & JavaXml & Java
Xml & Java
 
Android Data Storagefinal
Android Data StoragefinalAndroid Data Storagefinal
Android Data Storagefinal
 
srgoc
srgocsrgoc
srgoc
 
Chtp412
Chtp412Chtp412
Chtp412
 
We will be making 4 classes Main - for testing the code Hi.pdf
 We will be making 4 classes Main - for testing the code Hi.pdf We will be making 4 classes Main - for testing the code Hi.pdf
We will be making 4 classes Main - for testing the code Hi.pdf
 

Kürzlich hochgeladen

Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxMusic 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxleah joy valeriano
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 

Kürzlich hochgeladen (20)

Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxMusic 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 

Jtreenotes

  • 1. Next: Practical Usage of Tress Up: Trees JTree Previous: Trees JTree List v. Trees Lists are good for displaying simple lists of information from which the user can make single or multiple selections. A list is inadequate for more complex selections, for example: • Selecting items in a structured hierarchy, • If you were writing a program containing several general selection topics • Each of these topics could be subdivided into many specific subtopics, • You would require a list of subtopics for each general topic. • These numerous list boxes consume vast amounts of GUI space and would be hard to read and assimilate. • Huge amount of code you would be needed to support this also -- For four or five separate lists, you could still manage a scenario like this, but what if you had a hundred general topics? A better solution to this problem is to implement a single control containing all of the lists: • Simplified Interface • Reduced amount code YOU write.
  • 2. Example of a Tree Structure A hierarchical tree realises this structure: • It can nest lists of lists to any depth required. • Tree control via a series of collapsible/ expandable branches it can contain. • Each of the tree's branches can be divided into successively more specific branches • There is no practical limit to the number of items that each branch can hold, nor to the nesting of subbranches. The JTree class is the Swing component that provides these facilities. Next: Practical Usage of Tress Up: Trees JTree Previous: Trees JTree Dave Marshall 4/14/1999 Next: A basic JTree example Up: Trees JTree Previous: List v. Trees Practical Usage of Tress in Interfaces
  • 3. Novice software users may experience problems working with hierarchical data structures: • If you are designing for a more sophisticated user, this is not an issue. • For the average user, try to avoid using a tree control that doesn't already strongly conform to an existing UI metaphor, Windows/Motif users should be familiar with Trees for managing file directories: o This may mean using a tree in combination with a list to display lowest level tree nodes (for example, Windows Explorer). • Trees have the unfortunate side effect of encouraging the creation of deep nested structures that become difficult for the user to navigate and manage. Dave Marshall 4/14/1999 Next: Adding new tree items Up: Trees JTree Previous: Practical Usage of Tress A basic JTree example Let us demonstrate a simple creation of a Tree application, TreeExample.java: • we will create a simple tree (Fig ) containing only a few items and having no ability to add or remove items. • we will include a scrollable pane, which will become more important as we add features to our application. • The icons shown in this example are defaults provided by Swing. • We do not actually insert an items in the tree. When no specific data model has been specified, JTree reverts to a default example model which is impractical but useful for this demonstration. • When the program is executed, notice that the tree is initially collapsed down to the root level. • Opening the root expands it, showing the next level of indentation -- Notice the change in icon for the lowest element of the tree. • This is the default operation for the simple tree. The full code listing for TreeExample.java is as follows: // Imports import java.awt.*; import java.awt.event.*; import javax.swing.*; class TreeExample extends JFrame
  • 4. { // Instance attributes used in this example private JPanel topPanel; private JTree tree; private JScrollPane scrollPane; // Constructor of main frame public TreeExample() { // Set the frame characteristics setTitle( "Simple Tree Application" ); setSize( 300, 100 ); setBackground( Color.gray ); // Create a panel to hold all other components topPanel = new JPanel(); topPanel.setLayout( new BorderLayout() ); getContentPane().add( topPanel ); // Create a new tree control tree = new JTree(); // Add the listbox to a scrolling pane scrollPane = new JScrollPane(); scrollPane.getViewport().add( tree ); topPanel.add( scrollPane, BorderLayout.CENTER ); } // Main entry point for this example public static void main( String args[] ) { // Create an instance of the test application TreeExample mainFrame = new TreeExample(); mainFrame.setVisible( true ); } } Dave Marshall 4/14/1999 Next: Custom data models Up: Trees JTree Previous: A basic JTree example Adding new tree items Let us now look at some code, AddTreeExample.java that creates our own Tree items: Adding branches and items to a tree instance involves a bit of additional code.
  • 5. AddTreeExample.java generates a JTree instance containing all of the cards in a deck of playing cards: AddTreeExample.java Program Output • The key feature in this application is the use of the DefaultMutableTreeNode class. • The DefaultMutableTreeNode is a general-purpose node item compatible with the Swing graphical tree component. • Each child node is inserted into its parent node using the add() method • any child node can itself act as a parent for subsequent child nodes. • The root is a special node because it is the ultimate parent of all of the other tree nodes -- inserted into the tree's data model, an instance of the DefaultTreeModel class. • This is the model that a JTree instance should use, unless a custom data model is used. The full code listing for AddTreeExample.java is as follows: // Imports import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.tree.*; class AddTreeExample extends JFrame { // Instance attributes used in this example private JPanel topPanel; private JTree tree; private JScrollPane scrollPane; // Constructor of main frame public AddTreeExample()
  • 6. { // Set the frame characteristics setTitle( "More Advanced Tree Application" ); setSize( 300, 200 ); setBackground( Color.gray ); // Create a panel to hold all other components topPanel = new JPanel(); topPanel.setLayout( new BorderLayout() ); getContentPane().add( topPanel ); // Create data for the tree DefaultMutableTreeNode root = new DefaultMutableTreeNode( "Deck" ); DefaultMutableTreeNode itemClubs = new DefaultMutableTreeNode( "Clubs" ); addAllCard( itemClubs ); root.add( itemClubs ); DefaultMutableTreeNode itemDiamonds = new DefaultMutableTreeNode( "Diamonds" ); addAllCard( itemDiamonds ); root.add( itemDiamonds ); DefaultMutableTreeNode itemSpades = new DefaultMutableTreeNode( "Spades" ); addAllCard( itemSpades ); root.add( itemSpades ); DefaultMutableTreeNode itemHearts = new DefaultMutableTreeNode( "Hearts" ); addAllCard( itemHearts ); root.add( itemHearts ); // Create a new tree control DefaultTreeModel treeModel = new DefaultTreeModel( root ); tree = new JTree( treeModel ); // Add the listbox to a scrolling pane scrollPane = new JScrollPane(); scrollPane.getViewport().add( tree ); topPanel.add( scrollPane, BorderLayout.CENTER ); } // Helper method to write an enitre suit of cards to the // current tree node public void addAllCard( DefaultMutableTreeNode suit ) { suit.add( new DefaultMutableTreeNode( "Ace" ) );
  • 7. suit.add( new DefaultMutableTreeNode( "Two" ) ); suit.add( new DefaultMutableTreeNode( "Three" ) ); suit.add( new DefaultMutableTreeNode( "Four" ) ); suit.add( new DefaultMutableTreeNode( "Five" ) ); suit.add( new DefaultMutableTreeNode( "Six" ) ); suit.add( new DefaultMutableTreeNode( "Seven" ) ); suit.add( new DefaultMutableTreeNode( "Eight" ) ); suit.add( new DefaultMutableTreeNode( "Nine" ) ); suit.add( new DefaultMutableTreeNode( "Ten" ) ); suit.add( new DefaultMutableTreeNode( "Jack" ) ); suit.add( new DefaultMutableTreeNode( "Queen" ) ); suit.add( new DefaultMutableTreeNode( "King" ) ); } // Main entry point for this example public static void main( String args[] ) { // Create an instance of the test application AddTreeExample mainFrame = new AddTreeExample(); mainFrame.setVisible( true ); } } Dave Marshall 4/14/1999 Next: Custom rendering Up: Trees JTree Previous: Adding new tree items Custom data models The JTree class (like Jlist) supports custom data modeling: • slightly more complex here due to hierarchical nature of a tree The CustomTreeExample.java example, we will recreate the tree containing the deck of cards. However, • a custom data model is used to manufacture the cards within each suit. • The result is an application consisting of approximately the same number of code lines, but with much better performance, particularly for large data sets. • Graphical Appearance to User is the same. As in the Custom Data Model List example we have two classes: • CustomTreeExample -- the main class
  • 8. MyDataModel -- which creates the custom data model by extending the DefaultTreModel. The code listing for CustomTreeExample.java is: // Imports import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.tree.*; class CustomTreeExample extends JFrame { // Instance attributes used in this example private JPanel topPanel; private JTree tree; private JScrollPane scrollPane; // Constructor of main frame public CustomTreeExample() { // Set the frame characteristics setTitle( "Custom Data Model Tree Application" ); setSize( 300, 200 ); setBackground( Color.gray ); // Create a panel to hold all other components topPanel = new JPanel(); topPanel.setLayout( new BorderLayout() ); getContentPane().add( topPanel ); // Create data for the tree DefaultMutableTreeNode root = new DefaultMutableTreeNode( "Deck" ); CreateSuit( root, "Clubs" ); CreateSuit( root, "Diamonds" ); CreateSuit( root, "Spades" ); CreateSuit( root, "Hearts" ); // Create a new tree control MyDataModel treeModel = new MyDataModel( root ); tree = new JTree( treeModel ); // Add the listbox to a scrolling pane scrollPane = new JScrollPane(); scrollPane.getViewport().add( tree ); topPanel.add( scrollPane, BorderLayout.CENTER ); } public void CreateSuit( DefaultMutableTreeNode root, String suitName ) {
  • 9. DefaultMutableTreeNode itemSuit = new DefaultMutableTreeNode( suitName ); for( int iCtr = 0; iCtr < 13; iCtr++ ) itemSuit.add( new DefaultMutableTreeNode( "" ) ); root.add( itemSuit ); } // Main entry point for this example public static void main( String args[] ) { // Create an instance of the test application CustomTreeExample mainFrame = new CustomTreeExample(); mainFrame.setVisible( true ); } } The code listing for MyDataModel.java is: // Imports import java.util.*; import javax.swing.*; import javax.swing.tree.*; class MyDataModel extends DefaultTreeModel { private DefaultMutableTreeNode root; private String rootName = ""; private String cardArray[] = { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" }; public MyDataModel( TreeNode root ) { super( root ); DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode)root; rootName = (String)parentNode.getUserObject(); } public Object getChild( Object parent, int index ) { DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode)parent; String parentName = (String)parentNode.getUserObject(); if( parentName.equals( rootName ) )
  • 10. return super.getChild( parent, index ); else return new DefaultMutableTreeNode( cardArray[index] ); } } Dave Marshall 4/14/1999 Next: Editing tree nodes Up: Trees JTree Previous: Custom data models Custom rendering In addition to handling custom data modeling, the JTree class also supports custom rendering of individual items within the list. This is useful for adding graphics or changing the font of specific items in the tree. The example extends the playing card tree example by implementing a custom renderer class to handle some special features: • The top-level items (the suits) include graphics for each of the suits in a deck of cards, • The individual cards also include graphics. Custom Tree Rendering Example
  • 11. As in the JList rendering example we have two classes: • RenderTreeExample is almost identical to the main Tree classes shown in previous examples -- it t creates a tree instance and inserts all of the pertinent data. It has one additional line to notify the tree that we will provide a custom renderer. • CustomCellRenderer -- contains the rendering code for this application. o To save time during execution, the class implements a constructor to load the graphics. o The getTreeCellRendererComponent() method must be provided as a requirement of the TreeCellRender implementation used by this class is responsible for the actual drawing of tree cells. In this example, getTreeCellRendererComponent() • determines what it is drawing (based on the text in the specified tree node) and then assigns the correct image to the displayed items. • then sets the correct foreground color, depending on the selection state of the item. • A paint() method is required within the custom rendering class to work around a peculiar feature (yes a BUG) in Swing to set the item background. The code listing for RenderTreeExample.java is: // Imports import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.tree.*; class RenderTreeExample extends JFrame { // Instance attributes used in this example private JPanel topPanel; private JTree tree; private JScrollPane scrollPane; // Constructor of main frame public RenderTreeExample() { // Set the frame characteristics setTitle( "Custom Rendered Tree Application" ); setSize( 300, 200 ); setBackground( Color.gray ); // Create a panel to hold all other components topPanel = new JPanel(); topPanel.setLayout( new BorderLayout() ); getContentPane().add( topPanel );
  • 12. // Create data for the tree DefaultMutableTreeNode root = new DefaultMutableTreeNode( "Deck" ); DefaultMutableTreeNode itemClubs = new DefaultMutableTreeNode( "Clubs" ); addAllCard( itemClubs ); root.add( itemClubs ); DefaultMutableTreeNode itemDiamonds = new DefaultMutableTreeNode( "Diamonds" ); addAllCard( itemDiamonds ); root.add( itemDiamonds ); DefaultMutableTreeNode itemSpades = new DefaultMutableTreeNode( "Spades" ); addAllCard( itemSpades ); root.add( itemSpades ); DefaultMutableTreeNode itemHearts = new DefaultMutableTreeNode( "Hearts" ); addAllCard( itemHearts ); root.add( itemHearts ); // Create a new tree control DefaultTreeModel treeModel = new DefaultTreeModel( root ); tree = new JTree( treeModel ); // Tell the tree it is being rendered by our application tree.setCellRenderer( new CustomCellRenderer() ); // Add the listbox to a scrolling pane scrollPane = new JScrollPane(); scrollPane.getViewport().add( tree ); topPanel.add( scrollPane, BorderLayout.CENTER ); } // Helper method to write an enitre suit of cards to the // current tree node public void addAllCard( DefaultMutableTreeNode suit ) { suit.add( new DefaultMutableTreeNode( "Ace" ) ); suit.add( new DefaultMutableTreeNode( "Two" ) ); suit.add( new DefaultMutableTreeNode( "Three" ) ); suit.add( new DefaultMutableTreeNode( "Four" ) ); suit.add( new DefaultMutableTreeNode( "Five" ) ); suit.add( new DefaultMutableTreeNode( "Six" ) ); suit.add( new DefaultMutableTreeNode( "Seven" ) ); suit.add( new DefaultMutableTreeNode( "Eight" ) ); suit.add( new DefaultMutableTreeNode( "Nine" ) ); suit.add( new DefaultMutableTreeNode( "Ten" ) );
  • 13. suit.add( new DefaultMutableTreeNode( "Jack" ) ); suit.add( new DefaultMutableTreeNode( "Queen" ) ); suit.add( new DefaultMutableTreeNode( "King" ) ); } // Main entry point for this example public static void main( String args[] ) { // Create an instance of the test application RenderTreeExample mainFrame = new RenderTreeExample(); mainFrame.setVisible( true ); } } The code listing for CustomCellRenderer.java is: // Imports import javax.swing.*; import javax.swing.tree.*; import java.awt.*; public class CustomCellRenderer extends JLabel implements TreeCellRenderer { private ImageIcon deckImage; private ImageIcon[] suitImages; private ImageIcon[] cardImages; private boolean bSelected; public CustomCellRenderer() { // Load the images deckImage = new ImageIcon( "deck.gif" ); suitImages = new ImageIcon[4]; suitImages[0] = new ImageIcon( "clubs.gif" ); suitImages[1] = new ImageIcon( "diamonds.gif" ); suitImages[2] = new ImageIcon( "spades.gif" ); suitImages[3] = new ImageIcon( "hearts.gif" ); cardImages = new ImageIcon[13]; cardImages[0] = new ImageIcon( "ace.gif" ); cardImages[1] = new ImageIcon( "two.gif" ); cardImages[2] = new ImageIcon( "three.gif" ); cardImages[3] = new ImageIcon( "four.gif" ); cardImages[4] = new ImageIcon( "five.gif" ); cardImages[5] = new ImageIcon( "six.gif" ); cardImages[6] = new ImageIcon( "seven.gif" ); cardImages[7] = new ImageIcon( "eight.gif" ); cardImages[8] = new ImageIcon( "nine.gif" ); cardImages[9] = new ImageIcon( "ten.gif" ); cardImages[10] = new ImageIcon( "jack.gif" ); cardImages[11] = new ImageIcon( "queen.gif" ); cardImages[12] = new ImageIcon( "king.gif" );
  • 14. } public Component getTreeCellRendererComponent( JTree tree, Object value, boolean bSelected, boolean bExpanded, boolean bLeaf, int iRow, boolean bHasFocus ) { // Find out which node we are rendering and get its text DefaultMutableTreeNode node = (DefaultMutableTreeNode)value; String labelText = (String)node.getUserObject(); this.bSelected = bSelected; // Set the correct foreground color if( !bSelected ) setForeground( Color.black ); else setForeground( Color.white ); // Determine the correct icon to display if( labelText.equals( "Deck" ) ) setIcon( deckImage ); else if( labelText.equals( "Clubs" ) ) setIcon( suitImages[0] ); else if( labelText.equals( "Diamonds" ) ) setIcon( suitImages[1] ); else if( labelText.equals( "Spades" ) ) setIcon( suitImages[2] ); else if( labelText.equals( "Hearts" ) ) setIcon( suitImages[3] ); else if( labelText.equals( "Ace" ) ) setIcon( cardImages[0] ); else if( labelText.equals( "Two" ) ) setIcon( cardImages[1] ); else if( labelText.equals( "Three" ) ) setIcon( cardImages[2] ); else if( labelText.equals( "Four" ) ) setIcon( cardImages[3] ); else if( labelText.equals( "Five" ) ) setIcon( cardImages[4] ); else if( labelText.equals( "Six" ) ) setIcon( cardImages[5] ); else if( labelText.equals( "Seven" ) ) setIcon( cardImages[6] ); else if( labelText.equals( "Eight" ) ) setIcon( cardImages[7] ); else if( labelText.equals( "Nine" ) ) setIcon( cardImages[8] ); else if( labelText.equals( "Ten" ) ) setIcon( cardImages[9] ); else if( labelText.equals( "Jack" ) ) setIcon( cardImages[10] ); else if( labelText.equals( "Queen" ) ) setIcon( cardImages[11] ); else if( labelText.equals( "King" ) )
  • 15. setIcon( cardImages[12] ); // Add the text to the cell setText( labelText ); return this; } // This is a hack to paint the background. Normally a JLabel can // paint its own background, but due to an apparent bug or // limitation in the TreeCellRenderer, the paint method is // required to handle this. public void paint( Graphics g ) { Color bColor; Icon currentI = getIcon(); // Set the correct background color bColor = bSelected ? SystemColor.textHighlight : Color.white; g.setColor( bColor ); // Draw a rectangle in the background of the cell g.fillRect( 0, 0, getWidth() - 1, getHeight() - 1 ); super.paint( g ); } } Next: Editing tree nodes Up: Trees JTree Previous: Custom data models Dave Marshall 4/14/1999 Next: Listening for tree actions Up: Trees JTree Previous: Custom rendering Editing tree nodes In a manner similar to custom rendering, we can also perform custom editing of any item in a tree control. JTree supports three techniques for editing tree nodes. • The easiest is to simply call the setEditable(value) method and set the value to be true In this mode, the tree values are editable using a simple text editor.
  • 16. The second technique is to create a real custom editor for the tree, and this is what we will do in the EditTreeExample.java example below. • The third technique for tree editing is to replace the DefaultCellEditor() reference in EditTreeExample.java with a class that implements CellEditor. Refer to the online Swing documentation for details about the CellEditor implementation. Let us now look at the EditTreeExample.java example where we create a tree instance and set the default cell editor to point to a combo box which allows node changes only from the list of selections as shown below: EditTreeExample.java Output The full code listing for EditTreeExample.java is given below: // Imports import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; class EditTreeExample extends JFrame { // Instance attributes used in this example private JPanel topPanel; private JTree tree; private JScrollPane scrollPane; // Constructor of main frame public EditTreeExample() {
  • 17. // Set the frame characteristics setTitle( "Editable Tree Application" ); setSize( 300, 100 ); setBackground( Color.gray ); // Create a panel to hold all other components topPanel = new JPanel(); topPanel.setLayout( new BorderLayout() ); getContentPane().add( topPanel ); // Create a new tree control tree = new JTree(); tree.setEditable( true ); tree.setShowsRootHandles( false ); // Create a combo box of editing options JComboBox box = new JComboBox(); box.setMinimumSize( new Dimension( 100, 40 ) ); box.addItem( "Swing" ); box.addItem( "Java" ); box.addItem( "neat" ); box.addItem( "funky" ); box.addItem( "life" ); box.addItem( "awesome" ); box.addItem( "cool!" ); // Add a cell editor to the tree tree.setCellEditor( new DefaultCellEditor( box ) ); // Add the listbox to a scrolling pane scrollPane = new JScrollPane(); scrollPane.getViewport().add( tree ); topPanel.add( scrollPane, BorderLayout.CENTER ); } // Main entry point for this example public static void main( String args[] ) { // Create an instance of the test application EditTreeExample mainFrame = new EditTreeExample(); mainFrame.setVisible( true ); } } Dave Marshall 4/14/1999 Next: Listening for tree selections Up: Trees JTree Previous: Editing tree nodes
  • 18. Listening for tree actions When the user actually makes a selection or performs some other significant action on a JTree instance the application may need to detect this event There are two basic tree events • Tree Selections -- selection of items at any tree level • Tree Expansions -- Expanding or collapsing sub-branches of a tree • Listening for tree selections • Listening for tree expansions Dave Marshall 4/14/1999 Next: Listening for tree expansions Up: Listening for tree actions Previous: Listening for tree actions Listening for tree selections The TreeSelectionListener is responsible for handling events generated as a result of the user making item selections. The ActionTreeExample.java program: • creates a list which uses can select items from ActionTreeExample.java Output
  • 19. The program handles user selection events through the provision of the valueChanged() method -- the application extracts the current selection from the user and displays this information in the Java console display • The valueChanged() method contains another Swing class named TreePath. • The TreePath class is used to contain and describe all aspects of a path, including the string it contains and details about its parent and child relationships. For details on the TreePath class, see the Swing online documentation. The code listing for ActionTreeExample.java is: // Imports import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.tree.*; class ActionTreeExample extends JFrame implements TreeSelectionListener { // Instance attributes used in this example private JPanel topPanel; private JTree tree; private JScrollPane scrollPane; // Constructor of main frame public ActionTreeExample() { // Set the frame characteristics setTitle( "TreeSelectionListener Application" ); setSize( 300, 300 ); setBackground( Color.gray ); // Create a panel to hold all other components topPanel = new JPanel(); topPanel.setLayout( new BorderLayout() ); getContentPane().add( topPanel ); // Create a new tree control tree = new JTree(); // Add a selection listener tree.addTreeSelectionListener( this ); // Add the listbox to a scrolling pane scrollPane = new JScrollPane(); scrollPane.getViewport().add( tree ); topPanel.add( scrollPane, BorderLayout.CENTER ); } // Handle tree item selections public void valueChanged( TreeSelectionEvent event ) {
  • 20. if( event.getSource() == tree ) { // Display the full selection path TreePath path = tree.getSelectionPath(); System.out.println( "Selection path=" + path.toString() ); // Get the text for the last component System.out.println( "Selection=" + path.getLastPathComponent() ); } } // Main entry point for this example public static void main( String args[] ) { // Create an instance of the test application ActionTreeExample mainFrame = new ActionTreeExample(); mainFrame.setVisible( true ); } } Dave Marshall 4/14/1999 Next: Practical Programming of JTrees Up: Listening for tree actions Previous: Listening for tree selections Listening for tree expansions Events caused by the user expanding or collapsing a node can be intercepted in a tree object. The ExpandTreeExample.java example: • implements the same default tree object as in the previous example with an attached expansion listener. • When the user expands a tree branch, the treeExpanded() method is called, which displays the full path of the expanded node. • In a similar fashion, when a node is collapsed, the treeCollapsed() method handles the event. The full code listing for ExpandTreeExample.java is as follows: // Imports
  • 21. import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.tree.*; class ExpandTreeExample extends JFrame implements TreeExpansionListener { // Instance attributes used in this example private JPanel topPanel; private JTree tree; private JScrollPane scrollPane; // Constructor of main frame public ExpandTreeExample() { // Set the frame charac Next: Hiding the root node Up: Trees JTree Previous: Listening for tree expansions Practical Programming of JTrees There are many, more subtle aspects of the JTree component that we have not yet discussed. These features involve adding simple, one line or two line extensions to your code to effect changes (in some cases, dramatic changes) in the character of the tree control. • Hiding the root node • Expanding and collapsing the tree • Selecting and deselecting nodes Dave Marshall 4/14/1999 http://www.cs.cf.ac.uk/Dave/HCI/HCI_Handout_CALLER/node160.html