Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 631065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:55:26+00:00 2026-05-13T19:55:26+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T19:55:26+00:00Added an answer on May 13, 2026 at 7:55 pm

    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) {
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been writing a library for my project (for now I am using
I have a utility class library I am writing. A have written a factory
I have been mulling over writing a peak-fitting library for a while. I know
I have an error reporting functionality in my little C library I'm writing. I
I am writing library which wraps a lot of functions and methods from other
I am writing a library which uses a few functions from the windows user32.dll
Ok I am writing a library that will be shard between unix and windows.
I'm writing a library for mobile devices. This library notifies the users when certain
I am writing a library that will provide a collection of public types to
I am writing a library to generate PDF reports using prawn reports. One of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.