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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:00:44+00:00 2026-05-27T03:00:44+00:00

I am having a simple problem I guess, that u should know how to

  • 0

I am having a simple problem I guess, that u should know how to solve..
I am trying to do this:
I have a JFrame.
Then I create a JPanel and set his layout to GridLayout(5,5)
Finnaly I add this JPanel to the Container of my JFrame.

When I try to get the width of my panel it shouldnt give me 0 right?
I do this: System.out.println(mypanel.getSize().getWidth()); and it says that is zero :c
why ??
I wanna know the size so I can divide per 5 and know how much to paint for each label, this will be a grid of labels..
Thanks alot in advance…

    public janela(){

    window = new JFrame("teste");
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setSize(300,300);
    contentor = window.getContentPane();
    interior = new JPanel();
    interior.setLayout(new GridLayout(5,5));
    contentor.add(interior);

    System.out.println(interior.getSize().getWidth());   //it says 0 ?!?! why??
     }

thanks alot in advance guys!!!!

  • 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-27T03:00:44+00:00Added an answer on May 27, 2026 at 3:00 am

    Panels are sized based on their layout. specifically when they are shown and doLayout() is called for you. doLayout() figures out what the appropriate size for your panel is based on the contents of the panel, and the layout manager in force. Since your panel has nothing in it, the appropriate size is 0, 0.

    If you will never have anything in the panel, and want to circumvent this (0, 0) size, you can override getPreferredSize() of the panel to return a size that you want the panel to respect for layout.

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

Sidebar

Related Questions

I have a really simple jQuery problem (I guess). But im totally new to
i am having a very simple problem. I have created an activity in which
I am having trouble with a really simple problem. I'm trying to remove line
This seems like a simple problem, but somehow I'm having issues with this code.
I'm having a problem getting a simple ASP.NET webpage to display. The page contains
I'm having a problem with a pretty simple setup in NHibernate. (I'm using Fluent
I'm having a problem with a simple form where i'm attempting to fire an
I'm having a problem with java sessions. I'm developing a simple web app, where
Ok, so I'm having a problem with a simple textarea. I'm using a kind
This is probably got a simple answer to it, but I am having problems

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.