HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Climbing a JTree"
Volume: 4 Issue: 4, p. 24

	

Listing 1. adddata_examplea.java
 
import javax.swing.*; 
import javax.swing.tree.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.util.*; 

public class AddData_ExampleA 
{ 
  private DefaultMutableTreeNode  i_carNode; 
  private DefaultTreeModel        i_model; 
  private JTree                   i_tree; 
  private JButton                 i_add; 
  private JButton                 i_remove; 
  private JButton                 i_change; 
  private Vehicle                 i_car; 
  
  public AddData_ExampleA( JFrame frm ) 
  { 
    //Create the root 
    Vehicle vObj = new Vehicle( 
                       "Transportation Vehicles"); 
    DefaultMutableTreeNode tRoot = 
      new DefaultMutableTreeNode(vObj, true); 
    i_model = new DefaultTreeModel( tRoot ); 
    //Create the tree using the root. 
    i_tree = new JTree( i_model ); 

    //Used to give desireable look to the JTree 
    i_tree.putClientProperty("JTree.lineStyle", "Angled"); 

    i_tree.setShowsRootHandles( true ); 
    frm.getContentPane().add(i_tree, 
      BorderLayout.CENTER ); 

    i_add = new JButton("Add Node: 'Car'"); 
    AddDataActionListener al = 
      new AddDataActionListener(); 
    i_add.addActionListener( al ); 

    i_remove = new JButton( "Remove Node: 'Car'"); 
    i_remove.addActionListener( al ); 
    i_change = new JButton("Change Name to 'Van'"); 
    i_change.addActionListener( al ); 

    JPanel buttonPanel = 
      new JPanel( new FlowLayout() ); 
    buttonPanel.add( i_add ); 
    buttonPanel.add( i_remove ); 
    buttonPanel.add( i_change ); 
    frm.getContentPane().add( 
      buttonPanel, BorderLayout.SOUTH ); 
  } 

  public class AddDataActionListener 
    implements ActionListener 
  { 
    public void actionPerformed( ActionEvent aeV ) 
    { 
     Object source = aeV.getSource(); 
     if( source == i_add ) { 
        if( i_carNode == null ) { 
          i_car = new Vehicle( "Car" ); 
          i_carNode = new DefaultMutableTreeNode( 
            i_car ); 

          //Using the DefaultTreeModel's 
          //insertNodeInto() method. 
          i_model.insertNodeInto( i_carNode, 
            (MutableTreeNode)i_model.getRoot(),0); 

          //method is used to expose the new node 
          //that was just added. 
          i_tree.makeVisible( new TreePath( 
            i_model.getPathToRoot( i_carNode )) ); 
        } 
      } 
      else if( (source == i_remove ) 
        && (i_carNode != null) ) { 
        i_model.removeNodeFromParent( i_carNode ); 
        i_carNode = null; 
      } 
      else if ( (source == i_change) 
        && (i_carNode != null) ) { 
        i_car.setName("Van"); 
        TreeNode[] nodesToRoot = 
          i_model.getPathToRoot( i_carNode ); 
        TreePath pathToRoot = 
          new TreePath( nodesToRoot ); 
        i_model.valueForPathChanged( 
          pathToRoot, i_car ); 
      } 
    } 
  } 

  public static void main(String[] args) { 
    JFrame f = new JFrame( 
      "Add data to the JTree - Example A"); 
    f.getContentPane().setLayout( 
      new BorderLayout() ); 
    f.addWindowListener(new WindowAdapter() { 
      public void windowClosing( WindowEvent we ) 
      { 
       System.exit(0); 
      } 
    }); 
    AddData_ExampleA example = 
      new AddData_ExampleA( f ); 
    f.pack(); 
    f.setSize( 
      new Dimension( f.getSize().width, 400 ) ); 
    f.setLocation( 200, 100 ); 
    f.setVisible(true); 
  } 
} 

Listing 2. vehicle.java
 
import java.util.*; 

public class Vehicle implements UpdateEventSource 
{ 
  String    i_name; 
  String    i_type; 
  String    i_description; 
  Vector    i_subTypes; 
  Vector    i_listeners; 

  public Vehicle(String name) 
  { 
    i_subTypes = new Vector(); 
    i_listeners = new Vector(); 
    i_name = name; 
  } 

  public void setName( String name ) 
  { 
    i_name = name; 
    fireUpdateEvent( new UpdateEvent(this,this) ); 
  } 

