Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member

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

http://freeze-cse.blogspot.com/p/texthelp_07.html

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

TextHelp

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

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.

TextHelpListener

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

TextSelectedEvent

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. freeze-cse.blogspot.com
*
*/
public class TextHelpDemo extends JFrame implements TextHelpListener {
    Connection conn;
    TextHelp th;
    JTextField tf;
    JPanel panel;
    JLabel cname;
    TextHelpConfig thc;
    TextHelpDemo() {
        super("TextHelp");
        tf = new JTextField(10);
        cname = new JLabel();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
            th = new TextHelp(conn,KeyEvent.VK_F1,this);
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        getContentPane().setLayout(new FlowLayout());
        getContentPane().add(new JLabel("Country"));
        getContentPane().add(tf);
        getContentPane().add(cname);
        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);
            th.addTextHelpListener(this);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(200,200);
        setVisible(true);
    }
    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() +"'");
                rs.next();
                cname.setText(rs.getString(1));
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    }
}

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 http://freeze-cse.blogspot.com/p/country-table.html.

Check out the output.

On pressing F1

After Selection

Hope it helps the Java Community.

Fareez Ahamed

freeze-cse.blogspot.com

Labels in this area