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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:08:28+00:00 2026-05-31T05:08:28+00:00

Consider the following figure: I need to develop a swing GUI the looks like

  • 0

Consider the following figure:

Required Design

I need to develop a swing GUI the looks like this. I simply named them jLabel’s but there a few images and jLabels in it. The default awt background visible is a JPanel and each red background visible is a serperate JPanel. Now I need them to get stacked as shown above. I tried a number of LayoutManagers and still it doesn’t work.

The important point here is that the number of red colored divs are not constant. If there is only one red colored div then it must be displayed at the top, not at the center. As far as i know GridBagLayout should work, but it centers the single red colored jpanel available. All the layout managers are centering them but not stacking them from top to bottom.

  • 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-31T05:08:29+00:00Added an answer on May 31, 2026 at 5:08 am

    Even with anchor set to NORTH then the panels will still be centered. You could work around it by adding a dummy panel to fill the remaining space. Personally I’d stay well away from GridBagLayout though.

    JFrame frame = new JFrame();
    JPanel content = new JPanel();
    content.setBorder(BorderFactory.createLineBorder(Color.red));
    frame.setContentPane(content);
    frame.getContentPane().setLayout(new GridBagLayout());
    frame.setSize(400, 300);
    
    for (int i = 0; i < 3; i++) {
        JPanel panel = new JPanel();
        panel.add(new JLabel("label1"));
        panel.add(new JLabel("label2"));
        panel.add(new JLabel("label3"));
        panel.setBorder(BorderFactory.createLineBorder(Color.red));
        GridBagConstraints con = new GridBagConstraints();
        con.gridy = i;
        con.gridx = 0;
        con.anchor = GridBagConstraints.NORTHWEST;
        con.ipady = 10;
        frame.getContentPane().add(panel, con);
    }
    
    // dummy panel to use up the space (force others to top)
    frame.getContentPane().add(
            new JPanel(),
            new GridBagConstraints(0, 3, 1, 1, 1, 1,
                    GridBagConstraints.NORTHWEST,
                    GridBagConstraints.VERTICAL, new Insets(0, 0, 0, 0), 0,
                    0));
    
    frame.setVisible(true);
    

    GroupLayout example (my favourite layout manager).

    JFrame frame = new JFrame();
    JPanel content = new JPanel();
    frame.setContentPane(content);
    frame.getContentPane().setLayout(
            new BoxLayout(content, BoxLayout.Y_AXIS));
    frame.setSize(400, 300);
    GroupLayout gLayout = new GroupLayout(content);
    content.setLayout(gLayout);
    ParallelGroup hGroup = gLayout.createParallelGroup();
    gLayout.setHorizontalGroup(hGroup);
    SequentialGroup vGroup = gLayout.createSequentialGroup();
    gLayout.setVerticalGroup(vGroup);
    for (int i = 0; i < 3; i++) {
        JPanel panel = new JPanel();
        panel.add(new JLabel("label1"));
        panel.add(new JLabel("label2"));
        panel.add(new JLabel("label3"));
        panel.setBorder(BorderFactory.createLineBorder(Color.red));
        hGroup.addComponent(panel);
        vGroup.addComponent(panel, GroupLayout.PREFERRED_SIZE,
                GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE);
        vGroup.addGap(10);
    }
    
    frame.setVisible(true);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can't figure this out, consider the following code: <ListBox Width=200 ItemsSource={Binding CurrentArticle.ArticleCategories} DisplayMemberPath={Binding
consider the following pgSQL statement: SELECT DISTINCT some_field FROM some_table WHERE some_field LIKE 'text%'
Consider the following Javascript function (1): function setData(domElement) { domElement.myDataProperty = { 'suppose': 'this',
Consider following scenario: I have RESTful URL /articles that returns list of articles user
Consider following program: static void Main (string[] args) { int i; uint ui; i
Consider following SWT code example: http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet151.java?view=co How can I separate the inline defined class?
Consider following script: SET LANGUAGE 'German' GO SET DATEFIRST 1 GO DECLARE @FullDate DATETIME
Consider following: $(#myform).attr({ action: @Url.Action(MVC.Thing.Delete().AddRouteValue(id, myJsModel.Id )) }); I'm trying to set the action
Consider following code: enum size = 16; double[size] arr1 = [...]; double[size] arr2 =
Please consider following code: 1. uint16 a = 0x0001; if(a < 0x0002) { //

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.