  public String getName() 
  { 
    return( i_name ); 
  } 

  public void setType( String type ) 
  { 
    i_type = type; 
  } 

  public String getType() 
  { 
    return( i_type ); 
  } 

  public void setDescription( String desc ) 
  { 
    i_description = desc; 
  } 

  public String getDescription() 
  { 
    return( i_description ); 
  } 

  //Using a vector for this example, could be any 
  //means of data retrieval like SQL statements, 
  //CORBA calls, etc 
  public Vector getSubTypes() 
  { 
    return( i_subTypes ); 
  } 
  
  public void addSubType( Vehicle type ) 
  { 
    i_subTypes.addElement( type ); 
  } 

  public void removeSubType( Vehicle type ) 
  { 
    i_subTypes.removeElement( type ); 
  } 

  /*********************************************** 
  * NOTE: It is good practice to override this   * 
  * method from Object. The JTree uses this to   * 
  * determine how to display text in a TreeNode  * 
  * cell.                                        * 
  *                                              * 
  * @param void                                  * 
  *                                              * 
  * @return String Contains the components       * 
  *                                              * 
  ***********************************************/ 
  public String toString() 
  { 
    return( getName() ); 
  } 

  public void fireUpdateEvent(UpdateEvent update) 
  { 
    Enumeration enumListeners = 
      i_listeners.elements(); 
    while( enumListeners.hasMoreElements() ) 
    { 
      Object nextListener = 
        enumListeners.nextElement(); 
      if( nextListener instanceof 
        UpdateEventListener ) 
      { 
        ((UpdateEventListener)nextListener). 
          valueUpdated( update ); 
      } 
    } 
  } 

  public void addUpdateEventListener( 
    UpdateEventListener listener ) 
  { 
    if( !(i_listeners.contains( listener )) ) 
    { 
      i_listeners.addElement( listener ); 
    } 
  } 

  public void removeUpdateEventListener( 
    UpdateEventListener listener ) 
  { 
    i_listeners.removeElement( listener ); 
  } 
} 

Listing 3. vehicletreenode.java
 
import javax.swing.*; 
import javax.swing.tree.*; 
import java.awt.*; 
import java.awt.event.AdjustmentEvent; 
import java.util.*; 

