设为首页 - 加入收藏 ASP站长网(Aspzz.Cn)- 科技、建站、经验、云计算、5G、大数据,站长网!
热搜: 创业者 手机 数据
当前位置: 首页 > 运营中心 > 网站设计 > 正文

如何在包含HTML的JLabel中获取省略号?

发布时间:2021-05-15 20:08 所属栏目:12 来源:网络整理
导读:当我将 HTML标记组合到JLabel文本中时,我忽略了当空间太小而无法显示完整文本时显示的省略号行为.在我的特定情况下,它是一个TableCellRenderer,它扩展了JLabel(swing的默认值或其他).现在,当列宽太小而文本无法完全显示时,它不会显示省略号. 请参见下图,例如

当我将 HTML标记组合到JLabel文本中时,我忽略了当空间太小而无法显示完整文本时显示的省略号行为.在我的特定情况下,它是一个TableCellRenderer,它扩展了JLabel(swing的默认值或其他).现在,当列宽太小而文本无法完全显示时,它不会显示省略号.

请参见下图,例如:
对于左栏,我使用HTML将文本包装在渲染器中:setText(“< html>”“< strong>”value.toString()“< / strong>”“< / html>”);.正如您所看到的,当列宽太小而无法包含文本时,它只是被剪切.但是,右列显示日期和时间并使用DefaultTableCellRenderer在无法包含完整文本时显示省略号.

所以我的问题是,我可以同时拥有两个吗?意思是,用HTML包装文本仍然得到省略号?

更新:

我发现在使用HTML时没有得到省略号的原因.我按照JComponent#paintComponent(Graphics g)中的代码一直到BasicLabelUI#layoutCL(…).请参阅从上一个中获取的以下代码段.如果它没有html属性,它只剪裁字符串(当标签文本用html包装时为true).但我不知道如何解决它:

v = (c != null) ? (View) c.getClientProperty("html") : null;
    if (v != null) {
        textR.width = Math.min(availTextWidth,(int) v.getPreferredSpan(View.X_AXIS));
        textR.height = (int) v.getPreferredSpan(View.Y_AXIS);
    } else {
        textR.width = SwingUtilities2.stringWidth(c,fm,text);
        lsb = SwingUtilities2.getLeftSideBearing(c,text);
        if (lsb < 0) {
            // If lsb is negative,add it to the width and later
            // adjust the x location. This gives more space than is
            // actually needed.
            // This is done like this for two reasons:
            // 1. If we set the width to the actual bounds all
            //    callers would have to account for negative lsb
            //    (pref size calculations ONLY look at width of
            //    textR)
            // 2. You can do a drawString at the returned location
            //    and the text won't be clipped.
            textR.width -= lsb;
        }
        if (textR.width > availTextWidth) {
            text = SwingUtilities2.clipString(c,text,availTextWidth);
            textR.width = SwingUtilities2.stringWidth(c,text);
        }
        textR.height = fm.getHeight();
    }

解决方法

我要说:不,你不能两者都有.

我想如果你想要自定义样式和省略号,你将不得不自己做,没有HTML和自定义TableCellRenderer.

如果您想尝试拥有自己的蛋糕并吃掉它,您可以通过创建自己的View对象并使用c.putClientProperty(“html”,value)设置它来实现目标,但我怀疑HTML渲染代码具有没有ellipsing的概念(文本溢出是一个HTML 5ish功能)所以你必须弄清楚如何教它做这件事.我怀疑这比编写自己的TableCellRenderer要困难得多.

(编辑:ASP站长网)

    网友评论
    推荐文章
      热点阅读