JTable 单元格 - 处理长文本

发布于 2024-11-06 01:59:55 字数 425 浏览 0 评论 0原文

我有一个 JTable,其中一列偶尔会包含大量文本。我们使用一些算法将每行的高度扩展到最高的单元格。问题是对于长文本单元格,我们会得到“胖”行。

它看起来像这样:

=============================
| Col1 | Col2 | This is some|
|      |      | very long   |
|      |      | text!       |
=============================

我考虑了几个解决方案:

  • 剪切文本并添加鼠标侦听器以“扩展”剪切的文本
  • 剪切文本并添加工具提示或对话框以显示额外的内容

有谁知道有任何库解决这个问题吗?我愿意使用其他技术......我不相信我的解决方案是最好的。

提前致谢!

I have a JTable where one column occasionally has a fair amount of text in it. There are algorithms that we're using to expand the height of each row to the tallest cell. The problem is for long text cells we get "fat" rows.

It looks something like this:

=============================
| Col1 | Col2 | This is some|
|      |      | very long   |
|      |      | text!       |
=============================

I've considered a couple solutions:

  • Clipping the text and adding a mouse listener to "expand" the clipped text
  • Clipping the text and adding a tooltip or dialog to show the extra contents

Does anyone know of any libraries that fix this? I'm open to using some other technique...I'm not convinced my solution is the best.

Thanks in advance!

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

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

发布评论

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

评论(3

鹿港巷口少年归 2024-11-13 01:59:55

我只想使用工具提示。

您可以重写 JTable 的 getToolTipText(() 方法来执行此操作。

JTable table = new JTable(...)
{
    public String getToolTipText( MouseEvent e )
    {
        int row = rowAtPoint( e.getPoint() );
        int column = columnAtPoint( e.getPoint() );

        Object value = getValueAt(row, column);
        return value == null ? null : value.toString();
    }
};

或者,如果它仅适用于某些列,您可以使用渲染器来设置工具提示文本。请参阅 指定单元格的工具提示

I would just use a tooltip.

You can override the getToolTipText(() method of JTable to do this.

JTable table = new JTable(...)
{
    public String getToolTipText( MouseEvent e )
    {
        int row = rowAtPoint( e.getPoint() );
        int column = columnAtPoint( e.getPoint() );

        Object value = getValueAt(row, column);
        return value == null ? null : value.toString();
    }
};

Or if its only for certain columns you can use the renderer to set the tooltip text. See Specifying Tool Tips for Cells.

很快妥协 2024-11-13 01:59:55

我花了一段时间才找到如何在 Netbeans 上显示工具提示,但您的回答很有帮助。
这里它是在 netbeans GUI Builder 上实现的...
右键单击您的 Jtable -> 自定义代码。
选择“自定义创建”,其中新的“javax.swing.JTable();”代码是 和 位于分号之前;添加下面的答案的代码...看起来像这样:

YourTable = new javax.swing.JTable(){
    //add tooltip to display the full cell text when not displayed
    public String getToolTipText( MouseEvent e )
    {
        int row = rowAtPoint( e.getPoint() );
        int column = columnAtPoint( e.getPoint() );

        Object value = getValueAt(row, column);
        return value == null ? null : value.toString();
    }
}
;

It took me a while to find out how to display the tooltip on Netbeans but your answer helped so much.
here it is implemented on netbeans GUI Builder...
right click on your Jtable->customize code.
choose "custom creation" where the new "javax.swing.JTable();" code is and before the semi colon ; add the code of the answer below... looks like this:

YourTable = new javax.swing.JTable(){
    //add tooltip to display the full cell text when not displayed
    public String getToolTipText( MouseEvent e )
    {
        int row = rowAtPoint( e.getPoint() );
        int column = columnAtPoint( e.getPoint() );

        Object value = getValueAt(row, column);
        return value == null ? null : value.toString();
    }
}
;
泼猴你往哪里跑 2024-11-13 01:59:55

将鼠标运动侦听器添加到 JTable,获取鼠标所在位置的值,然后将长值添加到工具提示中,这是一种有效的方法,可以在鼠标悬停在该行上时轻松查看较长的表格单元格值:

    JTable yourTable = new JTable(<the data>, <the fields>);

    // Add a mouse motion listener to your JTable
    yourTable.addMouseMotionListener(new MouseMotionListener() {
        @Override
        public void mouseDragged(MouseEvent e) {
            // do nothing
        }

        @Override
        public void mouseMoved(MouseEvent e) {

            // Set the tooltip to an empty string
            yourTable.setToolTipText("");

            // Get the table cell value where the mouse is located
            String value = (String) yourTable.getValueAt(yourTable.rowAtPoint(e.getPoint()),
                    yourTable.columnAtPoint(e.getPoint()));

            // If the length of the value is greater than some number... 
            if (value.length() > 50) {

                // add a tooltip to the JTable that shows the value
                yourTable.setToolTipText(value);
            }
        }
    });

查看结果此处:https://i.sstatic.net/3LHyc.png

Adding a mouse motion listener to your JTable, getting the value of where the mouse is at, then adding the long value to a tooltip is an efficient way to easily see the longer table cell value when the mouse is hovered over that row:

    JTable yourTable = new JTable(<the data>, <the fields>);

    // Add a mouse motion listener to your JTable
    yourTable.addMouseMotionListener(new MouseMotionListener() {
        @Override
        public void mouseDragged(MouseEvent e) {
            // do nothing
        }

        @Override
        public void mouseMoved(MouseEvent e) {

            // Set the tooltip to an empty string
            yourTable.setToolTipText("");

            // Get the table cell value where the mouse is located
            String value = (String) yourTable.getValueAt(yourTable.rowAtPoint(e.getPoint()),
                    yourTable.columnAtPoint(e.getPoint()));

            // If the length of the value is greater than some number... 
            if (value.length() > 50) {

                // add a tooltip to the JTable that shows the value
                yourTable.setToolTipText(value);
            }
        }
    });

See result here: https://i.sstatic.net/3LHyc.png

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