I want to make a component to occupy the maximumAvailableHeight of the Container. In the code that I have pasted below for example, I have made the root frame to be 800,600.
Here is another approach using SpringLayout. I couldn't get Spring.width and Spring.height to work so I used a more verbose way
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.Spring;
import javax.swing.SpringLayout;
import javax.swing.tree.DefaultMutableTreeNode;
public class TestFrame extends JFrame {
private static final long serialVersionUID = 1L;
public TestFrame() {
super("Top Frame");
setDefaultCloseOperation(EXIT_ON_CLOSE);
SpringLayout layout = new SpringLayout();
JTree env = getEnvironmentTree();
env.expandRow(0);
JPanel contentPane = new JPanel(layout);
contentPane.setLayout(layout);
JScrollPane treePane = new JScrollPane(env);
JTextArea area = new JTextArea("Some contents");
contentPane.add(treePane);
contentPane.add(area);
setLayout(new BorderLayout());
add(contentPane);
setSize(800, 600);
SpringLayout.Constraints cons;
cons = layout.getConstraints(treePane);
cons.setX(Spring.constant(0));
cons.setY(Spring.constant(0));
cons.setWidth(Spring.scale(layout.getConstraint(SpringLayout.EAST, contentPane), .7f));
cons.setHeight(layout.getConstraint(SpringLayout.SOUTH, contentPane));
cons = layout.getConstraints(area);
cons.setX(layout.getConstraint(SpringLayout.EAST, treePane));
cons.setY(Spring.constant(0));
cons.setWidth(Spring.scale(layout.getConstraint(SpringLayout.EAST, contentPane), .3f));
cons.setHeight(layout.getConstraint(SpringLayout.SOUTH, contentPane));
setPreferredSize(getSize());
setLocationRelativeTo(null);
pack();
setVisible(true);
}
private JTree getEnvironmentTree() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
JTree tree = new JTree(root);
DefaultMutableTreeNode one = new DefaultMutableTreeNode("One");
root.add(one);
one.add(new DefaultMutableTreeNode("under one.1"));
one.add(new DefaultMutableTreeNode("under one.2"));
root.add(new DefaultMutableTreeNode("two"));
root.add(new DefaultMutableTreeNode("three"));
return tree;
}
public static void main(String[] args) {
new TestFrame();
}
}