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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:14:32+00:00 2026-06-13T07:14:32+00:00

In a Java project I’m working on, I am using a GridBagLayout to lay

  • 0

In a Java project I’m working on, I am using a GridBagLayout to lay out my UI. Every component in the UI has associated with it an x and y coordinate, which the GridBagConstraints uses to place the component in the UI.

I have a method addAt(component, x, y, constraints) that will place a given component at the coordinates (x,y) in the UI.

Rather than doing the following to set up my UI:

addAt(component1, 3, 1, constraints);
addAt(component2, 3, 2, constraints);
addAt(component3, 3, 3, constraints);
addAt(component4, 3, 4, constraints);

I would like to store the coordinates of each component in the object itself, which would give me:

addAt(component1, constraints);
addAt(component2, constraints);
addAt(component3, constraints);
addAt(component4, constraints);

My current approach to doing this is to override the base classes for all JComponents I’m using, and implement an interface with methods getXCoord() and getYCoord().

I have seen that JComponents have the methods getX() and getY(), but the purpose of those methods is different than what I’m going for.

So basically, my question is, given the above information, is there a cleaner way to implement this functionality than creating overridden versions of every individual JComponent? I apologize if I am missing something obvious 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-13T07:14:32+00:00Added an answer on June 13, 2026 at 7:14 am

    You could use a mediator or the get/putClientProperty methods to store the associations between components and coordinates and query it to retrieve the coordinates on the addAt method.

    Your coordinates can be like this:

    class Coordinates {
      private final int x;
      private final int y;
      public Coordinates(int x, int y) {
        this.x = x;
        this.y = y;
      }
      public int getX() { return x; }
      public int getY() { return y; }
    }
    

    And you use it in your addAt:

    private void addAt(JComponent component, GridBagConstraints constraints) {
      Coordinates coordinates = (Coordinates)component.getClientProperty("coords");
      int x = coordinates.getX();
      int y = coordinates.getY();
    
      // place it in the grid...
    }
    

    You create the association, for example, like this:

    public void setUpComponentsCoordinates() {
      component1.putClientProperty("coords", new Coordinates(3, 1));
      component2.putClientProperty("coords", new Coordinates(3, 2));
      component3.putClientProperty("coords", new Coordinates(3, 3));
      component4.putClientProperty("coords", new Coordinates(3, 4));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

in my Java project I am using an H2 in-memory database, for which I
The Java project I am working on is a list of testcases written in
For my Java project which uses Maven I have added Twitters Bootstrap lately to
I have a maven Java project (Project A) which depends on a second project
I have a java-project that I wish to do some analysis on using Sonar.
I have a Java project that is built using Maven , thus my build
I am making a java project and i am using JTable . The thing
(this is in a Java project) I'm using a search engine in parallel with
I have a Java project where I am pulling tweets from twitter using the
I've got a java project I'm working on in Eclipse, and I worked on

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.