This is partial of my code:
JPanel myPanel = new JPanel();
myPanel.setLayout( new GridBagLayout() );
GridBagConstraints gbc= new GridBagConstraints();
JTree myTree = new JTree();
myTree.addMouseListener(this);
myTree.getSelectionModel().setSelectionMode(TreeSe lectionModel.SINGLE_TREE_SELECTION);
EMyObject basicRoot = new EMyObject();
TreeModel treeModel = new DefaultTreeModel(new DefaultMutableTreeNode(basicRoot));
myTree.setModel(treeModel);
myTree.setCellRenderer(getTreeCellRenderer());
myTree.addTreeSelectionListener(this);
JScrollPane treeScrollPanel = new JScrollPane(myTree);
myPanel.add(treeScrollPanel, gbc);
gbc.gridy++;
JTextArea myTextArea = new JTextArea();
myPanel.add(myTextArea, gbc);
Via this function I catch the change of the selection in the tree and update the JTextArea with the updated message, and then try to return the focus and slection to the selected node. The problem is that when scroll are displayed the selection and focus are done but the scroll do not move to display that node.
public void valueChanged(TreeSelectionEvent e)
{
if (e.getSource() instanceof JTree)
{
String myText = "";
if (e.getNewLeadSelectionPath() != null &&
e.getNewLeadSelectionPath().getLastPathComponent() != null)
{
myText = e.getNewLeadSelectionPath().getLastPathComponent() .toString();
}
myTextArea.setText(myText);
myTree.scrollPathToVisible(e.getNewLeadSelectionPa th());
}
}
Thanks a lot!