The basic question:
While changing the width of a TableColumn the method SwingUtilities.layoutCompoundLabel(..)
sets the parameter Rectangle textR
to an old value (or 'the value before').
How could I get the current (real) Rectangle value?
Some background information and bugfixes:
I use the MatchingTextHighlighter.java from the SwingLabs-Demos (the example is SearchDemo.java)
It is a very nice start to mark just the found characters in a JXTable cell. But I have some issues with the position of the Highlighter if I change the alignment of the cell-content from LEFT
to:
table.getColumnExt( 1 ).setCellRenderer( new DefaultTableRenderer( null, SwingConstants.RIGHT ) );
or
table.getColumnExt( 1 ).setCellRenderer( new DefaultTableRenderer( null, SwingConstants.CENTER ) );
Three bugs occur if characters are highlighted:
Situation: The text of the JLabel is fully visible.
The problem: The wider the column gets (resized using the columnheader), the more the highlighter will drift to the right (awaaay from the matched characters).Situation: The text of the JLabel is partially visible (painted with ellipsis ...), but the highlighted string is fully visible.
The Problem: The highlighter position is wrong from one pixel to one character while resizing the column width.Situation: The text of the JLabel and the highlighted string are partially visible (the highlighter should be on the ellipsis)
The Problem: The highlighter on the ellipsis has a wrong width (from no pixel to correct width) while resizing the column width.
This is the only bug that's also visible in a left-aligned column (the highlighter has always the correct width but is jumping to the right sometimes).
The first bug can be fixed by commenting out textR.x
in 2 lines (starting at line 327 in MatchingTextHighlighter.java):
if (start == 0) {
// start highlight from the start of the field
highlightx = /* textR.x + */ xOffset;
} else {
// Calculate the width of the unhighlighted text to get the
// start of the highlighted region.
String strToStart = text.substring(0, start);
highlightx = /* textR.x + */ fm.stringWidth(strToStart) + xOffset;
}
Two smaller problems emerge:
One is that the highlighter starts one pixel more left if the matched area begins at the first Label-character. The second is a highlighter-one-pixel-jumping in the center-aligned column, if the width of the column is resized using the columnheader.
Both (plus a RightToLeft-Error) could be fixed with these changes (starting at line 397 in MatchingTextHighlighter.java):
return textR.x;//respect the icon and start the highlight at the beginning of the text not at 0
} else if (horizAlignment == SwingConstants.RIGHT
|| (horizAlignment == SwingConstants.TRAILING && leftToRight) //fix for rtol: ! deleted
|| (horizAlignment == SwingConstants.LEADING && !leftToRight)) //fix for rtol: ! added
{
return viewR.width - textR.width;
} else if (horizAlignment == SwingConstants.CENTER) {
return Math.round((viewR.width - textR.width) / 2f) - 1; //round a float to prevent a one-pixel-jumping Highlighter
The third bug can be partially fixed by (changing line 48 in XMatchingTextHighlighter.java):
int end = /* myTextR.x + */ fm.stringWidth(text) + offset;
Now the highlighter starts always at the first pixel of the ellipsis, fix! :-)
But the widths keeps changing while resizing the column, error! :-(
After debugging the second and (the remaining half of) the third bug in MatchingTextHighlighter.java, I think the call to the utility method
String clippedText = SwingUtilities.layoutCompoundLabel(.....)
sets the parameter textR
to an old value. While resizing the column, the calculated width of the text-rectangle seems to be "one event behind". And because of this, the position of the Highlighter is wrong.
Does anyone has an idea to get this fixed?
Thanks for reading all this...