I tried to add an ability of browsing files to my program. I wanted to use code from here: 1 (Gilbert's answer), but that was important to me to have JTree in certain position and size. Unfortunately, when I did this, the JTree doesn't "respond" when I click on it.
Here's the code:
public class Frame extends JFrame implements Runnable {
private DefaultMutableTreeNode root;
private DefaultTreeModel treeModel;
private JTree tree;
public File fileRoot;
public Frame(){
super("FileBrowser");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setSize(480, 320);
setLocation(50,50);
getContentPane().setLayout(null);
fileRoot = new File("C:/");
root = new DefaultMutableTreeNode(new FileNode(fileRoot));
treeModel = new DefaultTreeModel(root);
tree = new JTree(treeModel);
tree.setBounds(10, 39, 155, 177);
getContentPane().add(tree);
tree.setShowsRootHandles(true);
}
@Override
public void run() {
CreateChildNodes ccn = new CreateChildNodes(fileRoot, root);
new Thread(ccn).start();
}
}
Main class:
public class main {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Frame());
}
}
When I comment
getContentPane().setLayout(null);
and let JTree fill whole Frame, it works as it should