【问题标题】:How could I implement autocompletion using Swing?如何使用 Swing 实现自动完成?
【发布时间】:2010-10-03 21:16:51
【问题描述】:

我有兴趣在 JFrame 中提供一个自动完成框。触发机制将基于助记符(我认为),但我不太确定“自动完成框”使用什么(我希望在用户按键时过滤结果)。

您将如何实现这一点?某种 JFrame 或 JPopupMenu?

我想知道这是如何实现的,所以请不要发布指向可用 [J] 组件的链接。

【问题讨论】:

  • 请在得到解决方案后发布您的解决方案。
  • 另见this类似答案

标签: java swing autocomplete


【解决方案1】:

我会添加一个 actionListener,这样您就可以在按下每个键时获取它。

然后您可以在后台进行搜索(另一个线程)

【讨论】:

  • 但是,您会将 ActionListener 添加到哪个组件中?
  • 您将为其提供自动完成功能的组件。
  • DocumentListener 是自动完成JTextField 的更好选择。
【解决方案2】:

您可能想在 SwingLabs 试用免费的 AutoComplete 组件。

http://swinglabs.org

编辑:此站点似乎已移动 http://java.net/projects/swinglabs

有一个如何实现此代码的示例:

http://download.java.net/javadesktop/swinglabs/releases/0.8/docs/api/org/jdesktop/swingx/autocomplete/package-summary.html

【讨论】:

【解决方案3】:


Sun's tutorials "Using Swing Components"有一个文本区域自动完成的示例。

它以文字处理器的风格完成(没有弹出窗口,但是
建议的文本在光标之前输入)。

只需向下滚动到 “另一个示例:TextAreaDemo”
ant 按下启动按钮!

【讨论】:

  • 链接截至今天,已损坏。
  • 该链接再次起作用。我认为 Oracle 推翻了从 sun.com URL 中断转发的决定。
【解决方案4】:

这是我的简化示例。可悲的是,您必须先单击文本字段, 在开始输入之前,否则你会得到例外。 如果有人能找出原因,请告诉我/更新这个答案。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class _Autocompleter {

  private final static JPopupMenu textPopupMenu
      = new JPopupMenu("MENU") {

    {
      add(new JMenuItem("item 1"));
      add(new JMenuItem("item 2"));
      setFocusable(false);
    }

  };

  private final static KeyListener textInputListener
      = new KeyAdapter() {

    @Override
    public void keyTyped(KeyEvent e) {
      Point p = textInput.getCaret().getMagicCaretPosition();
      if (textPopupMenu.isVisible()) {
        SwingUtilities.convertPointToScreen(p, textInput);
        textPopupMenu.setLocation(p.x, p.y + 20);
      } else {
        textPopupMenu.show(textInput, p.x, p.y + 20);
      }
    }

  };

  private final static JTextArea textInput
      = new JTextArea("type something") {

    {
      addKeyListener(textInputListener);
      setCaretPosition(getText().length());
    }

  };

  private final static JFrame f = new JFrame("TEST") {

    {
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      add(textInput);

      setSize(400, 60);
      setLocationRelativeTo(null);
      setVisible(true);
    }

  };

  public static void main(String[] args)
      throws Exception {
        // YES, IT'S EMPTY !!!
        // It'll start anyway because of static initializers
  }

}

【讨论】:

  • 啊!我已经弄清楚出了什么问题! 1)当文本字段为空时,只需忽略插入符号的位置! 2) 使用 UndoableEditListener 代替 KeyListener。
【解决方案5】:

使用这个

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class Autocompleter2
{
    //~ Methods ------------------------------------------------------------------------------------

    public static void main(String[] args)
      throws Exception
    {
        // YES, IT'S EMPTY !!!
        // It'll start anyway because of static initializers
        SwingUtilities.invokeLater(new Runnable()
            {
                @Override
                public void run()
                {
                    final JPopupMenu textPopupMenu = new JPopupMenu("MENU")
                    {

                        {
                            add(new JMenuItem("item 1"));
                            add(new JMenuItem("item 2"));
                            setFocusable(false);
                        }
                    };

                    final JTextArea textInput = new JTextArea("type something la")
                    {

                        {
                            setCaretPosition(getText().length());
                        }
                    };

                    KeyListener textInputListener = new KeyAdapter()
                    {
                        @Override
                        public void keyTyped(KeyEvent e)
                        {
                            Point p = textInput.getCaret().getMagicCaretPosition();

                            if (textPopupMenu.isVisible())
                            {
                                SwingUtilities.convertPointToScreen(p, textInput);
                                textPopupMenu.setLocation(p.x, p.y + 20);
                            }
                            else
                            {
                                textPopupMenu.show(textInput, p.x, p.y + 20);
                            }
                        }
                    };

                    textInput.addKeyListener(textInputListener);
                    new JFrame("TEST")
                        {

                            {
                                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                                add(textInput);
                                setSize(400, 60);
                                setLocationRelativeTo(null);
                                setVisible(true);
                            }
                        };
                }
                ;
            });
    }
}

【讨论】:

    【解决方案6】:

    您可以将JEdit's textarea 与内置完成和语法高亮框架一起使用。

    一个更重量级的解决方案(从长远来看是好的)是使用NetBeans Platform

    【讨论】:

      【解决方案7】:

      这是一篇很棒的文章,它使用了几个库:Adding Auto-Complete Support to Swing Comboboxes@Java.net

      【讨论】:

      • 链接已损坏!
      【解决方案8】:

      你可以使用这个库: http://fifesoft.com/autocomplete/

      【讨论】:

        猜你喜欢
        • 2011-07-19
        • 1970-01-01
        • 1970-01-01
        • 2017-02-07
        • 1970-01-01
        • 1970-01-01
        • 2016-06-23
        • 2012-12-02
        • 1970-01-01
        相关资源
        最近更新 更多