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

  • Home
  • SEARCH
  • 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 6234191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:26:23+00:00 2026-05-24T10:26:23+00:00

Using layouts for me results in so much frustration that I end up using

  • 0

Using layouts for me results in so much frustration that I end up using an absolute layout and then scaling the components manually.

Let’s say I have a TextArea, and I want it to take 2/3rds of the screen.
What I would really like is having a GridLayout with 3 rows, and forcing the TextArea to take TWO of those rows at once.

So for example:

  • Row 1 (Occupied by first textArea)
  • Row 2 (Occupied by the same text area)
  • Row 3 (Occupied by buttons)

Here’s a picture using Gridbag layout (modified from the java tutorial). I would like the area in green to be taken up by a single component.

GridBagLayout

This is just a way of expressing myself, I am positively sure that there are other ways (or layouts) for doing this. However note that I would also like to do this with other specific scales, such as 9/10.

That would save so much time. Thanks!

  • 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-24T10:26:25+00:00Added an answer on May 24, 2026 at 10:26 am

    In general you should not be playing with absolute percentages, expecially on individual compoents. You should let a component display at its preferred size. So your example really doesn’t make any sense. When you have a form with a text area and buttons. Then changes in size should affect the text area and not the buttons. Then you add a scrollpane to the text area so the scroll bar will appear or disappear as required.

    However if for some reason you do need to use absolute percentages at a panel level you can use code like the following:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class GridBagSSCCE extends JPanel
    {
        JPanel red;
        JPanel green;
    
        public GridBagSSCCE()
        {
            setLayout( new GridBagLayout() );
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.fill = GridBagConstraints.VERTICAL;
    
            red = new JPanel();
            red.setBackground( Color.RED );
            red.setPreferredSize( new Dimension(300, 10) );
            gbc.weighty = 0.1;
            add(red, gbc);
    
            green = new JPanel();
            green.setBackground( Color.GREEN );
            green.setPreferredSize( new Dimension(300, 90) );
            gbc.gridy = 1;
            gbc.weighty = 0.9;
            add(green, gbc);
    
            JPanel buttons = new JPanel();
            buttons.add( new JButton("Ok") );
            buttons.add( new JButton("Cancel") );
            buttons.setAlignmentX(JPanel.RIGHT_ALIGNMENT);
            gbc.gridy = 2;
            gbc.weighty = 0.0;
            gbc.anchor = GridBagConstraints.LINE_END;
            add(buttons, gbc);
    
            addComponentListener( new ComponentAdapter()
            {
                public void componentResized(ComponentEvent e)
                {
                    System.out.println(red.getSize() + " : " + green.getSize());
                }
            });
        }
    
        private static void createAndShowUI()
        {
            JFrame frame = new JFrame("GridBagSSCCE");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add( new GridBagSSCCE() );
            frame.pack();
            frame.setLocationRelativeTo( null );
            frame.setVisible( true );
        }
    
        public static void main(String[] args)
        {
            EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                    createAndShowUI();
                }
            });
        }
    }
    

    The key in the above code is to start with a preferred size at your desired (10/90) ratio and to also set the weighty of the components in the same ratio.

    If you want you can also check out the Relative Layout which was written specifically for this purpose.

    Edit:

    A GridBagLayout does not display the components in percentages. Just because you have two rows does not mean each row is 50%. The height of each row is dependent on the height of each component added in each row.

    First you decide the preferred amount of data to be displayed in the text area. For two rows your would use something like:

    JTextArea textArea = new JTextArea(2, 40);
    JScrollPane scrollPane = new JScrollPane( textArea );
    

    Then you create a panel with buttons.

    It may turn out that a text area with 2 rows is approximately the same size a panel with buttons because the panel adds a vertical gap above and below the component and the button has a relatively large border, but that is just a coincidence. But you should not force the text area to be more than half. Just let the layout manager and the pack() method do their jobs.

    Now you need to decide what happens when the frame increases in size as this will determine the layout manager of the parent container. In this case I would probably use a BorderLayout so I would then use code like:

    frame.add(scrollPane, BorderLayout.CENTER);
    frame.add(buttonPanel, BorderLayout.SOUTH);
    frame.pack();
    

    You don’t care if the text area is half the frame or not. The pack method determines the relative sizes of each component based on their preferred sizes. If the frame increases the text area gets more space.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a doubt in layouts.I have designed one layout using group layout using
I have panel that is using group layout to organize some label. I want
I'm developing a web application that has a certain layout. I'm mainly using CSS
I've got a site using Zend Layouts and on certain pages I'd like to
I am using some nested layouts in Ruby on Rails, and in one of
Can we make cross browser css layouts with CSS positioning, without using float? What
I'm using the layout support (sitemesh) in Grails which works fine. I'd like to
I'm using this layout to create a 2 column fluid web page. What I
I have a 3 column layout using table-less design. <div id=main> <div id=left></div> <div
I'm developing a 3-column website using a layout like this: <div id='left' style='left: 0;

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.