Geek-Press Geeky Blog about software development

19Jul/120

Resizing all the components on a java.awt.Frame in Java

When we are used to use a programming language, sometimes we get our tasks a little bit automated.

I come from a background in C# and I am now doing some tasks in Java and I found some concept differences very quickly!

Task: Change the size of all the components inside a dialog based on a java.awt.Frame

After some failures figuring out how to reach my goal here is the best result I got.

protected void changeContent(Container parent) 
{
    for (Component comp : parent.getComponents()) 
    {
        changeFont(comp);
        comp.setMaximumSize( new java.awt.Dimension(comp.getWidth()*2, comp.getHeight()*2) );
        comp.setPreferredSize( new java.awt.Dimension(comp.getWidth()*2, comp.getHeight()*2) );
        comp.setSize( comp.getWidth()*2, comp.getHeight()*2);
        if (comp instanceof Container) 
        {
            changeContent((Container)comp);
        }
    }
}
protected void changeFont(Component comp) 
{
    AffineTransform at = AffineTransform.getScaleInstance(2, 2);
    if (comp.getFont() != null)
    {
        Font font = comp.getFont().deriveFont(at);
        comp.setFont(font);
    }
}

 

In my case, this little methods solved my problems and made my form grow like a charm! I hope this is usefull for someone else...

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

No trackbacks yet.