无论如何我可以在JTable中突出显示一行吗? [英] Is there anyway I can highlight a row in JTable?

查看:106
本文介绍了无论如何我可以在JTable中突出显示一行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用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?

谢谢

推荐答案

你是吗?你想要突出显示而不是过滤掉无关的结果吗?如果您突出显示,则必须滚动整个列表才能找到所有匹配的结果,而如果您过滤显示,则可以更轻松地找到您要查找的内容。

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.

如果你采用过滤途径,我会查看 GlazedLists ,这是一个真正优秀的Java用于执行JTables的动态过滤,排序等操作的库。

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.

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

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.

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

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.

这篇关于无论如何我可以在JTable中突出显示一行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