Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在文本字段内键入时列出建议_Java_Swing_Autocomplete_Jtextfield_Autosuggest - Fatal编程技术网

Java 如何在文本字段内键入时列出建议

Java 如何在文本字段内键入时列出建议,java,swing,autocomplete,jtextfield,autosuggest,Java,Swing,Autocomplete,Jtextfield,Autosuggest,嗨,在我的JavaSwing应用程序中,我需要用相同的首字母显示所有可能存在的条目 用户在文本字段中键入时使用的字符。假设用户在文本字段中键入字母“A”,该字段应为国家“美国”,建议为“非洲”。以便用户可以从列表中选择一个。国家/地区列表位于我的数据库中。我知道如何从数据库中检索数据。但我不知道如何列出它们,以及应该使用哪些组件来实现。 有人能指引我吗?或者如果你能举个例子,那就太好了 您可以使用。如果您需要更多的实际示例,请在Google上搜索“swingx autocomplete”,但最简

嗨,在我的JavaSwing应用程序中,我需要用相同的首字母显示所有可能存在的条目 用户在文本字段中键入时使用的字符。假设用户在文本字段中键入字母“A”,该字段应为国家“美国”,建议为“非洲”。以便用户可以从列表中选择一个。国家/地区列表位于我的数据库中。我知道如何从数据库中检索数据。但我不知道如何列出它们,以及应该使用哪些组件来实现。 有人能指引我吗?或者如果你能举个例子,那就太好了

您可以使用。如果您需要更多的实际示例,请在Google上搜索“swingx autocomplete”,但最简单的方法是创建
JComboBox
并调用


自动完成装饰(组合框)

