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

  • Home
  • SEARCH
  • 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 6940583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:46:18+00:00 2026-05-27T12:46:18+00:00

I need to determine the width of the text in a TextBox in Google

  • 0

I need to determine the width of the text in a TextBox in Google Web Toolkit.
I have a proportional font, so I can’t just use something like getText().length() * characterWidth.

What I’m trying to do is resize the text so it always fits into the TextBox. That’s why I need width of the text to calculate needed font size.

Only solution I can think of is to make a map of widths of characters of the font I use and calculate the width of the text as a sum of widths of its individual characters.

  • 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-27T12:46:18+00:00Added an answer on May 27, 2026 at 12:46 pm

    I solved the problem as Corvus did, by introducing an additional Label element and hiding it with CSS. I copy the text from the TextBox to the Label and retrieve the width by Label.getOffsetWidth().

    However, I needed additional code to

    1. handle repeated spaces correctly,
    2. handle style changes on the text box, and
    3. avoid text jumping when the user was typing.

    I found that I could match styles by calling

    label.setStyleName(textBox.getStyleName() + " prewrap");
    

    The prewrap style referenced in the above statement must be defined somewhere as

    .prewrap { white-space: pre-wrap; }
    

    This extra style is required because the built-in style for the textbox contains a pre-wrap directive which does not appear in the style list returned by textbox.getStyleName().

    I also added an extra two spaces at the end of the label’s text string when calculating the width so that the text won’t jump back and forth so much when the user is typing.

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

Sidebar

Related Questions

In a Win32 GUI application I need to determine the width of area occupied
I need to determine in 80% if a file is binary or text, is
I need to determine the clients .NET framework version in my web application. I'm
We need to determine a quick way for our web application deployed in a
If I have 2 textboxes, without using id's, how do I determine which textbox
I want to find correct approach for calculating text width for specified font in
I have a masked textbox with the need to have a min/max length set
How do you determine the width of the text in a WPF TreeViewItem at
My winform application is launched by another application (the parent), I need determine the
I need to determine file type (i.e., MimeType) of stored data in the SQL

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.