Casting JTree to JScrollPane

Hi,
Is this a reasonable thing to do? I'm debugging some code for a friend. The code was written by a third party who shall remain nameless. It reads a text file into a JTree structure and the casts it to a JScrollPane using "JScrollPane sp = (JScrollPane)srcList.getParent().getParent();". Is this reasonable?
Secondly, in trying to debug this I'm trying to follow the execution thread into the JScrollPane object, however Eclipse reports that the line number is unavaiable, this to me looks like a version of the library with no debugging/symbol info but I can't find details of a version with debugging/symbol info.
Apologies if these are dim questions, I'm still learning... I'm a masochist, I find debugging someone elses code is often the quickest way to learn a new programming language :-)
Many thanks for any input anyone can give! I'm desperate on this.
Cheers,
Brian

It is difficult to say what is reasonable without looking at the source code, but I consider it very unlikely that a sensibly designed program would guarantee that a JTree's parent's parent is always a JScrollPane. There are bound to be maintainability and reusability issues with implicit conditions like that.
While Swing is not bug-free, I'd also guess that it is unnecessary for you to step through any JScrollPane code to find the bug you are looking for. Can you give a slightly more concrete example of what you're trying to diagnose?
Some more information on debugging in the JRE applicable to Eclipse:
http://developer.java.sun.com/developer/bugParade/bugs/4652184.html

