29

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

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

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

4

8 回答 8

13

您可能想在 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

于 2009-01-27T22:31:20.907 回答
10


Sun 的教程“使用 Swing 组件”中有一个文本区域自动完成的示例。

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

只需向下滚动到“另一个示例:TextAreaDemo”
,然后 点击启动按钮!

于 2009-06-14T09:13:41.947 回答
7

这是我的简化示例。遗憾的是,在开始输入之前,您必须先单击文本字段,否则会出现异常。如果有人能找出原因,请让我知道/更新这个答案。

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
  }

}
于 2009-06-14T09:27:05.767 回答
3

这是一篇很棒的文章,它使用了几个库:向 Swing Comboboxes @Java.net添加自动完成支持

于 2010-08-26T11:32:24.190 回答
3

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

于 2010-09-21T09:06:16.480 回答
1

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

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

于 2010-03-11T06:47:46.737 回答
0

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

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

于 2009-01-27T22:13:57.870 回答
0

用这个

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);
                            }
                        };
                }
                ;
            });
    }
}
于 2009-08-21T21:06:47.667 回答