I want to resize child components related to its parent(Panel).
I am using the following method:
@Override
public Dimension getPreferredSize() {
Dimension d = getParent().getSize();
int w = d.width * wid / 100;
int h = d.height * he / 100;
//System.out.println("x"+w+"h"+h);
return new Dimension(w,h);
}
But it doesn’t solve my problem. Can anybody tell if there is another way to resize component?
You can just call the
setPreferredSize(Dimension d)on the child components. If you pack the JFrame, the components will try to become that size.EDIT: As the comment said, it is best to avoid
setPreferredSize(Dimension d), but I have had uses for the method. For example: A JPanel with a BorderLayout, in which you have 3 JPanels (custom classes). The WEST and SOUTH Panel (the other is CENTER) had to have a height of 0.1 * the width or the height respectively if the JPanel. I usedwhich worked well. The Borderlayout took that value, and made the Panel a bit wider (I had to do this because there were no other JComponents in any of the inner JPanels).