posted 18 years ago
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);
}