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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T21:26:20+00:00 2026-05-28T21:26:20+00:00

I can’t figure out a way to resize some components in a swing GUI.

  • 0

I can’t figure out a way to resize some components in a swing GUI. Some custom labels are being added to a FlowLayout which doesn’t behave how they should when resizing the dialog.
The panel is being built using the jgoodies forms framework.

If using this, where the FlowLayout is being added to xy(3, y)

FormLayout layout = new FormLayout("r:d, 5px, f:d:g", // columns
            "p, p, 5px, p, 5px, p") // rows

The FlowLayout expands and a scroll bar shows up
tags_scroll

If using

FormLayout layout = new FormLayout("r:d, 5px, f:10:g", // columns
            "p, p, 5px, p, 5px, p") // rows

The FlowLayout uses the available space and the items on the second line disappear
tags_vanish

I’d like to expand the height of each row containing a FlowLayout to the current height of the component. Unfortunately the preferred size always corresponds to the hight for a single row.
Would another layout be more appropriate? The bold text on the left should be aligned right, followed by the FlowLayout.

Sources

[edit] After having tried to figure out how to do this for weeks, the actual question can be resumed to this:
A set of labels is being added to a JPanel. This JPanel should use all available space horizontally (dialogue size minus width of tag name label) and expand as needed vertically. If the height of the JPanel gets bigger then the dialogue, a vertical scroll bar should show up (the horizontal scroll bar is never visible).
The dialogue can show multiple JPanels which will be shown one after the other (vertically).

Here’s an attempt using GridBagLayout and WrapLayout:

public class GridBagLayoutTagPanel extends JPanel {
private static final long serialVersionUID = -441746014057882848L;
private final int NB_TAGS = 5;

public GridBagLayoutTagPanel() {
    setLayout(new GridLayout());

    JPanel pTags = new JPanel(new GridBagLayout());
    pTags.setBackground(Color.ORANGE);
    GridBagConstraints c = new GridBagConstraints();
    c.ipadx = 5;
    c.ipady = 5;

    int rowIndex = 0;
    for (int i = 0; i < NB_TAGS; i++) {
        //add tag name
        JLabel lTagName = new JLabel(String.format("Tag %s:", i));
        lTagName.setFont(lTagName.getFont().deriveFont(Font.BOLD));
        c.fill = GridBagConstraints.NONE;
        c.gridx = 0;
        c.gridy = rowIndex++;
        pTags.add(lTagName, c);

        //add tag values
        JPanel pTag = new JPanel(new BorderLayout());
        pTag.add(new JLabel("+"), BorderLayout.LINE_START); //label used to add new tags
        pTag.add(getWrapPanel(), BorderLayout.CENTER); //the list of tag values
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        pTags.add(pTag, c);
    }

    //JScrollPane sp = new JScrollPane(pTags);
    //sp.setBorder(BorderFactory.createEmptyBorder());

    add(pTags);
    }

private static JPanel getWrapPanel() {
   JPanel p = new JPanel(new WrapLayout(FlowLayout.LEFT, 5, 0));

   for (int i = 0; i < 50; i++) {
           p.add(new JLabel("t" + i));
   }
   return p;
}

public static void main(String[] args) {
    JFrame f = new JFrame();
    f.getContentPane().add(new GridBagLayoutTagPanel());
    f.setSize(new Dimension(500, 300));
    f.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-28T21:26:21+00:00Added an answer on May 28, 2026 at 9:26 pm

    With Rob’s help and using his WrapPanel and ScrollablePanel it now works exactly as it should

    public class GridBagLayoutTagPanel extends JPanel {
      private final int NB_TAGS = 5;
      private final int NB_TAGVALUES = 50;
    
      public GridBagLayoutTagPanel() {
        setLayout(new GridLayout());
    
        ScrollablePanel pTags = new ScrollablePanel(new GridBagLayout());
        pTags.setScrollableWidth(ScrollablePanel.ScrollableSizeHint.FIT);
        pTags.setBackground(Color.ORANGE);
        GridBagConstraints c = new GridBagConstraints();
        c.ipadx = 5;
        c.ipady = 5;
    
        int rowIndex = 0;
        for (int i = 0; i < NB_TAGS; i++) {
          // add tag name
          JLabel lTagName = new JLabel(String.format("Tag %s:", i));
          lTagName.setFont(lTagName.getFont().deriveFont(Font.BOLD));
          c.fill = GridBagConstraints.NONE;
          c.gridx = 0;
          c.gridy = rowIndex++;
          c.weightx = 0; // keep minimum size of the cell containing the label when resizing
          c.weighty = 0;
          pTags.add(lTagName, c);
    
          // add tag values
          JPanel pTag = new JPanel(new BorderLayout());
          pTag.add(new JLabel("+"), BorderLayout.LINE_START); // label used to add new tags
          pTag.add(getWrapPanel(), BorderLayout.CENTER); // the list of tag values
          c.fill = GridBagConstraints.HORIZONTAL;
          c.gridx = 1;
          c.weightx = 1; // fill horizontally
          c.weighty = 1;
          pTags.add(pTag, c);
       }
    
       JScrollPane sp = new JScrollPane(pTags);
       sp.setBorder(BorderFactory.createEmptyBorder());
    
       add(sp);
      }
    
      private JPanel getWrapPanel() {
        JPanel p = new JPanel(new WrapLayout(FlowLayout.LEFT, 5, 0));
    
        for (int i = 0; i < NB_TAGVALUES; i++) {
          p.add(new JLabel("t" + i));
        }
    
        p.setSize(400, 1);
        return p;
      }
    
      public static void main(String[] args) {
        JFrame f = new JFrame();
        f.getContentPane().add(new GridBagLayoutTagPanel());
        f.setSize(new Dimension(500, 300));
        f.setVisible(true);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone help me figure out an easy way to limit the UITextField range,
Can't figure out how to do this in a pretty way : I have
Can't work out a way to make an array of buttons in android. This
Can please someone put me out of my misery and tell me why this
can any one please tell me how to make the some items in multichoice
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Can anybody shed some light on getDay() in Javascript please. Here datepicker is textbox
Can anybody tell what is the best(easy) way to sort the following string 'index'
Can I set a maximum fps? Is there any way to limit fps in
Can we add custom language for RecognizerIntent? I have search many SO Question like

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.