无论如何,我可以突出显示 JTable 中的一行吗?

发布于 2024-10-28 20:08:25 字数 129 浏览 2 评论 0原文

我目前正在使用 JTable 和 DefaultTableModel 构建数据库。在我的程序中我 具有允许用户搜索数据库的功能。我已经构建了搜索部分,但没有 知道如何突出显示 JTable 中的行或单元格。有人可以帮我吗?

谢谢

I am currently building a database using JTable and DefaultTableModel. In my program I
have a function which allows users to search the database. I have the search part build but I don't
know how to highlight a row or a cell in the JTable. Can someone please help me?

Thank you

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

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

发布评论

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

评论(3

Hello爱情风 2024-11-04 20:08:25

您确定要突出显示而不是过滤掉无关的结果吗?如果突出显示,则必须滚动整个列表才能找到所有匹配的结果,而如果过滤显示,则更容易找到所需的内容。

如果您选择过滤路线,我会考虑 GlazedLists,这是一个真正出色的 Java 库,用于执行动态过滤等操作JTables的排序、排序等。

如果您仍然想走突出显示路线,那么我认为有两种主要方法可以实现这一目标。第一种是使用 ListSelectionModel JTable 并确保所有匹配的行都在选定的集合中;这将使它们通过最少的编码在视觉上得以区分。另一方面,一旦用户在表格中拖动并选择其他内容,视觉效果就会丢失。

实现此目的的第二种方法是使用自定义 TableCellRenderer,如果行符合您的选择条件,它会更改行的呈现方式。一个简单的方法是更改​​背景颜色。

Are you sure you want to highlight as opposed to filter out the extraneous results? If you highlight, you'll have to scroll through the whole list to find all of the matching results, whereas if you filter the display, it's a lot easier to find what you're looking for.

If you go the filtering route I'd look into GlazedLists, a truly great Java library for doing things like dynamic filtering, sorting, etc. of JTables.

If you still want to go the highlighting route, then there's two main ways I see of accomplishing this. The first is to use the ListSelectionModel of the JTable and ensure that all of the matching rows are in the selected set; this will cause them to be visually distinguished with a minimum of coding. On the other hand, as soon as the user drags in the table and selects something else, the visual effect is lost.

The second way to accomplish this would be to use a custom TableCellRenderer that changes how a row is rendered if the row matches your selection criteria. An easy way to do that would be to change the background color.

司马昭之心 2024-11-04 20:08:25

关于如何使用表的 Swing 教程有一个部分进行过滤,这样您就可以只显示符合搜索条件的数据。如果您想查看所有数据,那么您只需删除过滤器即可。

如果您确实想单独突出显示,那么我会看看 表行渲染方法。

The Swing tutorial on How to Use Tables has a section on filtering so you can just dislay the data that meet the search criteria. If you want to see all the data, then you just remove the filter.

If you really want to do separate highlighting then I would take a look at the Table Row Rendering approach.

为你鎻心 2024-11-04 20:08:25

我可以告诉你我是怎么做的。
我将搜索实现为文档中的搜索,即一次查找单个结果。我正在存储所选行的当前索引,或者如果之前没有选择行,则从第一行开始。然后我让我的模型实现我的界面,具有搜索下一个或上一个匹配的功能,下面的示例显示了查找下一个匹配方法的使用,该方法返回找到匹配字符串的表中的行的索引,然后我将选择更改为否则我会清除选择,让用户知道没有匹配项。


    int index = serchableTableModel.findNextMatchIndex(serchedText, currentIndex);
    if(index != -1)
    table.changeSelection(index, 0, false, false);
    else
    table.clearSelection();

我希望这能解决你的问题。

注意:我之前不知道有光泽的列表,它们看起来确实很有前途。他们可以帮助我实现表格排序、自我搜索。

I can tell you how I am doing it.
I implemented my search to work as the search in a document, i.e. finding single result at a time. I am storing the current index of the selected row or starting from the first one if no row was previously selected. Then I have my model implement my interface with functionality to search for next or previous match, example below shows use of find next match method which returns an index of the row in a table where the matching string was found, then I change the selection to it else I clear the selection to let the user know there is no match.


    int index = serchableTableModel.findNextMatchIndex(serchedText, currentIndex);
    if(index != -1)
    table.changeSelection(index, 0, false, false);
    else
    table.clearSelection();

I hope this sorts your problem.

NOTE: I was not aware of the glazed lists before, they really seem promising. They would have saved me implementing sorting of tables, searching myself.

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