将 JXTable 与 MatchingTextHighlighter 一起使用时突出显示错误

发布于 2024-12-02 09:33:52 字数 3438 浏览 3 评论 0 原文

基本问题:

在更改 TableColumn 的宽度时,方法 SwingUtilities.layoutCompoundLabel(..) 将参数 Rectangle textR 设置为旧值(或“之前的值”)。
如何获得当前(真实)矩形值?


一些背景信息和错误修复:

我使用 MatchingTextHighlighter.java 来自 SwingLabs-Demos(示例是 SearchDemo.java)

仅标记 JXTable 单元格中找到的字符是一个非常好的开始。 则荧光笔的位置会出现一些问题

table.getColumnExt( 1 ).setCellRenderer( new DefaultTableRenderer( null, SwingConstants.RIGHT ) );

但是,如果我将单元格内容的对齐方式LEFT更改为:或

table.getColumnExt( 1 ).setCellRenderer( new DefaultTableRenderer( null, SwingConstants.CENTER ) );

出现三个错误, 如果字符突出显示:

一张图片中的三个错误

  1. 情况:JLabel 的文本完全可见。
    问题:列越宽(使用列标题调整大小),荧光笔就会向右漂移得越多(远离匹配的字符)。

  2. 情况:JLabel 的文本部分可见(用省略号绘制...),但突出显示的字符串完全可见。
    问题:调整列宽大小时,荧光笔位置从一个像素到一个字符都是错误的。

  3. 情况:JLabel 的文本和突出显示的字符串部分可见(突出显示应位于省略号上)
    问题:在调整列宽大小时,省略号上的荧光笔宽度错误(从无像素到正确宽度)。
    这是在左对齐列中也可见的唯一错误(荧光笔始终具有正确的宽度,但有时会跳到右侧)。

第一个错误可以通过在 2 行中注释掉 textR.x 来修复(从 MatchingTextHighlighter.java 中的第 327 行开始):

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;
}

出现了两个较小的问题:
其一是,如果匹配区域从第一个标签字符开始,则荧光笔会向左多一个像素开始。如果使用列标题调整列的宽度,则第二个是在中心对齐的列中突出显示一像素跳跃。
两者(加上 RightToLeft-Error)都可以通过这些更改来修复(从 MatchingTextHighlighter.java 中的第 397 行开始):

    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 

第三个错误可以通过以下方式部分修复(更改 XMatchingTextHighlighter.java 中的第 48 行):

int end = /* myTextR.x + */ fm.stringWidth(text) + offset;

现在荧光笔始终从省略号的第一个像素开始,修复! :-)
但是在调整列大小时宽度不断变化,错误! :-(

在调试 MatchingTextHighlighter.java 中的第二个和(剩余的一半)第三个错误之后,我认为对实用程序方法的调用

String clippedText = SwingUtilities.layoutCompoundLabel(.....)

设置了参数在调整列大小时,文本矩形的计算宽度似乎是“一个事件”,因此,荧光笔的位置是错误的

。 内容?

感谢您阅读全部 这...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文