public class VehicleTreeNode 
  implements MutableTreeNode, UpdateEventListener, 
    UpdateEventSource 
{ 
  private Vector          i_listeners; 
  private Vector          i_myChildren; 
  private boolean         i_allowChildren; 
  private Vehicle         i_vehicle; 
  private MutableTreeNode i_parent; 

  public VehicleTreeNode( Vehicle userObj, boolean allowChildren ) 
  { 
    i_myChildren = new Vector(); 
    i_allowChildren = allowChildren; 
    i_listeners = new Vector(); 
    if( userObj instanceof Vehicle ) 
    { 
      i_vehicle = (Vehicle)userObj; 
      i_vehicle.addUpdateEventListener( this ); 
      Enumeration enumSubTypes = 
        i_vehicle.getSubTypes().elements(); 
      while( enumSubTypes.hasMoreElements() ){ 
        Vehicle nextVehicle = 
          (Vehicle)enumSubTypes.nextElement(); 
        i_myChildren.addElement( 
          new VehicleTreeNode( nextVehicle, 
            allowChildren ) ); 
      } 
    } 
  } 

  public void valueUpdated( UpdateEvent update ) 
  { 
   if( update.getSource() instanceof Vehicle ) 
    { 
      fireUpdateEvent( update ); 
    } 
  } 

  public void fireUpdateEvent(UpdateEvent update) 
  { 
    UpdateEvent event = new UpdateEvent( 
      this, update.getUpdateValue() ); 
    Enumeration enumListeners = 
      i_listeners.elements(); 
   while( enumListeners.hasMoreElements() ) 
    { 
      Object nextListener = 
        enumListeners.nextElement(); 
      if( nextListener instanceof 
        UpdateEventListener ) 
      { 
        ((UpdateEventListener)nextListener). 
          valueUpdated( event ); 
      } 
    } 
  } 

  public void addUpdateEventListener( 
    UpdateEventListener listener ) 
  { 
   if( !(i_listeners.contains( listener )) ) 
    { 
      i_listeners.addElement( listener ); 
    } 
  } 

  public void removeUpdateEventListener( 
    UpdateEventListener listener ) 
  { 
    i_listeners.removeElement( listener ); 
  } 

  //Methods to satisfy the TreeNode interface 

  //Returns an enumeration of the children 
  public Enumeration children() 
  { 
    return( i_myChildren.elements() ); 
  } 

  //Returns true if the receiver allows children. 
  public boolean getAllowsChildren() 
  { 
    return( i_allowChildren ); 
  } 

  //Returns the child TreeNode at index. 
  public TreeNode getChildAt( int index ) 
  { 
    MutableTreeNode node = (VehicleTreeNode) 
            i_myChildren.elementAt( index ); 
    node.setParent( this ); 
    return( node ); 
  } 

  //Returns the number of children. 
  public int getChildCount() 
  { 
    return( i_myChildren.size() ); 
  } 

  //Returns the index of node. 
  public int getIndex(TreeNode node) 
  { 
    return( i_myChildren.indexOf( node ) ); 
  } 

  //Returns the parent TreeNode of the receiver. 
  public TreeNode getParent() 
  { 
    return( i_parent ); 
  } 

  public boolean isLeaf() 
  { 
    return( i_myChildren.size() == 0 ); 
  } 

  //Methods to satisfy MutableTreeNode interface 

  //Adds child to the receiver at index. 
  public void insert( 
    MutableTreeNode node, int index) 
  { 
    if( i_allowChildren ) { 
      node.setParent( this ); 
      i_myChildren.insertElementAt( node, index); 
    } 
  } 

  //Removes the child at index from the receiver. 
  public void remove(int index) 
  { 
    i_myChildren.removeElementAt(index); 
  } 

  //Removes node from the receiver. 
  public void remove(MutableTreeNode node) 
  { 
    i_myChildren.removeElement( node ); 
  } 

  //Removes the receiver from its parent. 
  public void removeFromParent() 
  { 
    if( i_parent != null ) { 
      i_parent.remove( this ); 
      i_parent = null; 
    } 
  } 

  //Sets the parent of the receiver to newParent. 
  public void setParent(MutableTreeNode node) 
  { 
    i_parent = node; 
  } 

  public Vehicle getUserObject() 
  { 
    return( i_vehicle ); 
  } 

  //Resets the user object of the receiver to object 
  public void setUserObject(Object userObject) 
  { 
   if( userObject instanceof Vehicle ) 
      i_vehicle = (Vehicle)userObject; 
  } 

  public String toString() 
  { 
    return( i_vehicle.toString() ); 
  } 
} 

Listing 4. updateeventsource.java
 
/*********************************************** 
*                                              * 
* Interface defining the methods to be         * 
* implemented.                                 * 
*                                              * 
***********************************************/ 
public interface UpdateEventSource 
{ 
    public void addUpdateEventListener( 
      UpdateEventListener listener ); 

    public void removeUpdateEventListener( 
      UpdateEventListener listener ); 
} 
  
  

Listing 5. updateeventlistener.java
 
public interface UpdateEventListener 
{ 
    public void valueUpdated( UpdateEvent update); 
} 

Listing 6. updateevent.java
 
import java.util.*; 

/*********************************************** 
*                                              * 
* Event class used to communicate between the  * 
* application and the TreeNode.                * 
*                                              * 
***********************************************/ 
public class UpdateEvent extends EventObject 
{ 
  private Object i_value; 

  public UpdateEvent( 
    Object sourceObj, Object newValue ) 
  { 
    super( sourceObj ); 
    i_value = newValue; 
  } 

  public Object getUpdateValue() 
  { 
    return( i_value ); 
  } 
} 

Listing 7. adddata_exampleb.java
 
import javax.swing.*; 
import javax.swing.tree.*; 
import javax.swing.event.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.util.*; 

public class AddData_ExampleB 
{ 
  private DefaultTreeModel      i_model; 
  private JTree                 i_tree; 
  private JButton               i_cmdAdd; 
  private JButton               i_cmdRemove; 
  private JButton               i_cmdUpdate; 

  private VehicleTreeNode       i_root; 
  private VehicleTreeNode       i_currentTreeNode; 
  private JTextField            i_txtName; 
  private JTextField            i_txtType; 
  private JTextArea             i_txtDescription; 
  private JLabel                i_lblName; 
  private JLabel                i_lblType; 
  private JLabel                i_lblDesc; 
  private AddDataActionListener i_al; 
  
