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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:39:49+00:00 2026-05-13T17:39:49+00:00

I’m trying to render a custom Swing component where I’ve extended the JComponent class.

  • 0

I’m trying to render a custom Swing component where I’ve extended the JComponent class.

For the purpose of simplifying the component requirements, lets just summarize my component as needing to render a few strings, each with their own fonts.

I need my component to be sized exactly to the summed width and height of my rendered strings.

In order to determine this size, I use the FontMetrics to calculate the dimensions of each string. Having this information I can figure out what size my component will be and resize it appropriately.

The problem is that when I access the getGraphics() it is always null, so I can’t get the FontMetrics instance. If I wait to calculate my component size from the overriden paintComponent() method, its pretty much too late (the component already has a size, right?).

The documentation says that “This method will return null if this component is currently not displayable”. So when do I know when the component is ready to be displayed and has a Graphics object for me to resize my component?

What is the Swing invokation order for rendering the component once the frame setVisible(true) is called?

Thanks


Update: Tuesday, Feb 06, 2010 at 23:34

As mentioned bellow in the comments, the GridLayout doesn’t respect any setXxxSize() at all. For anyone interested, I’ve posted results of using the GridLayout, BoxLayout and FlowLayout using a simple frame that receives 5 fixed size components of 200 wide by 50 in height (by setting min, max and preferred).

Test Results:

The GridLayout is always resized along the width and height (as mentioned in comments)

The FlowLayout always respected the components size regardless.

As for the the BoxLayout…

The PAGE_AXIS and Y_AXIS shrank the width of the components to about half their size (104) but did not shrink the height.

The LINE_AXIS and X_AXIS shrank the height of the components to what seemed zero but did not touch the width.

  • 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-13T17:39:50+00:00Added an answer on May 13, 2026 at 5:39 pm

    First, you can measure your Strings using the TextLayout class and its associated stuff. For example

    public static void main(final String[] args) throws Exception
    {
        final FontRenderContext frc = new FontRenderContext(null, true, true);
        final Font font = new Font("serif", Font.PLAIN, 18);
        final TextLayout layout = new TextLayout("This is a test", font, frc);
        final Rectangle2D bounds = layout.getBounds();
        System.err.println((int) (bounds.getWidth() + .5));
    }
    

    Secondly, you can be informed when your component has become visible by using a ComponentListener, but that’s not necessary for measuring your strings in “advance”!

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

Sidebar

Related Questions

I am trying to render a haml file in a javascript response like so:
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.