• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Setting the container position of JScrollPane

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
 
Austin Henggi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Austin,
Place the JScrollPane in a JPanel. Then add the Panel to the frame.
 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Austin,

Try JFrameBuilder, you will find the answer at once.
 
Austin Henggi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Pat for the response! I will check the program out.
 
reply
    Bookmark Topic Watch Topic
  • New Topic