tags:

views:

384

answers:

1
+1  Q: 

java swing resize

hello, I am writing a library where i am passed in a Container (usually JPanel)

and have an xml specification for different controls, their locations, size and other attributes. i have to create those controls at runtime and add it to the component. What's a good way to handle resizing of the parent Container?

+2  A: 

If the XML specifies the absolute locations then there is probably no way to elegantly resize the components. You could create a custom LayoutManager that would scale them linearly but that would probably look bad for most components.

EDIT: Here is a version that might be helpful:

public class LinearScaleLayout {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Linear Scale Layout Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(100, 100, 200, 200);

        JPanel scalePanel = new JPanel(new LinearScaleLayoutManager());
        scalePanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        scalePanel.add(new JTextField(), new Rectangle2D.Double(0, 0, 1, 0.2));
        scalePanel.add(new JScrollPane(new JEditorPane()), new Rectangle2D.Double(0, 0.25, 0.45, 0.75));
        scalePanel.add(new JSlider(), new Rectangle2D.Double(0.55, 0.25, 0.45, 0.75));

        frame.getContentPane().add(scalePanel);
        frame.setVisible(true);
    }

    private static class LinearScaleLayoutManager implements LayoutManager2 {
        private final HashMap<Component, Rectangle2D> rectMap = new HashMap<Component, Rectangle2D>();

        @Override
        public void layoutContainer(Container parent) {
            synchronized (parent.getTreeLock()) {
                Insets insets = parent.getInsets();
                int clientWidth = parent.getWidth() - insets.left - insets.right;
                int clientHeight = parent.getHeight() - insets.top - insets.bottom;
                if (clientWidth > 0 && clientHeight > 0) {
                    for (Component component : parent.getComponents()) {
                        Rectangle2D rect = rectMap.get(component);
                        if (rect != null) {
                            component.setBounds(new Rectangle(insets.left + (int)(rect.getX() * clientWidth), 
                                    insets.top + (int)(rect.getY() * clientHeight), (int)(rect.getWidth() * clientWidth), 
                                    (int)(rect.getHeight() * clientHeight)));
                        }
                    }
                }
            }
        }

        @Override
        public void addLayoutComponent(Component comp, Object constraints) {
            rectMap.put(comp, (Rectangle2D)constraints);
        }

        @Override
        public void removeLayoutComponent(Component comp) {
            rectMap.remove(comp);
        }

        @Override
        public Dimension minimumLayoutSize(Container parent) {
            return new Dimension(0, 0);
        }

        @Override
        public Dimension preferredLayoutSize(Container parent) {
            return new Dimension(100, 100);
        }

        @Override
        public Dimension maximumLayoutSize(Container target) {
            return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
        }

        @Override
        public void addLayoutComponent(String name, Component comp) {
        }

        @Override
        public float getLayoutAlignmentX(Container target) {
            return 0;
        }

        @Override
        public float getLayoutAlignmentY(Container target) {
            return 0;
        }

        @Override
        public void invalidateLayout(Container target) {
        }
    }
}
Russ Hayward
yeah! the xml specifies absolute locations. I'm looking into creating custom LayoutManager . Thanks