  public AddData_ExampleB( JFrame frm ) 
  { 
    GridBagLayout gb = new GridBagLayout(); 
    GridBagConstraints panConstr = 
      new GridBagConstraints(); 
    panConstr.anchor = 
      GridBagConstraints.NORTHWEST; 
    panConstr.insets = new Insets( 2, 2, 2, 2 ); 
    frm.getContentPane().setLayout( gb ); 

    //Create the root 
    i_al = new AddDataActionListener(); 

    Vehicle wrkVehicle = new Vehicle( "Vehicle" ); 
    wrkVehicle.setType( "Motor" ); 
    wrkVehicle.setDescription( 
      "Classification for motor vehicles" ); 

    Vehicle truck = new Vehicle( "Truck" ); 
    truck.setType( "Work" ); 
    truck.setDescription("For hauling goods"); 

 Vehicle pickup = new Vehicle( "Pickup " ); 
    pickup.setType( "Work" ); 
    pickup.setDescription( "For hauling hay" ); 
    truck.addSubType( pickup ); 
    wrkVehicle.addSubType( truck ); 

    i_root = new VehicleTreeNode(wrkVehicle,true); 
    i_model = new DefaultTreeModel(i_root, true); 
    //Create the tree using the root. 
    i_tree = new JTree( i_model ); 

    //Used to give desireable look to the JTree 
    i_tree.putClientProperty("JTree.lineStyle", "Angled"); 

    i_tree.setShowsRootHandles( true ); 
    i_tree.addTreeSelectionListener( i_al ); 

    panConstr.fill = GridBagConstraints.BOTH; 
    panConstr.weightx = 40; 
    panConstr.weighty = 100; 
    panConstr.gridx = 0; 
    panConstr.gridy = 0; 
    JScrollPane scrPane = new JScrollPane(i_tree); 
    gb.setConstraints( scrPane, panConstr ); 
    frm.getContentPane().add( scrPane ); 

    JPanel panel = createEditPanel(); 
    panConstr.weightx = 60; 
    panConstr.weighty = 100; 
    panConstr.gridx = 1; 
    panConstr.gridy = 0; 
    gb.setConstraints( panel, panConstr ); 
    frm.getContentPane().add( panel ); 

    JPanel buttonPanel = new JPanel( 
      new GridLayout(1, 2) ); 
    i_cmdAdd = new JButton("Add Nodes"); 
    i_cmdAdd.addActionListener( i_al ); 
    buttonPanel.add( i_cmdAdd ); 
    i_cmdRemove = new JButton( 
      "Remove Selected Node"); 
    i_cmdRemove.addActionListener( i_al ); 
    buttonPanel.add( i_cmdRemove ); 
    panConstr.fill = GridBagConstraints.NONE; 
    panConstr.weightx = 0; 
    panConstr.weighty = 0; 
    panConstr.gridx = 0; 
    panConstr.gridy = 1; 
    panConstr.gridwidth = 2; 
    gb.setConstraints(buttonPanel, panConstr); 
    frm.getContentPane().add( buttonPanel ); 
  } 

