【问题标题】:Editable JCombobox and the key listener [closed]可编辑的 JCombobox 和关键侦听器 [关闭]
【发布时间】:2013-04-18 04:10:24
【问题描述】:

如何在 jcombobox 中添加关键事件侦听器,以便它接受整个字符串作为 ex。如果我添加“S”,它应该在数据库中搜索以“s”开头的字符串。如果没有结果,那么它应该等待用户在组合框中键入其他字符以获得类似的结果

【问题讨论】:

  • 所以现在我们有了要求,您的问题是什么......
  • 我认为你需要为你的项目写一些代码
  • if there are no result then it should wait for the user to type other character in the combobox 用户如何知道没有结果,他/她需要输入另一个字符?
  • 并且可以从剪贴板插入字符串,然后 KeyListener 没有反应...,必须使用 Document,从可编辑 JComboBox 派生的 JTextComponent,这里有一些代码示例,可能在 DB 中自动完成

标签: java swing jdbc autocomplete jcombobox


【解决方案1】:

检查这是否有帮助-

    comboBox.addKeyListener(new KeyAdapter() {
        @Override
        public void keyReleased(KeyEvent ke) {
            // get the text typed and search for it
            // get the text from combo box, and not from the key event
        }
    });

【讨论】:

    【解决方案2】:

    您可以选择自己实现,也可以使用Java2sAutoTextFieldJava2sAutoComboBox。它们都易于使用。
    您只需提供一个 java.util.List<>,其中包含从您的数据库中提取的数据,它会为您完成自动完成的任务。
    为了更好地了解,请在此处查看我的问题:Implementing auto complete in Java - am I doing it right?

    您还可以选择使用GlazedLists
    我相信我已经充分回答了你的问题:)

    【讨论】:

    • +1 避免无限循环和 JDBC 查询
    猜你喜欢
    • 2012-06-11
    • 1970-01-01
    • 1970-01-01
    • 2013-12-20
    • 2013-01-04
    • 1970-01-01
    • 2021-08-02
    • 2014-12-28
    • 2019-01-24
    相关资源
    最近更新 更多