views:

2513

answers:

9

I'm interested in providing an autocompletion box in a JFrame. The triggering mechanism will be based on mnemonics (I think), but I'm not really sure what to use for the "autocompletion box" (I would like results to be filtered as the user presses keys).

How would you implement this? Some sort of JFrame, or a JPopupMenu?

I would like to know how this is implemented, so please don't post links to available [J]Components.

A: 

I would add a actionListener so you can get each key as it is pressed.

You can can then do a search in the background (another thread)

Peter Lawrey
But , to what component would you add the ActionListener ?
Geo
The component to which you will be providing auto-completion.
Peter Lawrey
+9  A: 

You might want to try the free AutoComplete component over at SwingLabs.

http://swinglabs.org

There is an example how to implement this code at:

http://javadesktop.org/swinglabs/build/weekly/latest/swingx-HEAD/javadoc/org/jdesktop/swingx/autocomplete/package-summary.html

Philz
Riduidel
+2  A: 

There is an example for auto-completion for text area at
Sun's tutorials "Using Swing Components".

It is done in the style of word processors (no pop ups, but the
suggested text is typed ahead of the cursor).

Just scroll down to "Another Example: TextAreaDemo"
ant hit the Launch button!

ivan_ivanovich_ivanoff
+5  A: 

And, once more, but with pop-ups, as you wanted.
You can also launch it, the button is on the bottom of the page.

God bless Java Webstart

Or here, my simplified version.
Very simple, but sadly here, you have to click the text field first, before start typing, or you get exceptions.
Perhaps you could figure out what's wrong...

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
  }

}
ivan_ivanovich_ivanoff
Ah! I've figured out what's wrong! 1) Just ignore the caret position when the text field is empty! 2) Use instead of KeyListener something such as UndoableEditListener.
ivan_ivanovich_ivanoff
+1  A: 

File path autocompletion

Santhosh Kumar T
+1  A: 

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);
                        }
                    };
            }
            ;
        });
}

}

+1  A: 

You can use JEdit's textarea with built-in completion & syntax highlighting framework.

A more heavyweight solution (that is good on the long term) is use NetBeans Platform.

Hendy Irawan
+1  A: 

Here is a great article that uses a couple of libraries: Adding Auto-Complete Support to Swing Comboboxes @Java.net

pek
+1  A: 

You can use this library: http://fifesoft.com/autocomplete/

Maciej Modelski