Similar Messages

  • Problem in refreshing JTree inside Jscrollpane on Button click

    hi sir,
    i have problem in refreshing JTree on Button click inside JscrollPane
    Actually I am removing scrollPane from panel and then again creating tree inside scrollpane and adding it to Jpanel but the tree is not shown inside scrollpane. here is the dummy code.
    please help me.
    import javax.swing.*;
    import java.awt.*;
    import javax.swing.tree.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.event.WindowListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    public class Test
    public static void main(String[] args)
         JFrame jf=new TestTreeRefresh();
         jf.addWindowListener( new
         WindowAdapter()
         public void windowClosing(WindowEvent e )
         System.exit(0);
         jf.setVisible(true);
    class TestTreeRefresh extends JFrame
         DefaultMutableTreeNode top;
    JTree tree;
         JScrollPane treeView;
         JPanel jp=new JPanel();
         JButton jb= new JButton("Refresh");
    TestTreeRefresh()
    setTitle("TestTree");
    setSize(500,500);
    getContentPane().setLayout(null);
    jp.setBounds(new Rectangle(1,1,490,490));
    jp.setLayout(null);
    top =new DefaultMutableTreeNode("The Java Series");
    createNodes(top);
    tree = new JTree(top);
    treeView = new JScrollPane(tree);
    treeView.setBounds(new Rectangle(50,50,200,200));
    jb.setBounds(new Rectangle(50,300,100,50));
    jb.addActionListener(new ActionListener()
    public void actionPerformed(ActionEvent ae)
              jp.remove(treeView);
              top =new DefaultMutableTreeNode("The Java ");
    createNodes(top);
              tree = new JTree(top);
                   treeView = new JScrollPane(tree);
                   treeView.setBounds(new Rectangle(50,50,200,200));
                   jp.add(treeView);     
                   jp.repaint();     
    jp.add(jb);     
    jp.add(treeView);
    getContentPane().add(jp);
    private void createNodes(DefaultMutableTreeNode top) {
    DefaultMutableTreeNode category = null;
    DefaultMutableTreeNode book = null;
    category = new DefaultMutableTreeNode("Books for Java Programmers");
    top.add(category);
    book = new DefaultMutableTreeNode("The Java Tutorial: A Short Course on the Basics");
    category.add(book);
    book = new DefaultMutableTreeNode("The Java Tutorial Continued: The Rest of the JDK");
    category.add(book);
    book = new DefaultMutableTreeNode("The JFC Swing Tutorial: A Guide to Constructing GUIs");
    category.add(book);
    book = new DefaultMutableTreeNode("Effective Java Programming Language Guide");
    category.add(book);
    book = new DefaultMutableTreeNode("The Java Programming Language");
    category.add(book);
    book = new DefaultMutableTreeNode("The Java Developers Almanac");
    category.add(book);
    category = new DefaultMutableTreeNode("Books for Java Implementers");
    top.add(category);
    book = new DefaultMutableTreeNode("The Java Virtual Machine Specification");
    category.add(book);
    book = new DefaultMutableTreeNode("The Java Language Specification");
    category.add(book);
    }

    hi sir ,
    thaks for u'r suggession.Its working fine but the
    properties of the previous tree were not working
    after setModel() property .like action at leaf node
    is not working,I'm sorry but I don't understand. I think you are saying that the problem is solved but I can read it to mean that you still have a problem.
    If you still have a problem then please post some code (using the [code] tags of course).

  • JTree and JScrollPane problems

    Hello all.
    bear with me a second, so I can explain what is happenning with my code. I have got a java application that is accessing an SQL database (either Oracle or postgreSQL) and inputing the data into a tree.
    More specifically it takes the "enrollment" data and draws into the scrollpane the courses and students enrolled. Everything seems to be working perfectly with the building of the tree.
    but I have a drop down list that can select which years to display (1-4th year). I want the tree in the scrollPane to update when u change the year in the drop down list.
    I have added an ActionListener to the drop down list and it picks up the right selection, and I create a new tree object with the new year details and nothing is changed in the scrollpane.
    This is what the GUI looks like:
    This is my tree class which creates teh tree and adds it to the scrollPane object:
    package Interface;
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.tree.*;
    import java.sql.*;
    import Database.Database;
    public class Tree{
         JTree tree;
         JScrollPane scrollPane;
         public Tree(GridBagConstraints c, Container contentPane, GridBagLayout gridBag, int year){
              //Set up the default table.
              TreeNode root = buildTree("Enrollments", year);
              tree = new JTree(root);
              //Set up a scrollPane for the tree.
              scrollPane = new JScrollPane(tree);
              //Set constraints for the ScrollPane
              c.fill = GridBagConstraints.BOTH;
              c.weightx = 1;
              c.weighty = 1;
              c.gridwidth = 2;
              c.gridx = 0;
              c.gridy = 1;
              c.anchor = GridBagConstraints.CENTER;
              gridBag.setConstraints(scrollPane, c);
              contentPane.add(scrollPane);     
              //Set defaults for teh Tree object.
              tree.expandRow(0);
              tree.setRootVisible(false);
              tree.setEditable(true);     
         public DefaultMutableTreeNode buildTree(String s, int year){
              //Set root of tree.
              DefaultMutableTreeNode node = new DefaultMutableTreeNode(s);
              //Create a database object.
              Database db = new Database();
              //change this bit to get the name's of the courses and add into array.
              ResultSet className = db.query("SELECT name FROM courses WHERE year = "+year);
              DefaultMutableTreeNode course = null;
              DefaultMutableTreeNode name = null;
              try {
                        while(className.next()){
                             String classStr = className.getString("name");
                             course = new DefaultMutableTreeNode(classStr);
                             node.add(course);
                             ResultSet firstName = db.query("SELECT students.name FROM students, enrollments, courses WHERE students.id = enrollments.studentID AND enrollments.courseid = courses.id AND courses.name = '"+classStr+"'");
                             while(firstName.next()){
                                  String nameStr = firstName.getString("name");
                                  name = new DefaultMutableTreeNode(nameStr);
                                  course.add(name);
                   catch(Exception e){
                        System.err.println("" + e.getMessage());
                        e.printStackTrace(System.err);
              return node;
    }This is the GUI class that calls on the tree class and draws the GUI. and the actionlistener for the drop down list.
    package Interface;
         import java.awt.*;
         import javax.swing.*;
         import java.awt.event.*;
    import java.sql.*;
         public class GUI extends JFrame implements ActionListener{
              private static final long serialVersionUID = 1L;     
              DropDownMenu yearDrop;
              ScrollPane scrollPane;
              JMenuItem menuItemSave;
              JMenuItem menuItemQuit;
              JMenuBar menuBar;
              JMenu menu;
              JButton save;
              JButton quit;
              Tree tree;
              String year[] = { "First Year", "Second Year", "Third Year", "Fourth Year" };
              Container contentPane = getContentPane();
              GridBagLayout gridBag = new GridBagLayout();
              GridBagConstraints c = new GridBagConstraints();
              public GUI(){
                   setSize(500,500);
                   setTitle("UTAS Enrollments");
                   contentPane.setLayout(gridBag);          
                   save = new JButton("Save");
                   quit = new JButton("Quit");
                   menuItemSave = new JMenuItem("Save");
                   menuItemQuit = new JMenuItem("Quit");
                   menuItemSave.setMnemonic('s');
                   menuItemQuit.setMnemonic('q');
                   yearDrop = new DropDownMenu(year, c, contentPane, gridBag);
                   tree = new Tree(c, contentPane, gridBag, 1);
                   menuBar = new JMenuBar();
                   menu = new JMenu("File");
                   menu.setMnemonic('f');
                   menuBar.add(menu);
                   menu.add(menuItemSave);
                   menu.add(menuItemQuit);
                   setJMenuBar(menuBar);
                   //Set constraints for the Save button
                   c.fill = GridBagConstraints.NONE;
                   c.weightx = 1;
                   c.weighty = 0;
                   c.gridwidth = 1;
                   c.gridx = 0;
                   c.gridy = 2;
                   c.anchor = GridBagConstraints.WEST;
                   save.setMnemonic('s');
                   gridBag.setConstraints(save, c);
                   contentPane.add(save);
                   //Set constraints for the Quit button
                   c.fill = GridBagConstraints.NONE;
                   c.weightx = 1;
                   c.weighty = 0;
                   c.gridwidth = 1;
                   c.gridx = 1;
                   c.gridy = 2;
                   c.anchor = GridBagConstraints.EAST;
                   quit.setMnemonic('q');
                   gridBag.setConstraints(quit, c);
                   contentPane.add(quit);
                   //Add ActionListener to the interactive buttons on the GUI.
                   //QUIT MENU
                   menuItemQuit.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                             quitAction();
                   //SAVE MENU
                   menuItemSave.addActionListener(new ActionListener(){
                        public void actionPerformed(ActionEvent e){
                             saveAction();
                   //QUIT BUTTON
                   quit.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e){
                             quitAction();
                   //SAVE BUTTON
                   save.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e){
                             saveAction();
                   //DROPDOWN LIST
                   yearDrop.dropDown.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e){
                             listAction();
              public void listAction(){
                   System.out.println("getSelected: "+ year[yearDrop.getSelected()]);
                   tree = new Tree(c, contentPane, gridBag, (yearDrop.getSelected()+1));
                   repaint();
         I have taken out the quitAction and saveAction methods to save space.
    I would have thought creating a new Tree object over the original tree would have updated the scrollPane.
    Any ideas would be awesome.
    Thanks in advance.

    and I create a new tree object with the new year details and nothing is changed in the scrollpane.Creating a new Tree doesn't add the tree to the scroll pane. It just changes the reference of the tree variable to point to the new Tree object. To update the scroll pane you would need to do:
    scrollPane.setViewportView( tree );
    Or another option is to just create a new TreeModel and then change the model that your existing tree is using:
    TreeModel model = new TreeModel(...);
    tree.setModel( model );

  • JTree in JScrollPane doesn't scroll

    I have a JTree in a JScrollPane that doesn't scroll.
    Background: it used to!
    History: I moved the creation of the JTree to inside an Object that returns it with a ".getTree()" method. Since then, it hasn't been scrolling. I even set the JScrollPane to always have scrollbars and they just sit there inactive.
    Does anyone know what this would happen and/or how to fix it?
    thanks.

    That actaully worked, but it was way to big. I'm not sure what I did, but I changed the values to Studio ONE's defaults and now it works ok.

  • JTree in JScrollpane, find out

    I have following constellation:
    A JTree in a JScrollpane with a vertical scrollbar.
    Most nodes are hidden "behind the scrollpane" and only view are seen.
    I search for a property which signals if a node is currently seen on screen and which are "scrolled" (that means those nodes which aren't visible) (the jcomponent method isVisible() doesn't work)
    I think about such a method which uses the information of the scrollbarposition and the dimension of the jviewport but i can't get the information which node is seen if some nodes are expanded and some are collapsed.
    Does anybody know such a property or method?
    Or is there a SWING-THING which solves the problem?
    Thank you for your answers

    isVisible() is a "Component-Method" which means i have to take the rendered component finding out the visible nodes.
    my application is following:
    i write a method which expand all nodes until the leafs are visible but because of performance (i have more than 1000 nodes directly under the root) i want to expand only the nodes which are showing and expand the other later.

  • JTree in JScrollPane not resizing after model change.

    I have a JTree in a JScrollPane that's put in a JPanel using the JGoodies FormLayout as it's layout manager. The column definition defines the column to grow so the JScrollPane should have enough space. The tree is empty when I create it and put it in the JScrollPane.
    After adding some nodes to the tree the JScrollPane does not resize automatically, it only resizes when a forced repaint occurs (moving or resizing the window , etc).
    I've tried calling invalidate(), repaint(),revalidate() on both the JTree and the JScrollPane. I also do a reload() on the TreeModel after adding the nodes (which is probably unnecessary as I use insertNodeInto(node,parent,position) from the DefaultTreeModel to add the new nodes).
    Anybody knows what I'm missing here?
    Thanks a lot in advance !

    Update:
    The card is running now. I had another Arch system (arch2) on the same network (to compare with), and after I shut that down and rebooted the first one (arch1) the network interfaces show up, both lo and eth0.
    However, the startup routine hangs about 2-3 minutes on the network daemon (with blinking router lights and hdd activity), so it's still not perfect. Another thing is that I can't get the right gateway from the router's dhcp. Here's an overview of the setup:
    Home: 192.168.2.x
    arch1 .2.102-------|
    |
    arch2 .2.101-------| old SMC router: .2.1
    |
    fritzbox .2.2 -----|
    router/modem
    |
    |
    ~~~~~
    internet
    The gateway is supposed to be the fritzbox with ip 192.168.2.2. The old SMC router serves addresses in the range .2.101-110. I set it up this way because I need the 8 ports on the SMC (and I like watching the lights).
    Here's my /etc/rc.conf for both systems:
    arch1 (starts slow):
    eth0="dhcp"
    ROUTES=(!gateway)
    arch2 (working):
    eth0="eth0 192.168.2.101 netmask 255.255.255.0 broadcast 192.168.2.255"
    gateway="default gw 192.168.2.2"
    ROUTES=(gateway)
    arch1 gets the SMC as gateway, arch2 gets the fritzbox.
    Ideally all computers on the network should get their IPs from the SMC dhcp, which also gives them the fritzbox as gateway. But that just doesn't work for the arch1. arch2 and the windows PCs get online just fine.
    I had also tried setting arch1 the same as arch2 except for the ip=2.102. Then the network starts faster, but the gateway is still stuck at the SMC router -> no internet.
    Rather complicated, but what are networks for? Anyone see daylight in this mess?
    Last edited by bitpal (2009-08-13 21:24:02)

  • JTree in JScrollPane incorrect scrolling

    Hi,
    I have a JTree in a JScrollPane, when I scroll (programatically) in the JTree using the setAnchorSelectionPath method of the JTree some scrolling occurs but not enough to bring the leaf node of the JTree into the visible viewport.
    Any suggestions,
    Mark

    Cheers for the pointer, I am not attempting drag and drop though ....
    I have a selected leaf node in the tree and then I change the type of sort I use on the data in the tree, I find the new leaf node, expand the path down to it and then attempt to scroll.
    Thus no pointer information to use with <i>Autoscroll</i>
    Mark

  • Refresing JTree with JScrollPane

    I have made a JTree where the nodes can be dynamically added and removed, this tree is added to a scroll pane:
    JTree tree = new JTree();
    JScrollPane scrollpane = new JScrollPane(tree);
    The object that instantiates the tree extends from JPanel and the scrollpane is added to the panel.
    Then the tree is added to a JSplitPane. When I resize the window the scrollbars will appear, although when the nodes are added to the tree they will keep extending without having the scrollbar appear.
    Does anyone know a good way to make the scroll bars appear when the tree gets larger than the frame?

    I have made a JTree where the nodes can be dynamically added and removed, this tree is added to a scroll pane:
    JTree tree = new JTree();
    JScrollPane scrollpane = new JScrollPane(tree);
    The object that instantiates the tree extends from JPanel and the scrollpane is added to the panel.
    Then the tree is added to a JSplitPane. When I resize the window the scrollbars will appear, although when the nodes are added to the tree they will keep extending without having the scrollbar appear.
    Does anyone know a good way to make the scroll bars appear when the tree gets larger than the frame?

  • JTree cut and paste multiple child and ancestor nodes not function correct

    Hello i'm creating a filebrowser for multimedia files (SDK 1.22) such as .jpg .gif .au .wav. (using Swing, JTree, DefaultMutableTreeNode)
    The problem is I want to cut and paste single and multiple nodes (from current node til the last child "top-down") which partly functions:
    single nodes and multiple nodes with only one folder per hierarchy level;
    Not function:
    - multiple folders in the same level -> the former order gets lost
    - if there is a file (MMed. document) between folders (or after them) in the same level this file is put inside one of those
    I tried much easier functions to cope with this problem but every time I solve one the "steps" the next problem appears...
    The thing I don't want to do, is something like a LinkedList, Hashtable (to store the nodes)... because the MMed. filebrowser would need to much resources while browsing through a media library with (e.g.) thousands of files!
    If someone has any idea to solve this problem I would be very pleased!
    Thank you anyway by reading this ;)
    // part of the code, if you want more detailed info
    // @mail: [email protected]
    import java.util.Enumeration;
    import javax.swing.*;
    import javax.swing.tree.*;
    // var declaration
    static Enumeration en;
    static DefaultMutableTreeNode jTreeModel, lastCopiedNode, dmt, insert, insertParent, insertSameFolder;
    static String varCut;
    static int index= 0;
    static int counter = 0;
    /* cut function */
    if (actionCommand.equals ("cut"))
         // get the selected node (DefaultMutableTreeNode)
         lastCopiedNode = (DefaultMutableTreeNode)  selPath.getLastPathComponent ();
         // get the nodes in an Enumeration array (in top- down order)
         en = dmt.preorderEnumeration();
         // the way to make sure if it is a cut or a copied node
         varCut = "cut";
    /* paste function */
    if (actionCommand.equals ("paste"))
    // node is cut
    if (varCut == "cut")
    // is necessary for first time same folder case
    insertParent = dmt;
    // getting the nodes out of the array
    while(en.hasMoreElements())
         // cast the Object to DefaultMutableTreeNode
         // and get stored (next) node
         insert = (DefaultMutableTreeNode) en.nextElement();
    // check if the node is a catalogue when getRepresentation()
    // is a function of my node creating class (to know if it is
    // a folder or a file)
    if (insert.getRepresentation().equals("catalogue"))
         // check if a "folder" node is inserted
         if (index == 1)
              counter = 0;
              index = 0;
         System.out.println ("***index and counter reset***");
         // the node is in the same folder
         // check if the folder is in the same hierarchy level
         // -> in this case the insertParent has to remain
         if (insert.getLevel() == insertParent.getLevel())
              // this is necessary to get right parent folder
              insertSameFolder = (Knoten) insert.getParent();
              jTreeModel.insertNodeInto (insert, insertSameFolder, index);
    System.out.println (">>>sameFolderCASE- insert"+counter+"> " + String.valueOf(insert) +
              "\ninsertTest -> " + String.valueOf(insertTest)
              // set insertParent folder to the new createded one
              insertParent = insert;
              index ++;
         else // the node is a subfolder
              // insertNode
              jTreeModel.insertNodeInto (insert, insertParent, index);
              // set insertParent folder to the new createded one
              insertParent = insert;
    System.out.println (">>>subFolderCASE- insertParent"+counter+"> " + String.valueOf(insertParent) +
              "\ninsertParent -> " + String.valueOf(insertParent)
              index ++;
    else // the node is a file
         // insertNode
         jTreeModel.insertNodeInto (insert, insertParent, counter);
         System.out.println (">>>fileCASE insert "+counter+"> " + String.valueOf(insert) +
         "\ninsertParent -> " + String.valueOf(insertParent)
    counter ++;
    // reset index and counter for the next loop
    index = 0;
    counter = 0;
    // reset cut var
    varCut = null;
    // remove the node (automatically deletes subfolders and files)
    dmt.removeNodeFromParent (lastCopiedNode);
    // if node is a copied one
    if varCut == null)
         // insert copied node the same way as for cut one's
         // to make it possible to copy more than one node
    }

    You need to use a recursive copy method to do this. Here's a simple example of the copy. To do a cut you need to do a copy and then delete the source.
    import java.io.*;
    import javax.swing.*;
    import javax.swing.tree.*;
    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.event.*;
    import java.util.*;
    public class CopyTree extends JFrame
         Container cp;
         JTree cTree;
         JScrollPane spTree;
         DefaultTreeModel cModel;
         CNode cRoot;
         JMenuBar menuBar = new JMenuBar();
         JMenu editMenu = new JMenu("Edit");
         JMenuItem copyItem = new JMenuItem("Copy");
         JMenuItem pasteItem = new JMenuItem("Paste");
         TreePath [] sourcePaths;
         TreePath [] destPaths;
         // =====================================================================
         // constructors and public methods
         CopyTree()
              super("Copy Tree Example");
              addWindowListener(new WindowAdapter()
              {public void windowClosing(WindowEvent evt){System.exit(0);}});
              // edit menu
              copyItem.addActionListener(new ActionListener()
              {public void actionPerformed(ActionEvent e){mnuCopy();}});
              editMenu.add(copyItem);
              pasteItem.addActionListener(new ActionListener()
              {public void actionPerformed(ActionEvent e){mnuPaste();}});
              editMenu.add(pasteItem);
              menuBar.add(editMenu);
              setJMenuBar(menuBar);
              buildModel();
              cp = getContentPane();
              cTree = new JTree(cModel);
              spTree = new JScrollPane(cTree);
              cp.add(spTree, BorderLayout.CENTER);
              pack();
         static public void main(String [] args)
              new CopyTree().show();
         // =====================================================================
         // private methods - User Interface
         private void mnuCopy()
              sourcePaths = cTree.getSelectionPaths();
         private void mnuPaste()
              TreePath sp, dp;
              CNode sn,dn;
              int i;
              destPaths = cTree.getSelectionPaths();
              if(1 == destPaths.length)
                   dp = destPaths[0];
                   for(i=0; i< sourcePaths.length;i++)
                        sp = sourcePaths;
                        if(sp.isDescendant(dp) || dp.isDescendant(sp))
                             JOptionPane.showMessageDialog
                                  (null, "source and destinations overlap","Paste", JOptionPane.ERROR_MESSAGE);
                             return;
                   dn = (CNode)dp.getLastPathComponent(); // the node we will append our source to
                   for(i=0; i< sourcePaths.length;i++)
                        sn = (CNode)sourcePaths[i].getLastPathComponent();
                        copyNode(sn,dn);
              else
                   JOptionPane.showMessageDialog
                        (null, "multiple destinations not allowed","Paste", JOptionPane.ERROR_MESSAGE);
         // recursive copy method
         private void copyNode(CNode sn, CNode dn)
              int i;
              CNode scn = new CNode(sn);      // make a copy of the node
              // insert it into the model
              cModel.insertNodeInto(scn,dn,dn.getChildCount());
              // copy its children
              for(i = 0; i<sn.getChildCount();i++)
                   copyNode((CNode)sn.getChildAt(i),scn);
         // ===================================================================
         // private methods - just a sample tree
         private void buildModel()
              int k = 0;
              cRoot = new CNode("root");
              cModel = new DefaultTreeModel(cRoot);
              CNode n1a = new CNode("n1a");
              cModel.insertNodeInto(n1a,cRoot,k++);
              CNode n1b = new CNode("n1b");
              cModel.insertNodeInto(n1b,cRoot,k++);
              CNode n1c = new CNode("n1c");
              cModel.insertNodeInto(n1c,cRoot,k++);
              CNode n1d = new CNode("n1d");
              cModel.insertNodeInto(n1d,cRoot,k++);
              k = 0;
              CNode n1a1 = new CNode("n1a1");
              cModel.insertNodeInto(n1a1,n1a,k++);
              CNode n1a2 = new CNode("n1a2");
              cModel.insertNodeInto(n1a2,n1a,k++);
              CNode n1a3 = new CNode("n1a3");
              cModel.insertNodeInto(n1a3,n1a,k++);
              CNode n1a4 = new CNode("n1a4");
              cModel.insertNodeInto(n1a4,n1a,k++);
              k = 0;
              CNode n1c1 = new CNode("n1c1");
              cModel.insertNodeInto(n1c1,n1c,k++);
              CNode n1c2 = new CNode("n1c2");
              cModel.insertNodeInto(n1c2,n1c,k++);
         // simple tree node with copy constructor
         class CNode extends DefaultMutableTreeNode
              private String name = "";
              // default constructor
              CNode(){this("");}
              // new constructor
              CNode(String n){super(n);}
              // copy constructor
              CNode(CNode c)
                   super(c.getName());
              public String getName(){return (String)getUserObject();}
              public String toString(){return  getName();}

  • Problem with JTree in expanded mode?

    Hi I have a JTree and I have added few nodes to it. Now when i run my program it is displaying all the nodes in the expanded mode. but at some point of time i need to add few more nodes. when i am am adding nodes to the root node when the tree in expanded mode the newly added nodes are not visible. They are not getting added. what could be the problem? how do i add nodes to the tree when in expanded mode as well as remove few nodes when in expanded mode?
    My code is as follows.
    import java.awt.BorderLayout;
    import javax.swing.JDesktopPane;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTree;
    import javax.swing.tree.DefaultMutableTreeNode;
    public class Main extends JFrame{
         private DefaultMutableTreeNode top;
         public JTree mainTree;
         public Main(){
              super();
              JDesktopPane dp=new JDesktopPane();          
              dp.setLayout(new BorderLayout());
              top =new DefaultMutableTreeNode("All Active Nodes");                    
              mainTree=new JTree(top);          
              JScrollPane mtsp=new JScrollPane(mainTree);
              dp.add(mtsp,BorderLayout.CENTER);
              this.setContentPane(dp);          
              this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);          
              this.setSize(300, 550);
              this.setVisible(true);
         public static void main(String[]args)throws Exception{
              Main as=new Main();
              DefaultMutableTreeNode top=as.getTop();
              DefaultMutableTreeNode node=new DefaultMutableTreeNode("murali");
              top.add(node);
              as.mainTree.expandRow(0);          
              Thread.sleep(10000);
              System.out.println("there");
              DefaultMutableTreeNode node1=new DefaultMutableTreeNode("murali12");
              top.add(node1);
          * @return the top
         public DefaultMutableTreeNode getTop() {
              return top;
    }

    I got the solution. The solution is to invoke nodesWereInserted(TreeNode node, int[] childIndices) this on treeModel after adding nodes are removing nodes.

  • How to show only all children of selected node in JTree??

    Dear friends:
    I have Two Panels, PA and PB,
    PA has a Jtree as code below, and PB listens to PA,
    I hope to do following,
    If I select a node called A in PA, then Node A's all children such as A1, A2, A3 will be displayed in PB, but not display A1, A2, A3's children such as A3 has C1, C2, C3, C4 & C5, until I select A3 then PB will display only all A3's children: C1, C2, C3, C4 & C5;
    i.e, only populate each ONE level of children of Node A or any node I select, not its grandchildren and its grand-grand children;
    Please help how to do it??
    I tried amny times, failed.
    Thanks
    [1]. PA panel code:
    package com.atest;
         import java.awt.BorderLayout;
         import java.awt.event.MouseAdapter;
         import java.awt.event.MouseEvent;
         import java.util.Enumeration;
         import java.awt.Dimension;
         import javax.swing.JFrame;
         import javax.swing.JPanel;
         import javax.swing.JScrollPane;
         import javax.swing.JTextField;
         import javax.swing.JTree;
         import javax.swing.tree.DefaultMutableTreeNode;
         import javax.swing.tree.TreeModel;
         import javax.swing.tree.TreePath;
         public class DefaultMutableTreeMain extends JPanel {
         protected DefaultMutableTreeNode    top = new DefaultMutableTreeNode("Options");
         protected DefaultMutableTreeNode      selectedNode = null;
         protected final JTree tree;
         protected final JTextField jtf;
        protected Enumeration      vEnum = null;
         private      TreeModel                m;
         protected  DefaultMutableTreeNode      getDefaultMutableTreeNode()  {
              //textArea.getText();
                   return selectedNode;
         protected  DefaultMutableTreeNode setDefaultMutableTreeNode(DefaultMutableTreeNode tt)  {
              //textArea.getText();
                   selectedNode = tt;
                   return selectedNode;
         protected  TreeModel getJTModel()  {
              //textArea.getText();
                   return m;
         protected  TreeModel setJTModel(TreeModel ta)  {
                   m = ta;
                   return m;
           public DefaultMutableTreeMain() {
             setSize(300,300);
             setLayout(new BorderLayout());
             DefaultMutableTreeNode a = new DefaultMutableTreeNode("A");
             top.add(a);
             DefaultMutableTreeNode a1 = new DefaultMutableTreeNode("A1");
             a.add(a1);
             DefaultMutableTreeNode a2 = new DefaultMutableTreeNode("A2");
             a.add(a2);
             DefaultMutableTreeNode a3 = new DefaultMutableTreeNode("A3");
             a.add(a3);
             DefaultMutableTreeNode b = new DefaultMutableTreeNode("B");
             top.add(b);
             DefaultMutableTreeNode b1 = new DefaultMutableTreeNode("B1");
             b.add(b1);
             DefaultMutableTreeNode b2 = new DefaultMutableTreeNode("B2");
             b.add(b2);
             DefaultMutableTreeNode b3 = new DefaultMutableTreeNode("B3");
             b.add(b3);
             DefaultMutableTreeNode c = new DefaultMutableTreeNode("C");
             a3.add(c);
             DefaultMutableTreeNode c1 = new DefaultMutableTreeNode("C1");
             c.add(c1);
             DefaultMutableTreeNode c2 = new DefaultMutableTreeNode("C2");
             c.add(c2);
             DefaultMutableTreeNode c3 = new DefaultMutableTreeNode("C3");
             c.add(c3);
             DefaultMutableTreeNode c4 = new DefaultMutableTreeNode("C4");
             c.add(c4);
             DefaultMutableTreeNode c5 = new DefaultMutableTreeNode("C5");
             c.add(c5);
             tree = new JTree(top);
             JScrollPane jsp = new JScrollPane(tree);
             jsp.setPreferredSize(new Dimension(400,300));
             add(jsp, BorderLayout.CENTER);
             jtf = new JTextField("", 20);
             add(jtf, BorderLayout.SOUTH);
               tree.addMouseListener(new MouseAdapter() {
               public void mouseClicked(MouseEvent me) {
                  TreePath   path = tree.getSelectionPath();
                  DefaultMutableTreeNode      selectedNode = (DefaultMutableTreeNode)path.getLastPathComponent();
                 TreePath tp = tree.getPathForLocation(me.getX(), me.getY());
                 setDefaultMutableTreeNode(selectedNode);
                     System.out.println("Current node selected is (tp.toString()=" + tp.toString());
                     System.out.println("Current node selected is getDefaultMutableTreeNode()=" + getDefaultMutableTreeNode());
                 if (tp != null){
                     jtf.setText(tp.toString());
                      System.out.println("It Has Children as selectedNode.getChildCount()= " + selectedNode.getChildCount());
                            Enumeration vEnum = selectedNode.children();
                                int i = 0;
                                while(vEnum.hasMoreElements()){
                                    System.out.println("2 selectedNode = " +  path.toString() + "  has " + i++ + " Children in vEnum.nextElement(" + i + ") = " + vEnum.nextElement());
                 else
                   jtf.setText("");
           public static void main(String[] args) {
             JFrame frame = new JFrame();
             frame.getContentPane().add(new DefaultMutableTreeMain());
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             frame.setSize(400, 400);
             frame.setVisible(true);
         }[2]. PB Panel code
    package com.atest;
    import java.awt.BorderLayout;
    import javax.swing.JScrollPane;
    import javax.swing.JTree;
    import javax.swing.JPanel;
    import javax.swing.event.TreeSelectionEvent;
    import javax.swing.event.TreeSelectionListener;
    import javax.swing.tree.*;
    import javax.swing.JButton;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.tree.DefaultTreeModel;
    import javax.swing.tree.TreePath;
    public class DefaultMutableTreeSub extends JPanel implements java.io.Serializable{
      private JButton removeButton;
      private JButton addButton;
      JTree tree;
      private TreeModel      m;
      protected TreeDragSource ds;
      protected TreeDropTarget dt;
    protected  TreeModel getJTModel()  {
              //textArea.getText();
                   return m;
    protected  TreeModel setJTModel(TreeModel ta)  {
                   m = ta;
                   return m;
    protected DefaultTreeModel model;
    protected DefaultMutableTreeNode rootNode;
    DefaultMutableTreeMain dmm = null;
    JPanel inputPanel  = new JPanel();
      public JPanel SLTreeDNDEditableDynamic(DefaultMutableTreeMain tdnd ) {
        //super("Rearrangeable Tree");
        setSize(400,450);
        dmm = tdnd;
             setLayout(new BorderLayout());
             inputPanel.setLayout(new BorderLayout());
             JPanel outputPanel = new JPanel();
             System.out.println("Sub selectedNode tdnd= " + tdnd);
             tdnd.tree.addTreeSelectionListener(new TreeSelectionListener(){
                  public void valueChanged(TreeSelectionEvent evt){
                  TreePath[] paths = evt.getPaths();
                  TreePath   path = dmm.tree.getSelectionPath();
                  DefaultMutableTreeNode      selectedNode = (DefaultMutableTreeNode)path.getLastPathComponent();
                 DefaultMutableTreeNode itemNode = dmm.getDefaultMutableTreeNode();
                     System.out.println("Sub node selected is dmm.getDefaultMutableTreeNode()=" + dmm.getDefaultMutableTreeNode());
                  model = new DefaultTreeModel(itemNode);
                  tree = new JTree(model);
                  System.out.println("Sub selectedNode paths= " + paths);
                  System.out.println("Sub selectedNode path= " + path);
                  System.out.println("Sub selectedNode = " + selectedNode);
                  System.out.println("Sub itemNode = " + itemNode);
                  tree.putClientProperty("JTree.lineStyle", "Angled");
                  tree.setRootVisible(true);
                   inputPanel.add(new JScrollPane(tree),BorderLayout.CENTER);
             return inputPanel;
         public DefaultMutableTreeSub() {
              super();
    }thanks
    sunny

    Thanks so much, I use your code and import followig:
    import java.util.ArrayList;
    import java.awt.List;
    but
    private static List<Object> getChildNodes(JTree j) {
         Object parent = j.getLastSelectedPathComponent();
         int childNodeCount = j.getModel().getChildCount(parent);
         List<Object> results = new ArrayList()<Object>;
         for (i = 0; i < childNodeCount; i++) {
              results.add(parent, i);
         return results;
    here List<Object> and ArrayList()<Object> show red,
    Is my JDK version problem??
    my one is JKD
    C:\temp\swing>java -version
    java version "1.4.2_08"
    Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_08-b03)
    Java HotSpot(TM) Client VM (build 1.4.2_08-b03, mixed mode)
    Error as follows:
    C:\temp\swing>javac DefaultMutableTreeSub.java
    DefaultMutableTreeSub.java:38: <identifier> expected
    private static List<Object> getChildNodes(JTree j) {
    ^
    1 error
    any idea??
    Thanks

  • Problem setting icon in jtree

    I have read the tutorial but i still cant get a home made icon in my tree-component. If i load it and teel to use it it doesn't show!!! what's wrong??
    public DefaultMutableTreeNode
                tnRoot = new DefaultMutableTreeNode("FleetCommander"),
                tnFree = new DefaultMutableTreeNode("Vessels Free to Use");
            DefaultTreeModel tmInfo = new DefaultTreeModel(tnRoot);
        JTree tInfo = new JTree(tmInfo);
        JScrollPane spInfo = new JScrollPane(tInfo);
        ImageIcon Icons[] = new ImageIcon[20];
        JALOCTreeCellRenderer tcr;
        public DisplayMap(Object par, DefaultMap m) {
            parent = par;
            this.m = m;
                // ControlPanel
            f2.setSize(300,400);
            f2.setLocation(700,10);
            Container p = f2.getContentPane();
            tnRoot.add(tnFree);
            ToolTipManager.sharedInstance().registerComponent(tInfo);
            Icons[0] = new ImageIcon("Images/jaloc_fc_main_icon.gif");
            tcr = new JALOCTreeCellRenderer(Icons, this);
            tInfo.setCellRenderer(tcr);
            p.add(tInfo,BorderLayout.CENTER);
            f2.setVisible(true);
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.tree.*;
    public class JALOCTreeCellRenderer extends DefaultTreeCellRenderer {
        Icon Icons[];
        DisplayMap disp;
        public JALOCTreeCellRenderer(Icon[] icons, DisplayMap dm) {
            this.Icons = icons;
            this.disp = dm;
        public Component getTreeCellRendererComponent(
                            JTree tree,
                            Object value,
                            boolean sel,
                            boolean expanded,
                            boolean leaf,
                            int row,
                            boolean hasFocus) {
            super.getTreeCellRendererComponent(
                            tree, value, sel,
                            expanded, leaf, row,
                            hasFocus);
            if (disp.tnRoot.equals(value)) {
                setIcon(Icons[0]);
                setToolTipText("This book is in the Tutorial series.");
            } else {
                setToolTipText(null); //no tool tip
            return this;
    }

    Yes that functions fine.Hm, I'm not completely awake yet ;-) but I checked with my own code
    and the only difference I see is that I return what the super call had
    returned; something like this: Component result= super.getTreeCellRendererComponent(
                            tree, value, sel,
                            expanded, leaf, row,
                            hasFocus);
            if (disp.tnRoot.equals(value)) {
                setIcon(Icons[0]);
                setToolTipText("This book is in the Tutorial series.");
            } else {
                setToolTipText(null); //no tool tip
            return result;Give it a try ...
    kind regards,
    Jos

  • Setting the color of a particular node in JTree

    Hi all!
    I'm new to Java and facing a problem with JTrees.
    I'm using the following snippet to create a JTree :
    DeafultMutableTreeNode root = new DefaultMutableTeeNode("Letters");
    DeafultMutableTreeNode parent1 = new DefaultMutableTeeNode("uppercase");
    DeafultMutableTreeNode parent2 = new DefaultMutableTeeNode("lowercase");
    DeafultMutableTreeNode child1 = new DefaultMutableTeeNode("A");
    DeafultMutableTreeNode child2 = new DefaultMutableTeeNode("B");
    DeafultMutableTreeNode child3 = new DefaultMutableTeeNode("C");
    DeafultMutableTreeNode child4 = new DefaultMutableTeeNode("a");
    DeafultMutableTreeNode child5 = new DefaultMutableTeeNode("b");
    DeafultMutableTreeNode child6 = new DefaultMutableTeeNode("c");
    root.add(parent1);
    root.add(parent2);
    parent1.add(child1);
    parent1.add(child2);
    parent1.add(child3);
    parent2.add(child4);
    parent2.add(child5);
    parent2.add(child6);
    JTree tree = new JTree(root);
    JScrollPane pane = new JScrollPane(tree);
    TreePath path = tree.getNextMatch("lowercase", 0, javax.swing.text.Position.Bias.Forward);
    tree.setSelectionPath(path);
    tree.expandPath(path);
    In the above JTree, the node labeled "lowercase" gets selected and expanded as soon as the program is run.
    What i want to know is that, is there any way in which i can set only the "lowercase" node's background to a different color (say, YELLOW) and have it so, no matter if the node is expanded or collapsed or other nodes are expanded or collapsed.
    ps: Sample snippet would really b helpful.

    You need to write your own renderer for this.
    Read the tutorial:
    http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html#display

  • Setting the color of a node in JTree

    Hi all!
    I'm new to Java and facing a problem with JTrees.
    I'm using the following snippet to create a JTree :
    DeafultMutableTreeNode root = new DefaultMutableTeeNode("Letters");
    DeafultMutableTreeNode parent1 = new DefaultMutableTeeNode("uppercase");
    DeafultMutableTreeNode parent2 = new DefaultMutableTeeNode("lowercase");
    DeafultMutableTreeNode child1 = new DefaultMutableTeeNode("A");
    DeafultMutableTreeNode child2 = new DefaultMutableTeeNode("B");
    DeafultMutableTreeNode child3 = new DefaultMutableTeeNode("C");
    DeafultMutableTreeNode child4 = new DefaultMutableTeeNode("a");
    DeafultMutableTreeNode child5 = new DefaultMutableTeeNode("b");
    DeafultMutableTreeNode child6 = new DefaultMutableTeeNode("c");
    root.add(parent1);
    root.add(parent2);
    parent1.add(child1);
    parent1.add(child2);
    parent1.add(child3);
    parent2.add(child4);
    parent2.add(child5);
    parent2.add(child6);
    JTree tree = new JTree(root);
    JScrollPane pane = new JScrollPane(tree);
    TreePath path = tree.getNextMatch("lowercase", 0, javax.swing.text.Position.Bias.Forward);
    tree.setSelectionPath(path);
    tree.expandPath(path);
    In the above JTree, the node labeled "lowercase" gets selected and expanded as soon as the program is run.
    What i want to know is that, is there any way in which i can set only the "lowercase" node's background to a different color (say, YELLOW) and have it so, no matter if the node is expanded or collapsed or other nodes are expanded or collapsed.
    ps: Sample snippet would really b helpful.

    You need to write your own renderer for this.
    Read the tutorial:
    http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html#display

  • How to dislay a frame when i clck on the node or leaf on the JTree

    Hi All,
    Iam doing project on swing. In that iam doing with JTree. The Jtree is displaying on the left side of the splitpane .My problem is if i click on the nodes or leaf in the JTree one panel should be open in on the right side of the spltpane. Iam pasting the code.please can any body knows the solution please send me.
    regards
    sonali
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.BorderFactory;
    import javax.swing.border.*;
    import java.awt.event.*;
    import javax.swing.tree.*;
    import javax.swing.JTree;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.event.TreeSelectionListener;
    import javax.swing.event.TreeSelectionEvent;
    import javax.swing.tree.TreeSelectionModel;
    import javax.swing.tree.DefaultTreeCellRenderer;
    import java.net.URL;
    import javax.swing.*;
    import java.io.IOException;
    import javax.swing.JEditorPane;
    import javax.swing.JScrollPane;
    import javax.swing.JSplitPane;
    import javax.swing.JFrame;
    import Test.Test1;
    public class JFrame1 extends javax.swing.JFrame {
    JScrollPane sp;
    JPanel topPanel;
    JPanel leftPanel;
    JPanel bottomPanel;
    JPanel rightPanel;
    Border b ;
    JTree tree;
    DefaultTreeModel treeModel;
         public JFrame1(){
              getContentPane().setLayout(null);
              setSize(405,305);
              setVisible(false);
              JPanel1.setLayout(null);
              getContentPane().add(JPanel1);
              JPanel1.setLayout(new GridLayout(1, 1));
              JPanel1.setBounds(0,0,405,305);
              JPanel1.add(JSplitPane1);
              JSplitPane1.setBounds(0,5,405,305);
              JSplitPane1.setContinuousLayout(false);
              topPanel = new JPanel();
              leftPanel = new JPanel();
              bottomPanel = new JPanel();
              topPanel.setLayout(new FlowLayout());
              leftPanel.setLayout(null);
              b = new EtchedBorder();
              leftPanel.setBorder(b);
              leftPanel.setBounds(0,0,300,305);
              bottomPanel.setLayout(new FlowLayout());
              rightPanel = new JPanel();
              rightPanel.setBounds(108, 0, 300, 305);
              JSplitPane1.setRightComponent(rightPanel);
              JSplitPane1.setLeftComponent(leftPanel);
              rightPanel.setLayout(null);
              topPanel.setBounds(0,0,305,25);
              b = new EtchedBorder(0);
              topPanel.setBorder(b);
              bottomPanel.setBounds(0,26,300,280);
              b = new BevelBorder(1, Color.black,Color.black);
              bottomPanel.setBorder(b);
              rightPanel.add(topPanel);
              rightPanel.add(bottomPanel);
              JSplitPane1.setDividerLocation(120);
              DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
    DefaultMutableTreeNode subroot = new DefaultMutableTreeNode("SubRoot");
    DefaultMutableTreeNode leaf1 = new DefaultMutableTreeNode("Leaf 1");
    DefaultMutableTreeNode leaf2 = new DefaultMutableTreeNode("Leaf 2");
    DefaultMutableTreeNode leaf3 = new DefaultMutableTreeNode("Leaf 3");
    DefaultMutableTreeNode subroot1 = new DefaultMutableTreeNode("SubRoot1");
    DefaultMutableTreeNode leaf11 = new DefaultMutableTreeNode("Leaf 11");
    DefaultMutableTreeNode leaf21 = new DefaultMutableTreeNode("Leaf 21");
    DefaultMutableTreeNode leaf31 = new DefaultMutableTreeNode("Leaf 31 skfjdsf sfjfdsl fslfkjdsfl sflkjaflksdfdslfjds lfsdlkfjdslk sdsdlkfjdsla fflkdasjfdslkjal");
    DefaultMutableTreeNode subroot2 = new DefaultMutableTreeNode("SubRoot2");
    DefaultMutableTreeNode leaf12 = new DefaultMutableTreeNode("Leaf 12");
    DefaultMutableTreeNode leaf22 = new DefaultMutableTreeNode("Leaf 22");
    DefaultMutableTreeNode leaf32 = new DefaultMutableTreeNode("Leaf 32");
    treeModel = new DefaultTreeModel(root);
    tree = new JTree(treeModel);
    treeModel.insertNodeInto(subroot, root, 0);
    treeModel.insertNodeInto(leaf1, subroot, 0);
    treeModel.insertNodeInto(leaf2, root, 1);
    treeModel.insertNodeInto(leaf3, root, 2);
    treeModel.insertNodeInto(subroot1, root, 3);
    treeModel.insertNodeInto(leaf11, subroot1, 0);
    treeModel.insertNodeInto(leaf21, subroot1, 1);
    treeModel.insertNodeInto(leaf31, subroot1, 2);
    treeModel.insertNodeInto(subroot2, root, 3);
    treeModel.insertNodeInto(leaf12, subroot2, 0);
    treeModel.insertNodeInto(leaf22, subroot2, 1);
    treeModel.insertNodeInto(leaf32, subroot2, 2);
    sp = new JScrollPane();
    sp.setBounds(0,0,300,305);
    sp.getViewport().add(tree);
    sp.setViewportBorder(BorderFactory.createRaisedBevelBorder());
    leftPanel.add(sp,BorderLayout.CENTER);
    //          SymPropertyChange lSymPropertyChange = new SymPropertyChange();
    //          JSplitPane1.addPropertyChangeListener(lSymPropertyChange);
              SymWindow aSymWindow = new SymWindow();
              this.addWindowListener(aSymWindow);
              SymComponent aSymComponent = new SymComponent();
              this.addComponentListener(aSymComponent);
         public JFrame1(String sTitle)
              this();
              setTitle(sTitle);
         public void setVisible(boolean b)
              if (b)
                   setLocation(50, 50);
              super.setVisible(b);
         static public void main(String args[])
              (new JFrame1()).setVisible(true);
         public void addNotify()
              Dimension size = getSize();
              super.addNotify();
              if (frameSizeAdjusted)
                   return;
              frameSizeAdjusted = false;
              Insets insets = getInsets();
              javax.swing.JMenuBar menuBar = getRootPane().getJMenuBar();
              int menuBarHeight = 0;
              if (menuBar != null)
                   menuBarHeight = menuBar.getPreferredSize().height;
              setSize(insets.left + insets.right + size.width, insets.top + insets.bottom + size.height + menuBarHeight);
         boolean frameSizeAdjusted = false;
         javax.swing.JPanel JPanel1 = new javax.swing.JPanel();
         javax.swing.JSplitPane JSplitPane1 = new javax.swing.JSplitPane();
         /*class SymPropertyChange implements java.beans.PropertyChangeListener{
              public void propertyChange(java.beans.PropertyChangeEvent event){
                   Object object = event.getSource();
                   if (object == JSplitPane1)
                        JSplitPane1_propertyChange(event);
    /*     void JSplitPane1_propertyChange(java.beans.PropertyChangeEvent event){
         System.out.println("!");
         sp.setSize((int)JSplitPane1.getLeftComponent().getBounds().getWidth()-2,(int)JSplitPane1.getLeftComponent().getBounds().getHeight()-2 );
         sp.repaint();
         tree.repaint();
         class SymWindow extends java.awt.event.WindowAdapter{
              public void windowActivated(java.awt.event.WindowEvent event){
                   Object object = event.getSource();
                   if (object == JFrame1.this)
                        JFrame1_windowActivated(event);
              public void windowIconified(java.awt.event.WindowEvent event){
                   Object object = event.getSource();
                   if (object == JFrame1.this)
                        JFrame1_windowIconified(event);
         void JFrame1_windowIconified(java.awt.event.WindowEvent event){
         void JFrame1_windowActivated(java.awt.event.WindowEvent event){
         class SymComponent extends java.awt.event.ComponentAdapter{
              public void componentShown(java.awt.event.ComponentEvent event){
                   Object object = event.getSource();
                   if (object == JFrame1.this)
                        JFrame1_componentShown(event);
              public void componentResized(java.awt.event.ComponentEvent event){
                   Object object = event.getSource();
                   if (object == JFrame1.this)
                        JFrame1_componentResized(event);
         void JFrame1_componentResized(java.awt.event.ComponentEvent event){
         System.out.println("!!");
              JPanel1.setSize(this.getContentPane().getMaximumSize());
              JSplitPane1.setSize((int)JPanel1.getMaximumSize().getWidth()-5,(int)JPanel1.getMaximumSize().getHeight()-5 );
              topPanel.setBounds(0, 0, rightPanel.getWidth()-3, 25);
              bottomPanel.setBounds(0,26,rightPanel.getWidth()-3,rightPanel.getHeight()-28);
              sp.setSize((int)leftPanel.getMaximumSize().getWidth(),(int)leftPanel.getMaximumSize().getHeight());
         sp.setAutoscrolls(true);
         //sp.setSize((int)JSplitPane1.getLeftComponent().getBounds().getWidth()-2,(int)JSplitPane1.getLeftComponent().getBounds().getHeight()-2 );
         sp.repaint();
         //sp.setSize((int)leftPanel.getSize().getWidth(),(int)leftPanel.getSize().getHeight() );
         void JFrame1_componentShown(java.awt.event.ComponentEvent event){
         System.out.println("!!!");
              JPanel1.setSize(this.getContentPane().getMaximumSize());
              JSplitPane1.setSize((int)JPanel1.getMaximumSize().getWidth()-5,(int)JPanel1.getMaximumSize().getHeight()-5 );
              topPanel.setBounds(0, 0, rightPanel.getWidth()-3, 25);
         sp.setSize((int)JSplitPane1.getLeftComponent().getBounds().getWidth()-2,(int)JSplitPane1.getLeftComponent().getBounds().getHeight()-2 );
         sp.repaint();
              bottomPanel.setBounds(0,26,rightPanel.getWidth()-3,rightPanel.getHeight()-28);
    }

    hi sculz,
    i develped the tree, and tree selection listener also the 4 th point i didnt able to do . here is the code belo what i did.
    iam not able to add the JSplit pane also.
    import java.io.*;
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.tree.*;
    import javax.swing.event.*;
    public class Tree08 extends JFrame{
    Hashtable theHashtable = new Hashtable();
    int frameWidth = 300;
    int frameHeight = 305;
    int numberRows = 19;
    JTree tree;
    JPanel treePanel;
    String plafClassName =
    "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
    For future reference, the three plaf implementations
    are:
    com.sun.java.swing.plaf.metal.MetalLookAndFeel
    com.sun.java.swing.plaf.motif.MotifLookAndFeel
    com.sun.java.swing.plaf.windows.WindowsLookAndFeel
    JLabel display;
    public static void main(String args[]) {
    new Tree08();
    public Tree08 () {
    theHashtable.put("Cabbage","Good in slaw");
    theHashtable.put("Squash","The yellow variety");
    theHashtable.put("Onion","Not too hot");
    theHashtable.put("Squirrel","Likes to store nuts");
    theHashtable.put("Rabbit","Runs fast");
    theHashtable.put("Fox","Crazy like a ...");
    theHashtable.put("Horse","Fun to ride");
    theHashtable.put("Pig","Lives in mud");
    theHashtable.put("Cow","Gives us milk");
    theHashtable.put("Peach","From Georgia");
    theHashtable.put("Grape","Saueeze into wine");
    theHashtable.put("Apple","Red delicious");
    theHashtable.put("Orange","Very juicy");
    DefaultMutableTreeNode[] theTreeNodes = new
    DefaultMutableTreeNode[numberRows];
    theTreeNodes[0] = new DefaultMutableTreeNode("Sample
    Tree");
    theTreeNodes[1] = new
    DefaultMutableTreeNode("Vegetables");
    theTreeNodes[2] = new
    DefaultMutableTreeNode("Cabbage");
    theTreeNodes[3] = new
    DefaultMutableTreeNode("Squash");
    theTreeNodes[4] = new
    DefaultMutableTreeNode("Onion");
    theTreeNodes[5] = new
    DefaultMutableTreeNode("Animals");
    theTreeNodes[6] = new
    DefaultMutableTreeNode("Forrest");
    theTreeNodes[7] = new
    DefaultMutableTreeNode("Squirrel");
    theTreeNodes[8] = new
    DefaultMutableTreeNode("Rabbit");
    theTreeNodes[9] = new DefaultMutableTreeNode("Fox");
    theTreeNodes[10] = new
    DefaultMutableTreeNode("Farm");
    theTreeNodes[11] = new
    DefaultMutableTreeNode("Horse");
    theTreeNodes[12] = new DefaultMutableTreeNode("Pig");
    theTreeNodes[13] = new DefaultMutableTreeNode("Cow");
    theTreeNodes[14] = new
    DefaultMutableTreeNode("Fruit");
    theTreeNodes[15] = new
    DefaultMutableTreeNode("Peach");
    theTreeNodes[16] = new
    DefaultMutableTreeNode("Grape");
    theTreeNodes[17] = new
    DefaultMutableTreeNode("Apple");
    theTreeNodes[18] = new
    DefaultMutableTreeNode("Orange");
    theTreeNodes[0].add(theTreeNodes[1]);
    theTreeNodes[1].add(theTreeNodes[2]);
    theTreeNodes[1].add(theTreeNodes[3]);
    theTreeNodes[1].add(theTreeNodes[4]);
    theTreeNodes[0].add(theTreeNodes[5]);
    theTreeNodes[5].add(theTreeNodes[6]);
    theTreeNodes[6].add(theTreeNodes[7]);
    theTreeNodes[6].add(theTreeNodes[8]);
    theTreeNodes[6].add(theTreeNodes[9]);
    theTreeNodes[5].add(theTreeNodes[10]);
    theTreeNodes[10].add(theTreeNodes[11]);
    theTreeNodes[10].add(theTreeNodes[12]);
    theTreeNodes[10].add(theTreeNodes[13]);
    theTreeNodes[0].add(theTreeNodes[14]);
    theTreeNodes[14].add(theTreeNodes[15]);
    theTreeNodes[14].add(theTreeNodes[16]);
    theTreeNodes[14].add(theTreeNodes[17]);
    theTreeNodes[14].add(theTreeNodes[18]);
    treePanel = new TreePanel(theTreeNodes[0]);
    tree.addTreeSelectionListener(new MyTreeListener());
    Container content = getContentPane();
    content.add(treePanel,BorderLayout.CENTER);
    display = new JLabel("Display Selection Here");
    content.add(display,BorderLayout.SOUTH);
    setSize(frameWidth, frameHeight);
    setTitle("Copyright 1998, R.G.Baldwin");
    setVisible(true);
    this.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    System.exit(0);}
    class MyTreeListener implements TreeSelectionListener{
    public void valueChanged(TreeSelectionEvent e) {
    DefaultMutableTreeNode theNode =
    (DefaultMutableTreeNode)
    (e.getPath().getLastPathComponent());
    if (theNode.isLeaf()) {
    display.setText((String)theHashtable.get(
    theNode.getUserObject()));
    class TreePanel extends JPanel {
    public TreePanel(TreeNode root){
    setLayout(new BorderLayout());
    tree = new JTree(root);
    JScrollPane sp = new JScrollPane(tree);
    add(sp, BorderLayout.CENTER);
    try{
    UIManager.setLookAndFeel(plafClassName);
    }catch(Exception ex){System.out.println(ex);}
    SwingUtilities.updateComponentTreeUI(this);
    for(int cnt = 0; cnt < numberRows; cnt++){
    tree.expandRow(cnt);
    regards
    sonali

Maybe you are looking for

  • Mail.app is COPYING instead of MOVING mail to folders (using IMAP)

    Hi there, I hope I can keep this simple, I'll go by the steps leading to my propblem: 1. I've been using POP3 mail for a long time, mainly with Outlook on PC's and now that I have made the godly switch to Apple I've been using Mail.app for a couple m

  • Can a iPad iso 5.1.1 upgrade to 6.0

    Can and I pad iso 5.1.1 upgrade to 6.0

  • H430 CPU Upgrade

    Bit irritated now. I know I have a h61 Chipset, but there is NO information online about the lenovo "Mahobay" motherboard. None. Nada. I have no idea whether my motherboard is capable of any upgrades, LET ALONE a new CPU, as my crappy Pentium G640 is

  • Revision number in forms.

    Hi, I have a series of forms that are stored in a subversion repository.  I want to be able to display the repository's version onto the form.  Does anyone know if this is possible?  We can do this with SSRS reports and stored procedures but this is

  • CD-RW Drive Tray Opens on boot!

    ANNOYANCE~ Why does this happen? Does anybody know? I recently purchased an MSI 52x32x52 CD/W/RW drive and every time i turn on the computer, it spits out the drive tray! It doesn't matter if there is a cd in it or not, it still spits it out. How do