JTree scrollPathToVisible() not working

I have a fairly complex JTree that's working great, except when initially populated. The business rules I have to follow state the tree must scroll to the current selection, and more often than not it might be well down into the tree.
The pane has horizontal/vertical scroll bars as necessary, all other scrolling works properly, and the initial selection (ie hilited) is correct, but the pane won't scroll to it. For example, the pane may only have room to display 20 rows, and the initially selected row ends up being row 30, requiring the user to vertically scroll to see it.
Here's the method invoked to set the current selection after the tree model has been populated, and I've verified the currentSelection variable is correct:
private void resetSelection(DefaultMutableTreeNode currentNode)
   TreePath currentSelection = new TreePath( currentNode.getPath() );
   jDirTree.setSelectionPath ( currentSelection );   
   jDirTree.scrollPathToVisible( currentSelection );        
}I've googled, tried various other things like updateUI(), but no luck. Any ideas?

I'm facing the same problem here. I've been searching the web for hours for a solution.
I solved it by calling scrollPathToVisible() or scrollRowToVisible() twice/multiple times.
This helped in my case, though I don't know why.
But maybe it's a workaround for you too until a better solution is available.
Regards,Ren�

Similar Messages

  • SetInvokesStopCellEditing(true) not working

    hi
    setInvokesStopCellEditing(true) in jTree is not working.
    when i set it true it should save the data entered in textfield but it doesnot
    any help
    thanks

    I've found that setting it to true makes JTree save the changes if you click on another node during editing, but hitting the Esc key discards changes regardless of this setting.

  • Custom TreeCellRenderer not working in Program

    The following 2 lines set my TreeCellRenderer
    FSDirectoryCellRenderer renderer1 = new FSDirectoryCellRenderer();
    jtree.setCellRenderer(renderer1);
    Rendering is not working in the Below Program. But If I comment the above 2 lines the default rendering is working. There is a probelm with my rendering class FSDirectoryCellRenderer which I am not able to figure out. PLease help me out
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextField;
    import javax.swing.JTree;
    import javax.swing.UIManager;
    import javax.swing.border.BevelBorder;
    import javax.swing.border.SoftBevelBorder;
    import javax.swing.event.TreeSelectionEvent;
    import javax.swing.event.TreeSelectionListener;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.tree.DefaultTreeCellRenderer;
    import javax.swing.tree.DefaultTreeModel;
    import javax.swing.tree.TreeCellRenderer;
    import javax.swing.tree.TreePath;
    public class UniversityLayout extends JFrame {
         private JTree jtree = null;
         private DefaultTreeModel defaultTreeModel = null;
         private JTextField jtfStatus;
         public UniversityLayout() {
              super("A University JTree Example");
              setSize(300, 300);
              Object[] nodes = buildJTree();
              DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
              renderer.setOpenIcon(new ImageIcon("opened.gif"));
              renderer.setClosedIcon(new ImageIcon("closed.gif"));
              renderer.setLeafIcon(new ImageIcon("leaf.gif"));
              jtree.setCellRenderer(renderer);
              jtree.setShowsRootHandles(true);
              jtree.setEditable(false);
              jtree.addTreeSelectionListener(new UTreeSelectionListener());
              FSDirectoryCellRenderer renderer1 = new FSDirectoryCellRenderer();
              jtree.setCellRenderer(renderer1);
              JScrollPane s = new JScrollPane();
              s.getViewport().add(jtree);
              getContentPane().add(s, BorderLayout.CENTER);
              jtfStatus = new JTextField(); // Use JTextField to allow copy operation
              jtfStatus.setEditable(false);
              jtfStatus.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
              getContentPane().add(jtfStatus, BorderLayout.SOUTH);
              TreePath path = new TreePath(nodes);
              jtree.setSelectionPath(path);
    //          jtree.scrollPathToVisible(path);
         class FSDirectoryCellRenderer extends JLabel implements TreeCellRenderer {
              private Color textSelectionColor;
              private Color textNoSelectionColor;
              private Color backgroundSelectionColor;
              private Color backgroundNoSelectionColor;
              private boolean sel;
              public FSDirectoryCellRenderer() {
                   super();
                   textSelectionColor = UIManager.getColor("Tree.selectionForeground");
                   textNoSelectionColor = UIManager.getColor("Tree.textForeground");
                   backgroundSelectionColor = UIManager
                             .getColor("Tree.selectionBackground");
                   backgroundNoSelectionColor = UIManager
                             .getColor("Tree.textBackground");
                   setOpaque(false);
              public Component getTreeCellRendererComponent(JTree tree, Object value,
                        boolean selected, boolean expanded, boolean leaf, int row,
                        boolean hasFocus) {
                   DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
                   Object obj = node.getUserObject();
                   setText(obj.toString());
                   if (obj instanceof Boolean) {
                        setText("Loading");
                   if (obj instanceof NodeIconData) {
                        NodeIconData nodeIconData = (NodeIconData) obj;
                        if (expanded) {
                             setIcon(nodeIconData.getExpandedIcon());
                        } else {
                             setIcon(nodeIconData.getNormalIcon());
                   } else {
                        setIcon(null);
                   setFont(jtree.getFont());
                   setForeground(selected ? textSelectionColor : textNoSelectionColor);
                   setBackground(selected ? backgroundSelectionColor
                             : backgroundNoSelectionColor);
                   sel = selected;
                   return this;
         private Object[] buildJTree() {
              Object[] nodes = new Object[4];
              DefaultMutableTreeNode root = new DefaultMutableTreeNode(new College(1, "College"));
              DefaultMutableTreeNode parent = root;
              nodes[0] = root;
              DefaultMutableTreeNode node = new DefaultMutableTreeNode(new College(2, "Class 1"));
              parent.add(node);
              parent = node;
              parent.add(new DefaultMutableTreeNode(new College(3, "Section A")));
              parent.add(new DefaultMutableTreeNode(new College(4, "Section B")));
              parent = root;
              node = new DefaultMutableTreeNode(new College(5, "Class 2"));
              parent.add(node);
              nodes[1] = node;
              parent = node;
              node = new DefaultMutableTreeNode(new College(6, "Science"));
              parent.add(node);
    //          nodes[2] = node;
              parent = node;
              parent.add(new DefaultMutableTreeNode(new College(7, "Computer Science")));
              parent.add(new DefaultMutableTreeNode(new College(8, "Information Science")));
              parent = (DefaultMutableTreeNode)nodes[1];
              node = new DefaultMutableTreeNode(new College(9, "Arts"));
              parent.add(node);
              nodes[2] = node;
              parent = (DefaultMutableTreeNode)nodes[2];
              parent.add(new DefaultMutableTreeNode(new College(10, "Drawing")));
              node = new DefaultMutableTreeNode(new College(11, "Painting"));
              parent.add(node);
              nodes[3] = node;
              parent = (DefaultMutableTreeNode)nodes[1];
              parent.add(new DefaultMutableTreeNode(new College(12, "Telecom")));
              defaultTreeModel = new DefaultTreeModel(root);
              jtree = new JTree(defaultTreeModel);
              return nodes;
         public static void main(String argv[]) {
              UniversityLayout frame = new UniversityLayout();
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.setVisible(true);
         class UTreeSelectionListener implements TreeSelectionListener {
              public void valueChanged(TreeSelectionEvent e) {
                   TreePath path = e.getPath();
                   Object[] nodes = path.getPath();
                   String status = "";
                   for (int k = 0; k < nodes.length; k++) {
                        DefaultMutableTreeNode node = (DefaultMutableTreeNode) nodes[k];
                        College nd = (College) node.getUserObject();
                        status += "." + nd.getId();
                   jtfStatus.setText(status);
    class College {
         protected int id;
         protected String name;
         public College(int id, String name) {
              this.id = id;
              this.name = name;
         public int getId() {
              return id;
         public String getName() {
              return name;
         public String toString() {
              return name;
    }

    It works fine for me (after commenting out the part about NodeIconData, whose source you didn't provide). What is the behavior you expect to see but don't? Is it that when you click on a tree node, it doesn't appear "selected?"
    Why not extends DefaultTreeCellRenderer? You appear to be doing minimal configuring (sometimes changing the text and/or icon of the node).
         class FSDirectoryCellRenderer extends DefaultTreeCellRenderer {
              public FSDirectoryCellRenderer() {
                   super();
                   setOpaque(false);
              public Component getTreeCellRendererComponent(JTree tree, Object value,
                        boolean selected, boolean expanded, boolean leaf, int row,
                        boolean hasFocus) {
                   super.getTreeCellRendererComponent(tree, value, selected, expanded,
                                       leaf, row, hasFocus);
                   DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
                   Object obj = node.getUserObject();
                   setText(obj.toString());
                   if (obj instanceof Boolean) {
                        setText("Loading");
    //               if (obj instanceof NodeIconData) {
    //                    NodeIconData nodeIconData = (NodeIconData) obj;
    //                    if (expanded) {
    //                         setIcon(nodeIconData.getExpandedIcon());
    //                    } else {
    //                         setIcon(nodeIconData.getNormalIcon());
    //               } else {
                        setIcon(null);
                   return this;
         }

  • JTree: how not to render the "tree connecting lines"

    I have a JTree.
    I would like that my custom tree renderer (using windows LAF) did not paint the dotted line that connect the hierarchy nodes. setting the icon to null in the default renderer only do not paint the folder. How can I disable also the painting of the dotted line?
    using
    tree.putClientProperty("JTree.lineStyle", "None")
    I disable it for the whole tree.
    First of all this do not work on Windows LAF. Then I'd like to disable only on certain branches or nodes, that are somehow as "empty containers", while keeping hte default feature for the "good branches".
    any idea?
    thanks
    Edited by: Davide_Gesino on Nov 8, 2009 5:24 AM

    hii,
    one from possible:
    tree.putClientProperty("JTree.lineStyle", "Horizontal"); a lots examples (runnable) at http://www.java2s.com/Code/Java/Swing-JFC/Tree.htm
    ... kopik

  • ExecuteUpdate() is not working

    I have a Statement and I am calling the executeUpdate() method to execute some SQL into my database. It says I'm using it from a static context, while my calling method is not!
    public void createTable()     {
    // describe Connection con, open connection pool, then
    Statement statement = con.createStatement(
         ResultSet.TYPE_SCROLL_SENSITIVE,
         ResultSet.CONCUR_UPDATABLE          );
    Statement.executeUpdate(     "CREATE TABLE newTable ();"     );
    non-static method executeUpdate(java.lang.String) cannot be referenced from a static context
    Statement.executeUpdate(     "CREATE TABLE newTable ();"     );
    I'm out of clues.

    Duh! At 2AM one is not working at optimum capacity...
    Additional question, I hope this one is not out of line.
    How can I create an empty table? The code above is not working for that. Does not give an error but I can't see a new table. If I add some code to create some columns, the table appears, but not the empty () one.
    And is it possible to create a table without a name? The GUI would prompt the user for the name after the creation (it would actually have to appear on a JTree component, with the focus on the name of the table selected)..

  • POST method of form not working

    I am using the post method in a HTML form the action
    attribute points to a pl/sql procedure and it is not working
    i get
    SIGNATURE (parameter names) MISMATCH
    VARIABLES IN FORM NOT IN PROCEDURE:
    NON-DEFAULT VARIABLES IN PROCEDURE NOT IN FORM:
    in IE 5.0
    the same code works if client is Netscape !!
    and if i change to GET method that too works
    any suggestions
    tia,
    -amol

    Hi,
    I have the following scenario:
    A Swing-Application is using JClient to bind to bc4j. The JUStatusBar works fine for the forms which have an iterator. But if I show a form with a JTree, e.g., I don't want the message "3 out of 4000 rows" to be displayed in the status bar... because it is a tree...You can install a custom StatusBar and override displayStatus() methods to ignore iterBinding that's bound to JTree.
    something like:
    JUStatusBar statusBar = new JUStatusBar(..)
    public void displayStatus(JUIteratorBinding bind, String msg, Object[] params)
    if bind == treebind or an iterator that's displayed in the tree then return
    else call super.
    I thought, the status bar should implement this behaviour, it displays a "iterator released" message, but the label from the form before the JTree-form was coming up isn't cleaned up.
    Any suggestion for me what to do?
    Torsten.

  • Custom Scriptmaps Not Working in DW6

    I use custom scriptmaps. I have modified every version of DW going back to Macromedia. The process is outlined here:
    http://helpx.adobe.com/dreamweaver/kb/change-add-recognized-file-extensions.html
    I made these changes for DW6 and I can open the files, but the soure code formatting is not working.
    DW6 does not recognize the page as HTML.
    Thank you.

    It works fine for me (after commenting out the part about NodeIconData, whose source you didn't provide). What is the behavior you expect to see but don't? Is it that when you click on a tree node, it doesn't appear "selected?"
    Why not extends DefaultTreeCellRenderer? You appear to be doing minimal configuring (sometimes changing the text and/or icon of the node).
         class FSDirectoryCellRenderer extends DefaultTreeCellRenderer {
              public FSDirectoryCellRenderer() {
                   super();
                   setOpaque(false);
              public Component getTreeCellRendererComponent(JTree tree, Object value,
                        boolean selected, boolean expanded, boolean leaf, int row,
                        boolean hasFocus) {
                   super.getTreeCellRendererComponent(tree, value, selected, expanded,
                                       leaf, row, hasFocus);
                   DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
                   Object obj = node.getUserObject();
                   setText(obj.toString());
                   if (obj instanceof Boolean) {
                        setText("Loading");
    //               if (obj instanceof NodeIconData) {
    //                    NodeIconData nodeIconData = (NodeIconData) obj;
    //                    if (expanded) {
    //                         setIcon(nodeIconData.getExpandedIcon());
    //                    } else {
    //                         setIcon(nodeIconData.getNormalIcon());
    //               } else {
                        setIcon(null);
                   return this;
         }

  • JTree.getNextMatch() only works on visible nodes?  Any alternatives?

    I'm trying to search for and select a node in the tree based on user input. I was trying to use JTree.getNextMatch(), but that method returns a NULL TreePath if the node being searched for is not currently visible (if this is, indeed, the case, why isn't this noted in the API documentation???)
    Anyway, is there a method that returns the path of a node, whether visible or not? I could then use that info to make the path visible using JTree.scrollPathToVisible(path).
    I found a link at
    http://www.exampledepot.com/egs/javax.swing.tree/FindNode.html
    that describes various methods, including getNextMatch() - and from the comments in that code, I got the notion that this is only for visible nodes. But I was hoping that since 2003, there have been additional methods in the standard toolkit which I can use rather than manually descend the tree myself.

    If you're using DefaultMutableTreeNodes, you can use the depthFirstEnumeration method, API is here: http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/tree/DefaultMutableTreeNode.html#depthFirstEnumeration()

  • Not work tablet UI on Prestigio 5080 PRO tablet

    I read that browser.ui.layout.tablet = "1" can fix this problem. But it not works. I can work only in pnone interface that is not good for my 8'' tablet.

    Would it be possible for you to share the problematic pdf and OS information  with us at [email protected] so that we may investigate?
    Thanks,
    Adobe Reader Team

  • Why self-defined access sequences of free goods can not work?

    Hi gurus,
    I have maintained access sequences of free goods self-defined.but when i creat the SO it does not work!
    when i used the standard access sequences ,it is OK .
    Can anybody tell me why?
    thanks in advance

    Dear Sandy,
    Go to V/N1 transaction select your self defined access sequence then go in to the accesses and fields and check all fields are activated.
    Make sure that these fields are flowing in your sales order.
    I hope this will help you,
    Regards,
    Murali.

  • Adobe bridge raw not working with windows vista in photoshop cc, why?

    adobe bridge raw not working in photoshop cc, is there a fix?

    Your sure your using photoshop cc on windows vista?
    I was under the impression that photoshop cc would not even install on windows vista.
    What version of camera raw do you have?
    In photoshop under Help>About Plugin does it list Camera Raw and if so which version is it?
    (click on the words Camera Raw to see the version)
    Camera raw doesn't work if it's a camera raw file or some other file type such as jpeg or tif?
    What camera are the camera raw files from?
    Officially camera raw 8.3 is the latest version of camera raw that will work on windows vista.

  • Adobe Bridge CS5 in windows 7 not working?

    Adobe Bridge CS5 in windows 7 not working. I was using bridge perfectly for last 2 years. It stops working since 3 days. I tried to install updates. Showing some error to install.
    Tried to install creative cloud..again some error. Error code : 82
    Could you please advice how I can fix my adobe bridge.

    https://www.youtube.com/watch?v=xDYpTOoV81Q&feature=youtu.be
    please check this video I uploaded..this is what happens when I click adobe bridge.. just blinks and go off. bridge not working on task manager

  • ADOBE CLOUD ON MY DESKTOP WILL NOT WORK. IT LOADS UP BUT NOTHING FILLS THE WINDOW

    ADOBE CLOUD ON MY DESKTOP WILL NOT WORK. IT LOADS UP BUT NOTHING FILLS THE WINDOW

    BLANK Cloud Screen http://forums.adobe.com/message/5484303 may help
    -and step by step http://forums.adobe.com/thread/1440508?tstart=0
    -and http://helpx.adobe.com/creative-cloud/kb/blank-white-screen-ccp.html

  • Partner application logoff not working

    We have a partner application registered with sso with custom login screen. The login works fine. We use the following code to logoff the partner application in logoff.jsp
    response.setHeader("Osso-Return-Url", "http://my.oracle.com" );
    response.sendError(470, "Oracle SSO");
    session.invalidate();
    but the logoff is not working properly. It is not invalidating the session and the logout http request is not going from the application server to the sso server.
    Are there any additional configurations for SSO logoff.Any help is appreciated.
    Thanks

    Hi
    The WF should also trigger if i add the Partner function in UI.If i change any Attribute the WF triggers but i dont want to change the attribute when i add the partner function.
    If i have only one event for WF that is Partner Change the WF will not trigger it for the 1st time when i save the UI. But next i come to the same saved doc and add a partner function then the Wf triggers.
    So this means that Partner change is active.
    the issue here is i need to trigger the WF on , the 1st time i save the UI, for which i wil be using Attribute Change and next time when i come back to saved doc the and add only the partner function and no changes are made to attributes the WF should again trigger.
    Thanks
    Tarmeem

  • IPhone 4 Voice Memos not working/saving

    Hi there,
    I'm having trouble with my voice memos too. Up until yesterday they were working fine and now, even though the record button works, the stop button does not and I can only pause them. Worse again is that the button to go into the menu to view all voice memos is not working so I can't play them from my iPhone and nothing new is saving to my iTunes. Please help!

    I've always had the "Include Voice Memos" option selected. I think that only pertains to syncing voice memos from iTunes to the iPhone after it has been copied to iTunes. It has to be the new OS/iTunes not communicating that new memos have been recorded. For some reason they won't sync when I want them to, and then a few syncs later they magically appear.
    By the way, I'm VERY comfortable with the iTunes and iPhone systems. I've been using iTunes for 5 years, and I've been recording class lectures with the iPhone voice memo app (and another app) for a couple years. It's not an error of not seeing that the memos were added; they don't exist in my library or music folders.
    JUST OUT OF CURIOSITY, POST WHICH FIRMWARE YOU ARE RUNNING EXACTLY!!!
    I'm on an iPhone 4, running firmware 4.0.1

Maybe you are looking for