您应该尝试将JComboBox作为自动建议框,而不是JTextField。但是如果您仍然希望使用JTextField来完成,那么

  • 制作一个包含建议列表的JPanel。最初它将不可见
  • 每当用户键入某个内容时,搜索该内容并将结果添加到JPanel中的列表中
  • 显示帧上层textfield底部的JPanel
  • 在列表上实现click事件,以便用户每次单击时,文本都会复制到textfield

  • 自动完成JTextField和自动完成JComboBox是两个类(正确的函数都需要您),其优点是您可以设置是否严格(如果列表不包含,则允许键入)

    我有一个类似的问题:
    我想要一个文本字段来自由输入文本,但可以建议现有的值

    我第一次发现这篇文章,但是swingX不允许输入建议之外的文本。 然后我发现了另一篇链接到此页面的帖子:

    经过一些修改,我从目录选择改为自己的字符串。可能会在此处发布此信息以供完成,并为以后的搜索者提供帮助:

    他制作了一个抽象类
    AutoCompleter
    ,用于处理事件:

    // @author Santhosh Kumar T - santhosh@in.fiorano.com 
    public abstract class AutoCompleter{ 
        JList list = new JList(); 
        JPopupMenu popup = new JPopupMenu(); 
        JTextComponent textComp; 
        private static final String AUTOCOMPLETER = "AUTOCOMPLETER"; //NOI18N 
    
        public AutoCompleter(JTextComponent comp){ 
            textComp = comp; 
            textComp.putClientProperty(AUTOCOMPLETER, this); 
            JScrollPane scroll = new JScrollPane(list); 
            scroll.setBorder(null); 
    
            list.setFocusable( false ); 
            scroll.getVerticalScrollBar().setFocusable( false ); 
            scroll.getHorizontalScrollBar().setFocusable( false ); 
    
            popup.setBorder(BorderFactory.createLineBorder(Color.black)); 
            popup.add(scroll); 
    
            if(textComp instanceof JTextField){ 
                textComp.registerKeyboardAction(showAction, KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), JComponent.WHEN_FOCUSED); 
                textComp.getDocument().addDocumentListener(documentListener); 
            }else 
                textComp.registerKeyboardAction(showAction, KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, KeyEvent.CTRL_MASK), JComponent.WHEN_FOCUSED); 
    
            textComp.registerKeyboardAction(upAction, KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), JComponent.WHEN_FOCUSED); 
            textComp.registerKeyboardAction(hidePopupAction, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_FOCUSED); 
    
            popup.addPopupMenuListener(new PopupMenuListener(){ 
                public void popupMenuWillBecomeVisible(PopupMenuEvent e){ 
                } 
    
                public void popupMenuWillBecomeInvisible(PopupMenuEvent e){ 
                    textComp.unregisterKeyboardAction(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0)); 
                } 
    
                public void popupMenuCanceled(PopupMenuEvent e){ 
                } 
            }); 
            list.setRequestFocusEnabled(false); 
        } 
    
        static Action acceptAction = new AbstractAction(){ 
            public void actionPerformed(ActionEvent e){ 
                JComponent tf = (JComponent)e.getSource(); 
                AutoCompleter completer = (AutoCompleter)tf.getClientProperty(AUTOCOMPLETER); 
                completer.popup.setVisible(false); 
                completer.acceptedListItem((String)completer.list.getSelectedValue()); 
            } 
        }; 
    
        DocumentListener documentListener = new DocumentListener(){ 
            public void insertUpdate(DocumentEvent e){ 
                showPopup(); 
            } 
    
            public void removeUpdate(DocumentEvent e){ 
                showPopup(); 
            } 
    
            public void changedUpdate(DocumentEvent e){} 
        }; 
    
        private void showPopup(){ 
            popup.setVisible(false); 
            if(textComp.isEnabled() && updateListData() && list.getModel().getSize()!=0){ 
                if(!(textComp instanceof JTextField)) 
                    textComp.getDocument().addDocumentListener(documentListener); 
                textComp.registerKeyboardAction(acceptAction, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), JComponent.WHEN_FOCUSED); 
                int size = list.getModel().getSize(); 
                list.setVisibleRowCount(size<10 ? size : 10); 
    
                int x = 0; 
                try{ 
                    int pos = Math.min(textComp.getCaret().getDot(), textComp.getCaret().getMark()); 
                    x = textComp.getUI().modelToView(textComp, pos).x; 
                } catch(BadLocationException e){ 
                    // this should never happen!!! 
                    e.printStackTrace(); 
                } 
                popup.show(textComp, x, textComp.getHeight()); 
            }else 
                popup.setVisible(false); 
            textComp.requestFocus(); 
        } 
    
        static Action showAction = new AbstractAction(){ 
            public void actionPerformed(ActionEvent e){ 
                JComponent tf = (JComponent)e.getSource(); 
                AutoCompleter completer = (AutoCompleter)tf.getClientProperty(AUTOCOMPLETER); 
                if(tf.isEnabled()){ 
                    if(completer.popup.isVisible()) 
                        completer.selectNextPossibleValue(); 
                    else 
                        completer.showPopup(); 
                } 
            } 
        }; 
    
        static Action upAction = new AbstractAction(){ 
            public void actionPerformed(ActionEvent e){ 
                JComponent tf = (JComponent)e.getSource(); 
                AutoCompleter completer = (AutoCompleter)tf.getClientProperty(AUTOCOMPLETER); 
                if(tf.isEnabled()){ 
                    if(completer.popup.isVisible()) 
                        completer.selectPreviousPossibleValue(); 
                } 
            } 
        }; 
    
        static Action hidePopupAction = new AbstractAction(){ 
            public void actionPerformed(ActionEvent e){ 
                JComponent tf = (JComponent)e.getSource(); 
                AutoCompleter completer = (AutoCompleter)tf.getClientProperty(AUTOCOMPLETER); 
                if(tf.isEnabled()) 
                    completer.popup.setVisible(false); 
            } 
        }; 
    
        /** 
         * Selects the next item in the list.  It won't change the selection if the 
         * currently selected item is already the last item. 
         */ 
        protected void selectNextPossibleValue(){ 
            int si = list.getSelectedIndex(); 
    
            if(si < list.getModel().getSize() - 1){ 
                list.setSelectedIndex(si + 1); 
                list.ensureIndexIsVisible(si + 1); 
            } 
        } 
    
        /** 
         * Selects the previous item in the list.  It won't change the selection if the 
         * currently selected item is already the first item. 
         */ 
        protected void selectPreviousPossibleValue(){ 
            int si = list.getSelectedIndex(); 
    
            if(si > 0){ 
                list.setSelectedIndex(si - 1); 
                list.ensureIndexIsVisible(si - 1); 
            } 
        } 
    
        // update list model depending on the data in textfield 
        protected abstract boolean updateListData(); 
    
        // user has selected some item in the list. update textfield accordingly... 
        protected abstract void acceptedListItem(String selected); 
    }
    
    在程序中使用

    new FileAutoCompleter(yourJTextField);
    

    美洲和非洲不是国家……它们是大陆,像欧洲、亚洲或大洋洲。这些都是以“A”开头的国家:我有同样的问题,然后我尝试使用SwingX,但不起作用,请遵循我的主题:您链接到的服务器已关闭。这就是答案中链接的问题所在。
    new FileAutoCompleter(yourJTextField);