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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:30:33+00:00 2026-06-11T10:30:33+00:00

In attempting to make some Swing code more readable, I have made an InlineGridBagConstraints

  • 0

In attempting to make some Swing code more readable, I have made an InlineGridBagConstraints class which looks like this:

public class InlineGridBagConstraints extends GridBagConstraints {

public InlineGridBagConstraints gridx(int x) {
    gridx = x;
    return this;
}

public InlineGridBagConstraints gridy(int y) {
    gridy = y;
    return this;
}

public InlineGridBagConstraints gridheight(int h) {
    gridheight = h;
    return this;
}

public InlineGridBagConstraints gridwidth(int w) {
    gridwidth = w;
    return this;
}

    // .... and so on, for all fields.

}

The intention is to change this kind of code:

GridBagConstraints c = new GridBagConstraints();
c.gridx = 2;
c.gridy = 1;
c.gridwidth = 3;
myJPanel.add(myJButton, c);

c.gridx = 3;
c.gridwidth = 2;
myJPanel.add(myOtherJButton, c);

c.gridx = 1;
c.gridy = 5;
c.gridheight = 4;
myJPanel.add(yetAnotherJButton, c);

…with something much easier to understand and read, like this:

InlineGridBagConstraints c = new InlineGridBagConstraints();
myJPanel.add(myJButton, c.gridx(2).gridy(1).gridwidth(3));
myJPanel.add(myOtherJButton, c.gridx(3).gridy(1).gridwidth(2);
myJPanel.add(yetAnotherJButton, c.gridx(1).gridy(5).gridheight(4);

However, the above code isn’t working. When I attempt it, all of the components occupy the same area in the center of the JPanel and overlap one another. They are not spaced out in the GridBagLayout. If I use the uglier version with the regular GridBagConstraints, however, it works perfectly as intended.

I have tried typecasting the InlineGridBagConstraints into GridBagConstraints, thinking that perhaps that was an issue (even though it shouldn’t be), but that didn’t help at all.

I’ve run out of ideas. Does anyone know why this is occurring or what the key difference between the first (standard) and the second (inline) implementations is?

  • 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-11T10:30:34+00:00Added an answer on June 11, 2026 at 10:30 am

    I really have no idea what your GUIConstants defines, as we don’t see it, but changing the reset() method in InlineGridBagConstraints to the one below, makes your UI look as you probably expected:

      public InlineGridBagConstraints reset() {
        gridx = 0;
        gridy = 0;
        gridheight = 1;
        gridwidth = 1;
        insets = new Insets(5, 5, 5, 5);
        fill = GridBagConstraints.BOTH;
        anchor = GridBagConstraints.CENTER;
        return this;
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was attempting to make some expression templates as an answer to this question
I'm trying to make some code which finds if a div exists, and if
I'm attempting to make a messaging system in which any class derived from Messageable
I am attempting to make a small website with a directory structure like this:
I'm attempting to make a StateMachine execute some database action between states. So I
Greetings from some noob trying to learn JQuery, I am attempting to make it
I am attempting to make a program in C which presents a GUI and
I am attempting to make some data structures to solve a graph puzzle. I
I am attempting to make some cross-DB(SQL Server and PostgreSQL) compatible SQL. What I
I'm attempting to create thumbnails out of some images, each of which isn't necessarily

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.