Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Width of String on cell in listView

Status
Not open for further replies.

klein

Programmer
Nov 7, 2001
2
IL
hii all,
I want to add toolTip on cell in list view when text in cell is hidden.
first of all, if list view providing function to know that(if text is hidden)?
if no if there is other function that providing information about width of String (in pixels)?
thanks head





 
Hi Klein. Im assuming you mean that some of the cells text in the List is hidden, and not the complete cell, otherwise ToolTip text won't show anyway.

To determine whether the text is partially hidden, i.e., partially off the screen, you'll need the width of the list, the starting location of the text, and the width of the text. Im going to let you worry about getting the first 2.
To get the pixel length of the text in the cell, you'll need to use a
Code:
FontMetrics
object. Creating these can be a pain, but they're the only accurate way to measure the pixel length of Strings. I usually wait till I get into the paint method before creating them, because it's easier to get them from a graphics object. Anyone who knows a better way to make one, let me know. This is what I usually do.
Code:
    FontMetrics fontMetrics;

    public void paint (Graphics graphics) {
      if (fontMetrics == null) {
        fontMetrics = grpahics.getFontMetrics ();
      }
    }
Once you actually have the
Code:
FontMetrics
object, you can get the length of you text in your cells.
There are a lot different ways to pass in your text, all require a Graphics object, though. I usually use
Code:
 getStringBounds (String, Graphics)
which retursn a
Code:
Rectangle2D
object, from which you can get the width of the text.
Finally, take the starting location of the text, add to it the width of the text, and compare that against the width of the cell. If
Code:
start + textWidth > cellWidth
, then the text is partially hidden.

However, if all you want is to have the ToolTip display the full text, I would set the ToolTip for all cells to be its text, then you won't have to worry about doing all this checking, though it doesn't hurt knowing how to do it. [smile]

Hope this helps,
MarsChelios
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top