  public class AddDataActionListener 
    implements ActionListener, 
      TreeSelectionListener, UpdateEventListener 
  { 

  /*********************************************** 
  * This method is required by implementing the  * 
  * ActiontListener interface. The method        * 
  * is called when a user clicks the Update, Add * 
  * or Remove JButtons. If the user clicks add   * 
  * the class creates 2 more Vehicle classes and * 
  * inserts them into the model. NOTE: Do not    * 
  * insert into the MutableTreeNode as the view  * 
  * will not be updated until a repaint() occurs * 
  *                                              * 
  * @param update The ActionEvent that is        * 
  *               generated by the JButtons      * 
  * @return void                                 * 
  *                                              * 
  ***********************************************/ 
    public void actionPerformed( ActionEvent aeV ) 
    { 
      Object source = aeV.getSource(); 
      /// 
      // Add 2 new objects to the JTree 
      // 
      if( (source == i_cmdAdd) && 
        (i_root.getChildCount() <= 1) ) 
      { 
        Vehicle car = new Vehicle("Car"); 
        car.setType( "Personal" ); 
        car.setDescription( 
          "Used to drive to and from work"); 
        VehicleTreeNode carNode = 
          new VehicleTreeNode(car,true); 
        Vehicle van = new Vehicle("Van"); 
        van.setType( "Groups" ); 
        van.setDescription( 
          "For transporting groups of people"); 
        VehicleTreeNode vanNode = 
          new VehicleTreeNode(van,true); 
        i_model.insertNodeInto( 
          carNode, i_root, 1 ); 
        i_model.insertNodeInto( 
          vanNode, i_root, 1 ); 
      } 
      /// 
      // Remove the selected TreeNode from JTree. 
      // 
      else if( (source == i_cmdRemove) ) 
      { 
        if( i_tree.getMinSelectionRow() > 1 ) 
        { 
          if( i_currentTreeNode != null ) { 
            i_currentTreeNode. 
              removeUpdateEventListener(this); 
          } 
          i_model.removeNodeFromParent( 
            i_currentTreeNode); 
        } 
        else if( i_tree.getMinSelectionRow() == 1) 
            JOptionPane.showMessageDialog( 
                    null, 
                    "Intentially not allowing " + 
                    "this node to be removed"); 

      } 
      /// 
      // Update a TreeNode with changed values 
      // 
      else if( source == i_cmdUpdate ) { 
        if( i_currentTreeNode != null ) 
        { 
          i_currentTreeNode.getUserObject(). 
            setName( i_txtName.getText() ); 
          i_currentTreeNode.getUserObject(). 
            setType(i_txtType.getText()); 
          i_currentTreeNode.getUserObject(). 
            setDescription( 
              i_txtDescription.getText()); 
        } 
      } 
    } 

  /*********************************************** 
  * This method is required by implementing the  * 
  * UpdateEventListener interface.The interface  * 
  * allows the MutableTreeNode/VehicleTreeNode   * 
  * to communicate with the JTree.               * 
  *                                              * 
  * @param update The UpdateEvent that is        * 
  *               generated by the               * 
  *               MutableTreeNode                * 
  *                           (VehicleTreeNode)  * 
  * @return void                                 * 
  *                                              * 
  ***********************************************/ 
  public void valueUpdated( UpdateEvent update ) 
  { 
    Object source = update.getSource(); 
    if( source instanceof TreeNode ) 
    { 
      TreeNode[] nodes = 
        i_model.getPathToRoot((TreeNode)source); 
      TreePath path = new TreePath( nodes ); 
      /// 
      // Inform the model that something has 
      // changed, which in turn updates the view 
      // 
      i_model.valueForPathChanged( path, update. 
        getUpdateValue() ); 
    } 
  } 

  /*********************************************** 
  * This method is required by implementing the  * 
  * TreeSelectionListener interface. This call   * 
  * indicates a change has occurred and the      * 
  * view needs to be updated. Rather than        * 
  * registring with each of the nodes to listen  * 
  * for changes, this class only registers to    * 
  * listen to the node currently selected.       * 
  *                                              * 
  * @param treeEvent The selection event that    * 
  *                  contains information re:    * 
  *                  which node is selected      * 
  *                                              * 
  * @return void                                 * 
  *                                              * 
  ***********************************************/ 
  public void valueChanged( TreeSelectionEvent 
      treeEvent ) 
    { 
      if( i_currentTreeNode != null ) { 
        i_currentTreeNode. 
          removeUpdateEventListener( this ); 
      } 
      Object source = treeEvent.getSource(); 
      if( source instanceof JTree ) 
      { 
        if( ((JTree)source).getSelectionPath() 
          != null ) 
        { 
          Object selNode = ((JTree)source). 
            getSelectionPath(). 
              getLastPathComponent(); 
          if( selNode instanceof VehicleTreeNode ) 
          { 
            /// 
            // Add as listener for changes to the 
            // TreeNode 
            // 
            i_currentTreeNode = 
              (VehicleTreeNode)selNode; 
            i_currentTreeNode. 
              addUpdateEventListener( this ); 
            Vehicle curVehicle = 
              i_currentTreeNode.getUserObject(); 
            /// 
            // Update the view (JTextFields, etc) 
            // 
            if( curVehicle != null ) 
            { 
              i_txtName.setText( 
                curVehicle.getName() ); 
              i_txtType.setText( 
                curVehicle.getType() ); 
              i_txtDescription.setText( 
                curVehicle.getDescription() ); 
            } 
          } 
        } 
      } 
    } 
  } 
  

