自动完成Combobox

时间:2013-12-13 13:16:02

标签: java javafx javafx-2 javafx-8

我想创建自动完整的可编辑组合框:

final ComboBox emailComboBox = new ComboBox();
        emailComboBox.getItems().addAll(
            "jacob.smith@example.com",
            "isabella.johnson@example.com",
            "ethan.williams@example.com",
            "emma.jones@example.com",
            "michael.brown@example.com"  
        );

自动完成课程:

public class AutoCompleteComboBoxListener<T> implements EventHandler<KeyEvent>
{

    private ComboBox comboBox;
    private StringBuilder sb;
    private ObservableList<T> data;
    private boolean moveCaretToPos = false;
    private int caretPos;

    public AutoCompleteComboBoxListener(final ComboBox comboBox)
    {
        this.comboBox = comboBox;
        sb = new StringBuilder();
        data = comboBox.getItems();

        this.comboBox.setEditable(true);
        this.comboBox.setOnKeyPressed(new EventHandler<KeyEvent>()
        {

            @Override
            public void handle(KeyEvent t)
            {
                comboBox.hide();
            }
        });
        this.comboBox.setOnKeyReleased(AutoCompleteComboBoxListener.this);
    }

    @Override
    public void handle(KeyEvent event)
    {
        ListView lv = ((ComboBoxListViewSkin) comboBox.getSkin()).getListView();

        if (event.getCode() == KeyCode.UP)
        {
            caretPos = -1;
            moveCaret(comboBox.getEditor().getText().length());
            return;
        }
        else if (event.getCode() == KeyCode.DOWN)
        {
            if (!comboBox.isShowing())
            {
                comboBox.show();
            }
            caretPos = -1;
            moveCaret(comboBox.getEditor().getText().length());
            return;
        }
        else if (event.getCode() == KeyCode.BACK_SPACE)
        {
            moveCaretToPos = true;
            caretPos = comboBox.getEditor().getCaretPosition();
        }
        else if (event.getCode() == KeyCode.DELETE)
        {
            moveCaretToPos = true;
            caretPos = comboBox.getEditor().getCaretPosition();
        }

        if (event.getCode() == KeyCode.RIGHT || event.getCode() == KeyCode.LEFT
            || event.isControlDown() || event.getCode() == KeyCode.HOME
            || event.getCode() == KeyCode.END || event.getCode() == KeyCode.TAB)
        {
            return;
        }

        ObservableList list = FXCollections.observableArrayList();
        for (int i = 0; i < data.size(); i++)
        {
            if (data.get(i).toString().toLowerCase().startsWith(
                AutoCompleteComboBoxListener.this.comboBox
                .getEditor().getText().toLowerCase()))
            {
                list.add(data.get(i));
            }
        }
        String t = comboBox.getEditor().getText();

        comboBox.setItems(list);
        comboBox.getEditor().setText(t);
        if (!moveCaretToPos)
        {
            caretPos = -1;
        }
        moveCaret(t.length());
        if (!list.isEmpty())
        {
            comboBox.show();
        }
    }

    private void moveCaret(int textLength)
    {
        if (caretPos == -1)
        {
            comboBox.getEditor().positionCaret(textLength);
        }
        else
        {
            comboBox.getEditor().positionCaret(caretPos);
        }
        moveCaretToPos = false;
    }

}


AutoCompleteComboBoxListener autoCompleteComboBoxListener = new AutoCompleteComboBoxListener(emailComboBox);


        grid.add(emailComboBox, 1, 9);

但是当我想插入自动完成组合框时,我收到错误:

incompatible types: AutoCompleteComboBoxListener cannot be converted to Node

你能告诉我如何修改代码以返回节点吗?

2 个答案:

答案 0 :(得分:1)

过滤模式下的

自动填充文本框请参阅此示例和自动填充的源代码 Auto Fill Text box example

自动填充文本框源代码:Source Code

答案 1 :(得分:0)

小型实用程序库jalvafx中的解决方案

List<String> items = Arrays.asList("Mercury", 
                                   "Venus", 
                                   "Earth", 
                                   "Mars", 
                                   "Jupiter", 
                                   "Saturn", 
                                   "Neptune");

ComboBoxCustomizer.create(comboBox)
                  .autocompleted(items)
                  .customize();

默认情况下,双击以清除值。

还有一些其他有用的功能。您可以添加额外的列或字形,选择特定项目,将项目默认更改为字符串表示形式...