JTable 单元格 - 处理长文本
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我只想使用工具提示。
您可以重写 JTable 的 getToolTipText(() 方法来执行此操作。
或者,如果它仅适用于某些列,您可以使用渲染器来设置工具提示文本。请参阅 指定单元格的工具提示。
I would just use a tooltip.
You can override the getToolTipText(() method of JTable to do this.
Or if its only for certain columns you can use the renderer to set the tooltip text. See Specifying Tool Tips for Cells.
我花了一段时间才找到如何在 Netbeans 上显示工具提示,但您的回答很有帮助。
这里它是在 netbeans GUI Builder 上实现的...
右键单击您的 Jtable -> 自定义代码。
选择“自定义创建”,其中新的“javax.swing.JTable();”代码是 和 位于分号之前;添加下面的答案的代码...看起来像这样:
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:
将鼠标运动侦听器添加到 JTable,获取鼠标所在位置的值,然后将长值添加到工具提示中,这是一种有效的方法,可以在鼠标悬停在该行上时轻松查看较长的表格单元格值:
查看结果此处: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:
See result here: https://i.sstatic.net/3LHyc.png