Autocomplete

Autocomplete

Released 5 years ago , Last update 1 year ago

Versatile and customizable autocomplete widget for Swing text components.

Swing Autocomplete

Generic, easy-to-use autocomplete widget that can be readily plugged into all of your Swing text components[1].

Introduction

Autocomplete widget is used to auto-complete a user's keystrokes by providing self-defined, context-sensitive completions in a drop-down menu. It can be plugged into any instances of JTextComponent including JTextField, JTextArea and JTextPane.

Autocomplete reads and parses the content typed by the user into a token based on a defined pattern (delimiter). The token is then used to query suitable completions from a user-defined engine called CompletionProvider. A CompletionProvider is used to connect to a data source and supply the drop-down menu's contents according to the given token and context. The drop-down menu is automatically activated when contents become available for the menu.

Features

  • Supports custom completion providers to connect to any data source
  • Configurable delimiter/ rule for parsing completion tokens
  • Able to set the minimum prefix length for completions to be fetched
  • Able to set the maximum rows to display in the menu
  • Able to customize the font color and background colors
  • Able to set the shortcut keystroke for invoking the menu*
  • Able to pre-complete the user input immediately when a completion is selected*
  • Able to set the visible rows to display in the menu*
  • Able to customize the menu cell renderer*
  • Able to register listeners to listen for various events including selection change events, completion accepted events, popup menu events etc

* Not available in the binary version

Remarks

  1. Tested on standard JTextField, JTextArea and JTextPane. Fitness of the software is subject to the purpose of use and certain technical limitations.

Autocomplete is a successor to discontinued Suggestion List published on Binpress by the same author. Download the binary version for free today. Get Application or Developer license for full source code and complete functionalities.

Usage Examples

Example 1: Using an array or List as the data source

public class TestAutocomplete extends JFrame {

public TestAutocomplete() {
JTextField jtf = new JTextField();
JTextArea jta = new JTextArea();
jta.setLineWrap(true);
jta.setWrapStyleWord(true);

String[] entries = {"auto", "autobus", "autocorrect", "autocomplete",
"autograph", "automate", "automated", "automates", "automatic",
"automatically", "automation", "automobile", "autonomous",
"autonomy", "autopilot", "autopsied", "autopsies", "autopsy"};

// create an autocomplete for JTextField
Autocomplete autocomplete = new Autocomplete(entries, true);
autocomplete.setMaximumRowCount(10);
// configure autocomplete to read the token from the beginning of the line
autocomplete.setDelimiter(Autocomplete.LINE_START_PATTERN);
// configure autocomplete to read the token up to the newline rather than the caret position
autocomplete.setCompletionBoundary(Autocomplete.CompletionBoundary.NEWLINE);
// ATTENTION: the following methods whose comment is marked with ** are not available in the binary version
// make autocomplete select/ highlight a menu item when the mouse pointer rolls over it**
autocomplete.setSelectedOnRollover(true);
// make autocomplete pre-insert a completion into the document immediately as it is selected**
autocomplete.setCompleteImmediately(true);
// allow the menu to clear its selection when user navigates past the first/ last item using the keyboard keys**
autocomplete.setListNavigationMode(ListNavigationMode.EXTENDED);
// set the visible row count**
autocomplete.setVisibleRowCount(5);
// set the shortcut key to trigger the autocomplete menu**
autocomplete.setShortcutKey(KeyStroke.getKeyStroke("pressed DOWN"));
autocomplete.setTextComponent(jtf);

// create an autocomplete for JTextArea
Autocomplete autocomplete2 = new Autocomplete(Arrays.asList(entries), true);
autocomplete2.setMinimumPrefixLength(1);
autocomplete2.setMaximumRowCount(10);
// set the shortcut key to trigger the autocomplete menu**
autocomplete2.setShortcutKey(KeyStroke.getKeyStroke("shift pressed TAB"));
autocomplete2.setTextComponent(jta);

add(jtf, BorderLayout.NORTH);
add(jta, BorderLayout.CENTER);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new TestAutocomplete();
frame.setTitle("TestAutocomplete");
frame.setSize(300, 200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
 

 Example 2: Using a custom completion provider to connect to the data source

public class CustomCompletionProvider extends JFrame {

JdbcCompletionProvider completionProvider;

class JdbcCompletionProvider implements CompletionProvider<String> {

Connection conn = null;

public JdbcCompletionProvider(String url, String user, String password) throws SQLException {
conn = DriverManager.getConnection(url, user, password);
}

@Override
public Collection<String> getCompletions(TokenChangedEvent tokenChangedEvent) {
List<String> result = new ArrayList<String>();
PreparedStatement stmt = null;
try {
int maxCount = tokenChangedEvent.getLimitHint();
String inputParam = tokenChangedEvent.getToken() + "%";
stmt = conn.prepareStatement("SELECT description FROM Product WHERE description LIKE ? ORDER BY description");
stmt.setString(1, inputParam);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
result.add(rs.getString(1));
if (result.size() >= maxCount) {
break;
}
}
rs.close();
stmt.close();
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException ex) {
}
}
return result;
}

public void closeConnection() {
if (conn != null) {
try {
conn.close();
conn = null;
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}

}

public CustomCompletionProvider() {
JTextField jtf = new JTextField();

try {
this.completionProvider = new JdbcCompletionProvider("jdbc:oracle:thin:@localhost:1521:XE", "user", "passwd123");
Autocomplete autocomplete = new Autocomplete(completionProvider, Autocomplete.DEFAULT_CONVERTER);
autocomplete.setDelimiter(Autocomplete.LINE_START_PATTERN);
autocomplete.setCompletionBoundary(CompletionBoundary.NEWLINE);
autocomplete.setMinimumPrefixLength(1);
autocomplete.setMaximumRowCount(8);
autocomplete.setTextComponent(jtf);
} catch (SQLException ex) {
ex.printStackTrace();
}

getContentPane().add(jtf, BorderLayout.NORTH);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
if (completionProvider != null) {
completionProvider.closeConnection();
}
}
});
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new CustomCompletionProvider();
frame.setTitle("Using Completion Provider");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}

Pricing

14 day 14-day money-back guarantee

$0.00

Binary License

  • Perpetual license

  • unlimited applications

  • Can distribute binary products only

  • Commercial use

$59.99

Developer License

  • Perpetual license

  • Unlimited projects

  • Can distribute code and binary products

  • Commercial use

  • 3 months support

3 licenses, starting from From » FREE View Licenses

Get A Quote

What do you need?
  • Custom development
  • Integration
  • Customization / Reskinning
  • Consultation
When do you need it?
  • Soon
  • Next week
  • Next month
  • Anytime

Thanks for getting in touch!

Your quote details have been received and we'll get back to you soon.


Or enter your name and Email
No comments have been posted yet.