简体   繁体   English

在JTable中进行搜索时,如何从JTable中以编程方式选择特定文本?

[英]how can i programatically select a particular text from JTable when i do search in it?

here are the screenshots of the application 这是该应用程序的屏幕截图

这就是我们运行它时的样子

我要这样

Rows will be displayed in the Table according to the text which is written in the search textfield. 根据搜索文本字段中写入的文本,表格中将显示行。 Now i want to mark that particular text as per the shown in the second image with the yellow color I know how to select a row or a particular cell. 现在我想用第二个图像中的黄色标记该特定文本,我知道如何选择行或特定单元格。 but I don't know how to select a particular text inside the cell of any row in the table. 但我不知道如何在表格中任何行的单元格中选择特定的文本。

I am guessing you know how to search in JTable, so I am not pasting code of it here. 我猜您知道如何在JTable中搜索,因此我不在这里粘贴代码。

You can look over the SwingX library. 您可以查看SwingX库。 It has this kind of function as you said predefined it it. 正如您所说的那样,它具有这种功能。 You just need to add it to your table. 您只需要将其添加到表中即可。 This is where you can find it. 是在那里你可以找到它。 Give it a try you will surely like it. 试试看,您一定会喜欢的。

The basic premise would be to use a custom TableCellRenderer that provided the functionality that you require. 基本前提是使用提供所需功能的自定义TableCellRenderer

The problem is how to implement it. 问题是如何实现它。

I would create a TableCellRenderer based on a JTextField , remove it's border and make it transparent. 我将基于JTextField创建一个TableCellRenderer ,删除它的边框并使其透明。 This will allow you to use the text highlighting functionality provided by JTextCompoent to highlight portions of the text, as demonstrated here . 这将允许您使用所提供的文本加亮功能JTextCompoent来突出显示文本的部分,这表现在这里

The next problem is then knowing what to highlight. 然后,下一个问题就是知道要突出显示的内容。 There are a number of possibilities. 有很多可能性。

You could provide a method in your table model that could return the current text that should be highlighted. 您可以在表模型中提供一个方法,该方法可以返回应突出显示的当前文本。

I'd, personally, probably use the JTable#putClientProperty and JTable#getClientProperty methods to seed the search text. 我个人可能会使用JTable#putClientPropertyJTable#getClientProperty方法来播种搜索文本。

Or, you could actually provide a simple model that directly to the renderer which had a method that returned the current search text. 或者,您实际上可以提供直接提供给渲染器的简单模型,该渲染器具有返回当前搜索文本的方法。 This might actually be more useful as you could link it to field, the method building the filter and the renderers and allow them to simply seed each other 实际上,这可能会更有用,因为您可以将其链接到字段,构建过滤器和渲染器的方法,并允许它们彼此简单地设置种子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM