This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes SpringLayout applied to a JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "SpringLayout applied to a JPanel" Watch "SpringLayout applied to a JPanel" New topic
Author

SpringLayout applied to a JPanel

Austin Henggi
Greenhorn

Joined: Jul 08, 2005
Posts: 10
I have a JTree which is inside a JScrollPane which is inside a JPanel that I want to put inside of a JFrame. The tree appears in the center of my frame as it should before I apply my layout. I create a springlayout and then set the layout to my FRAME. I then set up springs to add constraints on where I want my panel to appear, but no matter what I do, the panel always appears in the top right-hand corner of my screen. I am not sure why. My code is below, any suggestions?

public SimpleTree(JFrame frame, SpringLayout layout)
{
DefaultMutableTreeNode top =
new DefaultMutableTreeNode("The World");
CreateNodes(top);
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);
JPanel treePanel = new JPanel();
treePanel.add(treeView);
frame.setLayout(layout);
treePanel.setBounds(100,100,50,50);
frame.getContentPane().add(treePanel);
Spring s = Spring.constant(200,300,800);
layout.putConstraint(SpringLayout.EAST, frame, s, SpringLayout.EAST, treePanel);
layout.putConstraint(SpringLayout.WEST, frame, s, SpringLayout.WEST, treePanel);
layout.putConstraint(SpringLayout.NORTH, frame, s, SpringLayout.NORTH, treePanel);
layout.putConstraint(SpringLayout.SOUTH, frame, s, SpringLayout.SOUTH, treePanel);
}
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: SpringLayout applied to a JPanel
 
Similar Threads
My .PNG background will not export ||THIS HAS BEEN RESOLVED!!! THANKS!!||
SpringLayout - handcoding
passing value from jcombobox to other classes?
SpringLayout Question - (code snipet inside)
JLabel and JTextArea do not show properly in ubuntu