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 6033877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:32:51+00:00 2026-05-23T05:32:51+00:00

I finally got the behavior I want for vertically stacking components that have a

  • 0

I finally got the behavior I want for vertically stacking components that have a preferred height that changes with time. But I needed to use MigLayout.

Is there a way to do this w/o MigLayout? (It’s for a library and I don’t want to force the dependency unless I have to)

Here’s the behavior I’m looking for (which my test program achieves):

enter image description here

In vertical order, there’s a resize button, “empty space” (well, a JLabel marked as such), a red rectangle, and a green square. The resize button has fixed height. The red square has a random size that can change at arbitrary times. The green square sets its preferred height to match its width, and I want to expand its width to fill the container. The empty space expands horizontally and vertically to fill the remaining space in the container.

What would work instead of MigLayout?

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.Random;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;

public class AutoResizeDemo extends JPanel
{   
    static private class ResizingPanel extends JPanel
    {
        final private Color color;

        private Dimension dpref = new Dimension(100,100);

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            int w = getWidth();
            int h = getHeight();
            g.setColor(this.color);
            g.fillRect(0, 0, w, h);
            g.setColor(Color.BLACK);
            g.drawRect(0, 0, w-1, h-1); 
            String s = this.dpref.width+"x"+this.dpref.height;
            FontMetrics fm = g.getFontMetrics();
            g.drawString(s, 0, fm.getHeight());
        }

        public ResizingPanel(Color color, boolean isSquare)
        {
            this.color = color;
            if (isSquare)
            {
                addComponentListener(new ComponentAdapter() {
                    @Override public void componentResized(ComponentEvent e) {
                        doResize(getWidth(), getWidth());
                    }               
                });
            }
        }

        @Override public Dimension getPreferredSize() {
            return this.dpref;
        } 