  /*********************************************** 
  * This method creates the view containing the  * 
  * JTree and text components.                   * 
  *                                              * 
  * @param none                                  * 
  *                                              * 
  * @return JPanel Contains the components       * 
  *                                              * 
  ***********************************************/ 
  public JPanel createEditPanel() 
  { 
    JPanel panel = new JPanel(); 
    GridBagLayout layout = new GridBagLayout(); 
    GridBagConstraints gbc = 
      new GridBagConstraints(); 
    panel.setLayout( layout ); 
    i_lblName = new JLabel( "Name" ); 
    gbc.anchor = GridBagConstraints.NORTHWEST; 
    gbc.insets = new Insets( 2, 2, 2, 2 ); 
    gbc.fill = GridBagConstraints.NONE; 
    gbc.weightx = 0; 
    gbc.weighty = 0; 
    gbc.gridx = 0; 
    gbc.gridy = 0; 
    layout.setConstraints(i_lblName, gbc); 
    panel.add( i_lblName ); 
  
    i_txtName = new JTextField(); 
    gbc.fill = GridBagConstraints.HORIZONTAL; 
    gbc.fill = GridBagConstraints.BOTH; 
    gbc.weightx = 100; 
    gbc.gridx = 1; 
    layout.setConstraints( i_txtName, gbc ); 
    panel.add( i_txtName ); 
  
    i_lblType = new JLabel( "Type" ); 
    gbc.fill = GridBagConstraints.NONE; 
    gbc.weightx = 0; 
    gbc.gridx = 0; 
    gbc.gridy = 1; 
    layout.setConstraints( i_lblType, gbc ); 
    panel.add( i_lblType ); 
  
    i_txtType = new JTextField(); 
    gbc.fill = GridBagConstraints.HORIZONTAL; 
    gbc.weightx = 100; 
    gbc.gridx = 1; 
    layout.setConstraints( i_txtType, gbc ); 
    panel.add( i_txtType ); 
  
    i_lblDesc = new JLabel( "Description " ); 
    gbc.fill = GridBagConstraints.NONE; 
    gbc.weightx = 0; 
    gbc.gridx = 0; 
    gbc.gridy = 2; 
    layout.setConstraints( i_lblDesc, gbc ); 
    panel.add( i_lblDesc ); 
  
    i_txtDescription = new JTextArea(); 
    i_txtDescription.setLineWrap( true ); 
    i_txtDescription.setWrapStyleWord( true ); 

    JScrollPane scrPane = 
      new JScrollPane( i_txtDescription ); 
    gbc.fill = GridBagConstraints.BOTH; 
    gbc.weighty = 100; 
    gbc.gridy = 3; 
    gbc.gridwidth = 2; 
    layout.setConstraints( scrPane, gbc ); 
    panel.add( scrPane ); 
  
    i_cmdUpdate = new JButton( "Update " ); 
    i_cmdUpdate.addActionListener( i_al ); 
    gbc.fill = GridBagConstraints.NONE; 
    gbc.weighty = 0; 
    gbc.gridy = 4; 
    gbc.gridwidth = 1; 
    layout.setConstraints( i_cmdUpdate, gbc ); 
    panel.add( i_cmdUpdate ); 
  
    JPanel filler = new JPanel(); 
    gbc.fill = GridBagConstraints.BOTH; 
    gbc.weightx = 100; 
    gbc.weighty = 100; 
    gbc.gridy = 5; 
    gbc.gridwidth = 2; 
    layout.setConstraints( filler, gbc ); 
    panel.add( filler ); 
    gbc.gridx = 1; 
    gbc.weightx = 100; 
    gbc.weighty = 0; 
  
    return( panel ); 
  } 
  
  /*********************************************** 
  * Main method for launching the application    * 
  *                                              * 
  * @param args Required signature for main()    * 
  *                                              * 
  * @return void                                 * 
  *                                              * 
  ***********************************************/ 
  public static void main(String[] args) { 
    JFrame f = new JFrame( 
      "Add data to the JTree - Example B"); 
    f.addWindowListener(new WindowAdapter() { 
      public void windowClosing( WindowEvent we ) 
      { 
       System.exit(0); 
      } 
    }); 
    AddData_ExampleB example = 
                      new AddData_ExampleB( f ); 
    f.pack(); 
    f.setSize( 
      new Dimension( f.getSize().width, 400 ) ); 
    f.setLocation( 200, 100 ); 
    f.setVisible(true); 
  } 
} 


  

 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.