1

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:

three bugs in one picture

  1. 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).

  2. 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.

  3. 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...

Kevin Reid
  • 37,492
  • 13
  • 80
  • 108
bobndrew
  • 395
  • 10
  • 32
  • cool! wasn't aware of those problems (there are others, as it cant handle rtol) - thanks for tracking them down :-) Would you consider to contribute them? – kleopatra Sep 02 '11 at 08:29
  • filed an issue to not forget: http://java.net/jira/browse/SWINGLABS_DEMOS-1 – kleopatra Sep 02 '11 at 09:48
  • seeing that you are doing more tracking: keep in mind that icons must be accounted for. With your first fix (comment the myTextR.x) the highlight is at the wrong position in left-aligned labels with icon. Didn't try your most recent sugestion, though – kleopatra Sep 02 '11 at 12:52
  • Thanks for your comments! What should I contribute and how ? The Bug reports or my _attempts_ to fix them? Originally i wanted to post this question to the java.net swinglabs forum... but had no luck the last 3 days – bobndrew Sep 02 '11 at 13:33
  • java.net was down due a failing DNS, is fixed but might take a while for rounding the world. Would love to see your fixed classes as attachment to the issue I created, than I'll commit it in your name – kleopatra Sep 02 '11 at 14:56
  • Attached the fixed classes plus a demo to the issue you filed on java.net. Fixed are now: Cell alignments left, right, center; JLabel with icon; Right to left component orientation; Highlight matches in viewed text AND in the ellipsis; Stable moving of the Highlighter while dragging the Column-width; Rows with different rowHeight. The 'SwingUtilities.layoutCompoundLabel()' Bug remains a mystery :-( – bobndrew Sep 06 '11 at 14:36
  • great - will commit later this week, thanks! – kleopatra Sep 07 '11 at 08:05
  • finally did it, thanks! BTW, you can answer and accept your own question, then I can vote it the answer up :-) – kleopatra Sep 26 '11 at 10:59

0 Answers0