posted 18 years ago
As you may be able to tell from the headline, I am a bit of a newbie at this. What I am trying to to is create a very basic GUI that contains a directory JTree and some other components. I have successfully build the tree, but I am trying to add it to a specific position in my frame. I added the tree to a JScrollPane so that the user can scroll around the tree. So I assume that by setting the parameters of JScrollPane through the inherited setBounds() command of the Component class, I can control where my JScrollPane and hence my tree appears within the frame. But for some reason my code compiles, but the directory tree appears in the top left hand corner of the screen. My relevant code is below. Also, if I want to create a simple button and specify its position in the frame along with the physical dimensions of the button (height, width), do I simply call the setBounds command to position the button within my frame? It is necessary to add the button to a container or does the button class provide its own container?
class SimpleTree extends JFrame
{
public SimpleTree(JFrame frame)
{
DefaultMutableTreeNode top =
new DefaultMutableTreeNode("The World");
CreateNodes(top); //CreateNodes adds nodes to "top", the root of my tree.
JTree tree = new JTree(top);
tree.getSelectionModel().setSelectionMode
(TreeSelectionModel.SINGLE_TREE_SELECTION);
JScrollPane treeView = new JScrollPane(tree);
treeView.setHorizontalScrollBarPolicy(
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
treeView.setVerticalScrollBarPolicy(
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
treeView.setBounds(100,100, 50,50);
JViewport treeport = treeView.getViewport();
//treeport.add(tree);
//treeView.setViewport(treeport);
//Dimension treesize = new Dimension(treewidth, treeheight);
//treeport.setViewSize(treesize);
//Point treeposition = new Point(treex, treey);
//treeport.setViewPosition(treeposition);
frame.getContentPane().add(treeView);
treeport.setBounds(100,100, 50,50);
}