当控件中的文本太大而无法显示时,文本控件的 SWT/JFace 工具提示

发布于 2025-01-01 14:18:20 字数 178 浏览 3 评论 0原文

我知道这可以相当容易地实现,但我必须为此使用标准功能。 我需要在文本字段上显示工具提示,但仅当文本字段中的文本太长而无法在字段中显示时。表和树在调整列大小时具有此功能,但我没有找到文本字段的任何类似功能。

我也没能在 Eclipse 中找到这个功能,所以我猜它不是一个标准功能。 请证明我错了:)。

提前致谢。

I know this can be implemented fairly easily but I must use a standard functionality for this.
I need a tooltip to be shown on a text field but only when the text in the text field is to long to be displayed in the field. Tables and Tree's have this functionality when resizeing columns but I didn't find anything similar for text fields.

I didn't manage to find this functionality in Eclipse either so I'm guessing it's not a standard functionality.
Please prove me wrong:).

Thanks in advance.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

站稳脚跟 2025-01-08 14:18:20

“标准功能”是什么意思?将 modifyListener 添加到 Text 实例是(imo)标准公平的。

这是我的方法

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;


public class TextLabel {
    public TextLabel() {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new GridLayout());
        shell.setSize(200, 150);
        shell.setText("Long Text content label");

        Text txtLong = new Text(shell, SWT.SINGLE | SWT.BORDER);
        txtLong.addModifyListener(new ModifyListener() {

            @Override
            public void modifyText(ModifyEvent e) {
                Text txtSource = (Text) e.getSource();
                Point size = (new GC(txtSource)).stringExtent(txtSource.getText());
                if(size.x > txtSource.getBounds().width - txtSource.getBorderWidth()) txtSource.setToolTipText(txtSource.getText());
                else txtSource.setToolTipText(null);
            }
        });

        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }

    public static void main(String args[]) {
        new TextLabel();
    }
}

What does it mean "Standard functionality"..? Adding modifyListener to Text instance is (imo) standard fair enough.

Here's my approach

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;


public class TextLabel {
    public TextLabel() {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new GridLayout());
        shell.setSize(200, 150);
        shell.setText("Long Text content label");

        Text txtLong = new Text(shell, SWT.SINGLE | SWT.BORDER);
        txtLong.addModifyListener(new ModifyListener() {

            @Override
            public void modifyText(ModifyEvent e) {
                Text txtSource = (Text) e.getSource();
                Point size = (new GC(txtSource)).stringExtent(txtSource.getText());
                if(size.x > txtSource.getBounds().width - txtSource.getBorderWidth()) txtSource.setToolTipText(txtSource.getText());
                else txtSource.setToolTipText(null);
            }
        });

        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }

    public static void main(String args[]) {
        new TextLabel();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文