具有多列的ComboBox - 按键搜索

时间:2022-08-24 20:08:28

I have a JComboBox that displays 2 columns. Now I would like to enable to search by key on all columns. Example:

我有一个显示2列的JComboBox。现在我想启用所有列的按键搜索。例:

Column1 | Column2
A1      | S1
A2      | B1
A3      | P1

The search by key on the first column works fine with default Implementation of KeySelectionManager for JComboBox. However, I would also like to be able to search for the second column as well, meaning when I press 'B' the second item is selected.

第一列上的按键搜索可以正常使用KeyComlectionManager for JComboBox的默认实现。但是,我也希望能够搜索第二列,这意味着当我按下'B'时,第二个项目被选中。

I have taken a look at the KeySelectionManager but didn't find anything useful. I have attached a screenshot of the ComboBox to show what I mean.

我看了一下KeySelectionManager,但没有找到任何有用的东西。我附上了ComboBox的截图,以显示我的意思。

Thanks for any pointers.

谢谢你的任何指示。

具有多列的ComboBox  - 按键搜索

2 个解决方案

#1


3  

Check KeySelectionManager implemetation in JComboBox's source code

检查JComboBox源代码中的KeySelectionManager实现

class DefaultKeySelectionManager implements KeySelectionManager, Serializable {
    public int selectionForKey(char aKey,ComboBoxModel aModel) {
....
       for ( i = ++currentSelection, c = aModel.getSize() ; i < c ; i++ ) {
            Object elem = aModel.getElementAt(i);
            if (elem != null && elem.toString() != null) {
                v = elem.toString().toLowerCase();
                if ( v.length() > 0 && v.charAt(0) == aKey )
                    return i;
            }
        }

So try to override your model's Element's class toString() method to include both columns.

因此,尝试覆盖模型的Element的类toString()方法以包含两个列。

#2


1  

If you display two columns in the combo box then you must be using a custom renderer.

如果在组合框中显示两列,则必须使用自定义渲染器。

So maybe the approach presented in Combo Box With Custom Renderer can help. The solution in the blog combines the renderer and KeySelectionManager into a single class. All you need to do is implement the getDisplayValue() method to return the text to renderer.

因此,使用自定义渲染器的组合框中提供的方法可能会有所帮助。博客中的解决方案将渲染器和KeySelectionManager组合到一个类中。您需要做的就是实现getDisplayValue()方法以将文本返回到渲染器。

In your case you have two pieces of text to render and search, so you might be able to just change the getDisplayValue() method to return List of text you want to display.

在您的情况下,您有两个要呈现和搜索的文本,因此您可以只更改getDisplayValue()方法以返回要显示的文本列表。

Then the renderer can use both items in the List and the getNextMatch() method of the class would also be modified to check each item in the list for a match.

然后渲染器可以使用List中的两个项目,并且还将修改类的getNextMatch()方法以检查列表中的每个项目是否匹配。

#1


3  

Check KeySelectionManager implemetation in JComboBox's source code

检查JComboBox源代码中的KeySelectionManager实现

class DefaultKeySelectionManager implements KeySelectionManager, Serializable {
    public int selectionForKey(char aKey,ComboBoxModel aModel) {
....
       for ( i = ++currentSelection, c = aModel.getSize() ; i < c ; i++ ) {
            Object elem = aModel.getElementAt(i);
            if (elem != null && elem.toString() != null) {
                v = elem.toString().toLowerCase();
                if ( v.length() > 0 && v.charAt(0) == aKey )
                    return i;
            }
        }

So try to override your model's Element's class toString() method to include both columns.

因此,尝试覆盖模型的Element的类toString()方法以包含两个列。

#2


1  

If you display two columns in the combo box then you must be using a custom renderer.

如果在组合框中显示两列,则必须使用自定义渲染器。

So maybe the approach presented in Combo Box With Custom Renderer can help. The solution in the blog combines the renderer and KeySelectionManager into a single class. All you need to do is implement the getDisplayValue() method to return the text to renderer.

因此,使用自定义渲染器的组合框中提供的方法可能会有所帮助。博客中的解决方案将渲染器和KeySelectionManager组合到一个类中。您需要做的就是实现getDisplayValue()方法以将文本返回到渲染器。

In your case you have two pieces of text to render and search, so you might be able to just change the getDisplayValue() method to return List of text you want to display.

在您的情况下,您有两个要呈现和搜索的文本,因此您可以只更改getDisplayValue()方法以返回要显示的文本列表。

Then the renderer can use both items in the List and the getNextMatch() method of the class would also be modified to check each item in the list for a match.

然后渲染器可以使用List中的两个项目,并且还将修改类的getNextMatch()方法以检查列表中的每个项目是否匹配。