Skip to Content
Author's profile photo Former Member

TextHelp – A Search Help API for JAVA

Inspired by SAP Search Help, I was desperate of making such a component in Java for Java Swing. I developed a reusable API named TextHelp and hosted the project on Google Code. I shared the link in SCN previously through Search Help for Java. Here I would like to share a sample program using TextHelp.

You can download the binary and documentation from the following link

All you have to do is provide the database connection, the table name, columns to be shown and return value column (column from which the return value should be taken). TextHelp will show a dialog box automatically on key press (key should be provided at first). It is not having all the features of SAP Search Help. I tried to give a basic one, when I take it to further step I will share it with you for sure.

It is having only three classes and an interface.

  • TextHelp
  • TextHelpConfig
  • TextHelpListener
  • TextSelectedEvent


For each window in which one or more text fields that uses TextHelp, an instance of this class should be created. It contains the mappings of JTextFields and their corresponding TextHelpConfig


This class contains the table name from which data should be fetched, fields that should be shown on text help dialog and the field from which data should be entered into the text field. For each text field, an instance of this object should be attached.


This is an interface. textSelected method of this interface’s implementation will be called after each successful selection from the dialog box.


TextSelectedEvent contains the selected text and the row number of the table.

The following is a sample implementation.

package texthelpdemo;
import com.fareez.swing.texthelp.TextHelp;
import com.fareez.swing.texthelp.TextHelpConfig;
import com.fareez.swing.texthelp.TextHelpListener;
import com.fareez.swing.texthelp.TextSelectedEvent;
import java.awt.FlowLayout;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
 * A sample implementation
 * @author Fareez Ahamed K.N.
public class TextHelpDemo extends JFrame implements TextHelpListener {
    Connection conn;
    TextHelp th;
    JTextField tf;
    JPanel panel;
    JLabel cname;
    TextHelpConfig thc;
    TextHelpDemo() {
        tf = new JTextField(10);
        cname = new JLabel();
        try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
            th = new TextHelp(conn,KeyEvent.VK_F1,this);
        } catch (ClassNotFoundException ex) {
        } catch (SQLException ex) {
        getContentPane().setLayout(new FlowLayout());
        getContentPane().add(new JLabel("Country"));
        thc = new TextHelpConfig("countries","country_code", new String[] {"country_code","country_name"} );
        try {
            thc.setSortOrder(new String[]{ TextHelpConfig.DESC, TextHelpConfig.ASC } );
            th.attach(tf, thc);
        } catch (SQLException ex) {
    public static void main(String args[])
        TextHelpDemo test = new TextHelpDemo();
    public void textSelected(TextSelectedEvent e) {
        if(e.getTextField()==tf) {
            try {
                Statement stmt = conn.createStatement();
                ResultSet rs = stmt.executeQuery("select country_name from countries where country_code='" + e.getSelectedText() +"'");
            } catch (SQLException ex) {

I used MySQL as database here. The database contains a single table named countries with fields id, country_code and country_name. SQL to create the table and all the country names and codes are available at

Check out the output.


On pressing F1


After Selection

Hope it helps the Java Community.

Fareez Ahamed

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.