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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:22:14+00:00 2026-05-15T21:22:14+00:00

I have a panel with GridLayout(1, 3) but i want centralize the content of

  • 0

I have a panel with GridLayout(1, 3) but i want centralize the content of cells in this layout(without addition Panels):

public MainMenu() {
        setLayout(new GridLayout(1, 3));

        setBorder(BorderFactory.createLineBorder(Color.BLUE));
        setOpaque(false);


        ImageComponent img = new ImageComponent("resources/music.png", true, this);
        add(img);
        ImageComponent img1 = new ImageComponent("resources/video.png", true, this);
        add(img1);
        ImageComponent img2 = new ImageComponent("resources/audio", true, this);
        add(img2);


    }

Because if i just add this 3 ImageComponents to MainMenu they appears upwards.

  • 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-15T21:22:14+00:00Added an answer on May 15, 2026 at 9:22 pm

    GridLayout will size all components to fill the available space. So, if the dimensions of your 3 ImageComponents are 50×50, 50×50, and 75×75, they will all be sized to 75×75. From there it is up to ImageComponent how it paints itself.

    Most likely ImageComponent implements paintComponent something like this:

    protected void paintComponent(Graphics g) {
        g.drawImage(image, 0, 0, this);
    }
    

    That will paint the image in the upper-left corner, not centered.

    This will paint a centered image:

    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        int iw = image.getWidth(this);
        int ih = image.getHeight(this);
    
        if (iw != -1 && ih != -1)
        {
            int w = getWidth();
            int h = getHeight();
            g.drawImage(image, (w -iw)/2, (h-ih)/2, this);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have panel that is using group layout to organize some label. I want
I have Panel with a BottomToolbar . I want to add and remove dynamically
i have a panel (A), which contains 3 other panels (AA, AB, AC). Each
I have update panel that content check box, textbox, 3 DropDownList with CascadingDropDown extender.
i have a panel in another panel and i want to access an member
I have a panel (with GridLayout( 0,1 )) embedded on JFrame. As per my
I have a GridLayout 2 rows by 5 columns, and I want to make
I realsie that similar questions have been answered here about this problem but im
So i have this code that creates a Calculator in Java. But it is
I'm trying to display a frame using GridLayout, but one of my panels isn't

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.