        public void doResize(int w, int h)
        {
            this.dpref = new Dimension(w, h);
            revalidate();
        }
    }

    public AutoResizeDemo()
    {
        super(new MigLayout("","[grow]",""));
        setPreferredSize(new Dimension(200, 800));

        final ResizingPanel resizingPanelRandom = new ResizingPanel(Color.RED, false);
        ResizingPanel resizingPanelSquare = new ResizingPanel(Color.GREEN, true);
        JPanel buttonPanel = new JPanel(new FlowLayout());

        final Random rand = new Random();
        addButton(buttonPanel, "resize",new Runnable() {
            @Override public void run() {
                resizingPanelRandom.doResize(
                        rand.nextInt(100)+100,
                        rand.nextInt(100)+100
                        );
            }           
        });
        add(buttonPanel, "wrap");
        JLabel spaceLabel = new JLabel("empty space");
        spaceLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        add(spaceLabel, "push, grow, wrap");
        add(resizingPanelRandom, "wrap");
        add(resizingPanelSquare,"pushx, growx, wrap");
    }

    private void addButton(JPanel panel, String title, final Runnable r) {
        JButton button = new JButton(title);
        button.addActionListener(new ActionListener() {
            @Override public void actionPerformed(ActionEvent e) {
                r.run();
            }           
        });
        panel.add(button);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame(AutoResizeDemo.class.getSimpleName());
        frame.setContentPane(new AutoResizeDemo());
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }

}
  • 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-23T05:32:52+00:00Added an answer on May 23, 2026 at 5:32 am

    You can solve this using SpringLayout by wiring all your compenents together and to the edges of their container.

    Button Panel
    left and top of the button panel to left and top of the container panel

    Green Panel
    left, right and bottom to the left, right and bottom of the container panel

    Red Panel
    left to left of container panel and bottom to top of the green panel

    Space Label
    top to south of the button panel, left and right to left and right of the container panel, bottom to top of the red panel

    Edit: I love SpringLayout, there’s nothing it can’t do.

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.FontMetrics;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ComponentAdapter;
    import java.awt.event.ComponentEvent;
    import java.util.Random;
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SpringLayout;
    
    public class AutoResizeDemo2 extends JPanel {
      static private class ResizingPanel extends JPanel {
        final private Color color;
    
        private Dimension dpref = new Dimension(100, 100);
    
        @Override
        protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          int w = getWidth();
          int h = getHeight();
          g.setColor(this.color);
          g.fillRect(0, 0, w, h);
          g.setColor(Color.BLACK);
          g.drawRect(0, 0, w - 1, h - 1);
          String s = this.dpref.width + "x" + this.dpref.height;
          FontMetrics fm = g.getFontMetrics();
          g.drawString(s, 0, fm.getHeight());
        }
    
        public ResizingPanel(Color color, boolean isSquare) {
          this.color = color;
          if (isSquare) {
            addComponentListener(new ComponentAdapter() {
              @Override
              public void componentResized(ComponentEvent e) {
                doResize(getWidth(), getWidth());
              }
            });
          }
        }
    
        @Override
        public Dimension getPreferredSize() {
          return this.dpref;
        }
    
        public void doResize(int w, int h) {
          this.dpref = new Dimension(w, h);
          revalidate();
        }
      }
    
      public AutoResizeDemo2() {
    
        SpringLayout layout = new SpringLayout();
        setLayout(layout);
    
        setPreferredSize(new Dimension(200, 800));
    
        final ResizingPanel resizingPanelRandom = new ResizingPanel(Color.RED, false);
        ResizingPanel resizingPanelSquare = new ResizingPanel(Color.GREEN, true);
        JPanel buttonPanel = new JPanel(new FlowLayout());
    
        final Random rand = new Random();
        addButton(buttonPanel, "resize", new Runnable() {
          @Override
          public void run() {
            resizingPanelRandom.doResize(rand.nextInt(100) + 100, rand.nextInt(100) + 100);
          }
        });
        add(buttonPanel);
        layout.putConstraint(SpringLayout.NORTH, buttonPanel, 5, SpringLayout.NORTH, this);
        layout.putConstraint(SpringLayout.WEST, buttonPanel, 5, SpringLayout.WEST, this);
    
        JLabel spaceLabel = new JLabel("empty space");
        spaceLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    
        add(resizingPanelSquare);
        layout.putConstraint(SpringLayout.SOUTH, resizingPanelSquare, -5, SpringLayout.SOUTH, this);
        layout.putConstraint(SpringLayout.WEST, resizingPanelSquare, 5, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.EAST, resizingPanelSquare, -5, SpringLayout.EAST, this);
    
        add(resizingPanelRandom);
        layout.putConstraint(SpringLayout.SOUTH, resizingPanelRandom, -5, SpringLayout.NORTH, resizingPanelSquare);
        layout.putConstraint(SpringLayout.WEST, resizingPanelRandom, 5, SpringLayout.WEST, this);
    
        add(spaceLabel);
        layout.putConstraint(SpringLayout.NORTH, spaceLabel, 5, SpringLayout.SOUTH, buttonPanel);
        layout.putConstraint(SpringLayout.WEST, spaceLabel, 5, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.EAST, spaceLabel, -5, SpringLayout.EAST, this);
        layout.putConstraint(SpringLayout.SOUTH, spaceLabel, -5, SpringLayout.NORTH, resizingPanelRandom);
      }
    
      private void addButton(JPanel panel, String title, final Runnable r) {
        JButton button = new JButton(title);
        button.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            r.run();
          }
        });
        panel.add(button);
      }
    
      public static void main(String[] args) {
        JFrame frame = new JFrame(AutoResizeDemo2.class.getSimpleName());
        frame.setContentPane(new AutoResizeDemo2());
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    
      }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I finally got back to fleshing out a GitCommit message mode that I want
I finally got my application to seemingly post to https. However, every time that
I finally got some understanding of how Ninject handles DI, but have faced the
I've got some autocompletes working finally, but there is one behavior I'd like to
Have finally got a responsive site working (of a fashion). What I want to
I have finally got the green light to use Memcached in our project (after
I've have finally got the datepicker to work on my MVC demo site. One
I have finally got in my mind what worried me about Dependency Injection and
Well, now that I've finally got my stupid ODBC stuff configured, I took a
I finally got some time on my hand to upgrade my main dev. machine

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.