Bom pessoal,
Depois de quebrar um pouco a cabeça, consegui resolver meu problema. Segue abaixo o código:
public void setSelectedNode(File file)
{
try
{
String path = file.getPath();
ShellFolder shellFolder = ShellFolder.getShellFolder(file);
// Pastas de Sistema
if (file.getPath().contains("::"))
{
path = shellFolder.getDisplayName();
}
TreePath treePath = tree.getNextMatch(path, 1, Position.Bias.Forward);
tree.setSelectionPath(treePath);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
Ao executar o comando…
tree.setSelectionPath(treePath);
…o listener abaixo é chamado
tree.addTreeSelectionListener(new TreeSelectionListener()
{
public void valueChanged(TreeSelectionEvent event)
{
File file = (File) tree.getLastSelectedPathComponent();
tree.expandPath(tree.getSelectionPath());
if (file != null)
{
populateTable(); //popula minha JTable com os filhos do diretorio selecionado
}
}
});
Como nada na vida é fácil, me deparei com outro problema.
Já que dentro do listener da tree temos o comando…
tree.expandPath(tree.getSelectionPath());
…o scroll da minha tree que esta dentro de um JScrollPane não acompanha os nodes que estão sendo selecionados e expandindo.
Já fiz alguns testes e descobri que se eu tirar o comando de expandir a tree de dentro do listener, o node expandido fica sempre visivel no
topo.
Como fazer para que o node expandido fique sempre no topo, sem ter que tirar tal comando de dentro do listener?