简体   繁体   English

切断左侧JLabel的文本

[英]Cut off the text of a JLabel on the left

By default JLabel cuts off text on the right with 3 dots, if the text is too long to be displayed completely like this: 默认情况下,如果文本太长而无法完全显示, JLabel在右侧切断3个点的文本: 在此输入图像描述

(Image is from a small backup application I'm working on). (图像来自我正在处理的小型备份应用程序)。 As you can see the last JLabel above the "Cancel"-button is cut off on the right. 正如您所看到的,“取消”按钮上方的最后一个JLabel在右侧被切断。 This behavior is clearly not desirable, as the more relevant part of the text is cut off. 这种行为显然是不可取的,因为文本中更相关的部分被切断了。

I'd like the resulting label to look like in this image (right column, sry for the bad resolution): 我希望生成的标签看起来像这张图片(右栏,sry表示错误的分辨率):
在此输入图像描述

Source 资源

So far I've tried to change the alignment of the text within the label to JLabel.RIGHT , alter the components orientation to ComponentOrientation.RIGHT_TO_LEFT , set horizontalTextPosition , all to no avail: 到目前为止,我已经尝试将标签中文本的对齐方式更改为JLabel.RIGHT ,将组件方向更改为ComponentOrientation.RIGHT_TO_LEFT ,设置horizontalTextPosition ,但都无济于事:

import javax.swing.*;
import java.awt.*;

public class Backup
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(()->{
            JLabel label = new JLabel("Some test text 1 2 3 4 5 6 7 8 9 10 abcdefghjiklmnopqrstuvwxyz", JLabel.RIGHT);
            label.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
            label.setHorizontalTextPosition(JLabel.RIGHT);

            JFrame frame = new JFrame();
            frame.add(label);
            frame.pack();
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            frame.setVisible(true);
        });
    }
}

What I've tried as well, using HTML: 我也尝试过使用HTML:

<html>
    <body>
        <p style="width:300px;overflow-x:hidden;direction:rtl">
            kladsjhglakjsjdghlekfalksjdvhlkjdsnkljhsdlkvjhasdkjhfslkdjhcksdjhvflkasjvhlkajdlkajvsdhvlkjsadhaaaaaaaaaaaaa
        </p>
    </body>
</html>

While this works just in the way it's supposed to in my browser, swing doesn't seem to support a sufficient set of style-properties to support this behavior. 虽然这只是在我的浏览器中应用的方式,但swing似乎不支持足够的样式属性集来支持这种行为。

It shouldn't be too hard to code a own implementation that fulfills the requirement of doing precisely this. 编写一个满足正确执行要求的自己的实现应该不会太难。 Nevertheless I was wondering whether there was a "swing-way" of achieving this. 然而,我想知道是否有实现这一目标的“摆动方式”。

This doesn't provide the leading ellipsis (…), but at least it is simple and clean. 这不提供前导省略号(...),但至少它简单而干净。 You can put the JLabel in a JViewport and keep it scrolled to the end at all times: 您可以将JLabel放在JViewport中并始终将其滚动到最后:

JViewport viewport = new JViewport();
viewport.setView(label);
viewport.addComponentListener(new ComponentAdapter() {
    @Override
    public void componentResized(ComponentEvent event) {
        int width = viewport.getWidth();
        Dimension size = label.getPreferredSize();
        viewport.setViewPosition(new Point(size.width - width, 0));
    }
});

You can add this and see if it is what you need: 你可以添加它,看看它是否是你需要的:

label.setHorizontalAlignment(SwingConstants.LEFT);

By the documentation: 通过文件:

Sets the alignment of the label's contents along the X axis. 设置标签内容沿X轴的对齐方式。

Using the excellent answer from trashgod ( https://stackoverflow.com/a/3597688/567496 ), here is a simple implementation of a BasicLabelUI that creates a left-side ellipsis. 使用trashgod( https://stackoverflow.com/a/3597688/567496 )的优秀答案,这是一个创建左侧省略号的BasicLabelUI的简单实现。

It does use Apache's StringUtils.reverse(text) , but only for convenience. 它确实使用Apache的StringUtils.reverse(text) ,但仅为方便起见。 It could be replaced with calls to StringBuilder(text).reverse().toString() . 它可以用对StringBuilder(text).reverse().toString()调用来替换。

static class LeftEllipsisUI extends BasicLabelUI {
    @Override
    protected String layoutCL(JLabel label, FontMetrics fontMetrics, String text, Icon icon, Rectangle viewR, Rectangle iconR, Rectangle textR) {
        return StringUtils.reverse(super.layoutCL(label, fontMetrics, StringUtils.reverse(text), icon, viewR, iconR, textR));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM