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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:10:44+00:00 2026-06-02T00:10:44+00:00

The grid layout has two columns and is giving 50% space to each side,

  • 0

The grid layout has two columns and is giving 50% space to each side, no matter what size the components actually are. SetPrefferedSize doesn’t seem to work either – but I shouldn’t need it, this shold be done automatically, right?

enter image description here

Can I make the first row’s width to resize to fit “SomeOtherLongText3:”?

SSCCE:

package test;

import java.awt.*;
import javax.swing.*;

public class test {

    public static void main(String[] args) {
        JFrame frame = new JFrame();

        JPanel detailsPanel = new JPanel();
        detailsPanel.setLayout(new GridLayout(0, 2, 15, 0));

        // Add to left side (WEST)
        frame.add(detailsPanel, BorderLayout.WEST);

        // Add some labels:
        detailsPanel.add(getLabel("Some text:"));
        detailsPanel.add(new JLabel("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc id mauris "));


        detailsPanel.add(getLabel("Some text 2:"));
        detailsPanel.add(new JLabel("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc id mauris "));

        detailsPanel.add(getLabel("Some other text 3:"));
        detailsPanel.add(new JLabel("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc id mauris "));

        frame.setSize(new Dimension(900, 100));
        frame.setVisible(true);
        frame.pack();
    }

    public static JLabel getLabel(String text)
    {
        JLabel lbl = new JLabel(text);
        lbl.setHorizontalAlignment(SwingConstants.TRAILING);
        return lbl;
    }

}

Also, another problem is that you can just re-size the window to cut the text:
enter image description here

  • 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-06-02T00:10:46+00:00Added an answer on June 2, 2026 at 12:10 am

    Layout Strategies

    1. Put 2 single column GridLayout instances in the WEST (labels) and CENTER (values) of a BorderLayout. Add the outer panel to the WEST or the parent..
    2. Use a GroupLayout. That might be better since you can use HTML/CSS to limit the width of the values and force them to multiple lines.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Swing, using Grid Layout, each grid has same size.. is it possible to
So I'm creating columns dynamically for a Grid layout in Silverlight (V3.0, C#): LayoutRoot.ColumnDefinitions.Add(new
How to create a layout like this? It has two textblocks that are next
I have a dojo layout with two content panes, each at 50% height, one
On my Java Swing application I have two components. On the left side is
I have a JPanel inside BorderLayout.CENTER The JPanel has a Grid Layout, and I
I'm trying to have a grid that has items that have a custom layout/feel.
Has anybody gotten the support library to render a grid layout correctly in Android
I have a JPanel with a Grid Layout. In the cells of the grid
I have a registration form on my layout grid, made out of a stackPanel.

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.