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. I want to set the height/width of only that frame (and I do not want to try and pixelify its children). If I run this, I see a badly aligned UI.
Firstly, I want a panel (that is inside the root frame) to take up the 100% height of frame (in this case 800px minus that little space it takes for painting the frame title).
Secondly, inside the panel I have a tree and text area. I want both of them to take 100% height and let the tree take 30% and textArea take 70% width (if the tree is expanded to 10 levels then I am ok with ScrollPane).
Understand that this is easiest to achieve in HTML. Just say height=100% and width to be 30% etc and we are done. Does someone know if this can be done in Swing? (I can achieve this by setting pixel heights and layout manager but I am looking for the cleanest solution to set percentage heights and widths.)
package com.ekanathk.logger.gui;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
public class TestFrame extends JFrame {
public TestFrame() {
super("Top Frame");
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JTree env = getEnvironmentTree();
env.expandRow(0);
panel.add(new JScrollPane(env));
panel.add(new JTextArea("Some contents"));
getContentPane().add(panel);
setSize(800, 600);
SwingUtil.centerComponentOnScreen(this);
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();
}
}