JComboBox automatic completion and Oracle ADF

Thomas Bierhance has written an excellent article on implementing automatic completion for JComboBoxes: Inside JComboBox: adding automatic completion.

Unfortunately, when you try to use it with Oracle’s ADF you’ll notice that it doesn’t work. I’ve modified Thomas’s AutoCompletion class to make it work with ADF. The code requires two changes.

First, when you are using ADF, the ComboBoxModel contains Row objects. A row object can have multiple attributes and you need to determine which attribute needs to be matched with the entered text. You can get this info from a UIModel.

Second, empty rows in the ComboBoxModel aren’t displayed when you use ADF. I think this is a bug, but it means that you can’t select an empty value in the JComboBox. You can work around this problem by setting the selected item to null if the entered string is completely selected and the delete key is pressed.

You can download the modified code here: AdfAutoCompletion.java.

The following is a small example using the autocompletion code. The example demonstrates how you can build a JPanel using ADF, based on a POJO model. Most examples on the Oracle site show you how to do this with ADF Business Components, but you can also use POJO’s.

First create an Emp object. The JComboBox we will display on the panel will display a list of Emp objects:

package model;
public class Emp {
    private Integer id;
    private String name;

    public Emp() {
    }

    public Emp(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public String toString() {
        return name;
    }
}

Next we need a model service. This class contains an id attribute. You’ll be able to change the value of id using the combobox. The model service also contains a method to get the array of objects which need to be displayed by the combobox.

package model;
public class AutoCompletionModelService {
    public static Integer id;
    public static Emp[] emps;

    public AutoCompletionModelService() {
    }

    public Emp[] getEmps() {
        if (emps == null) {
            emps = new Emp[] {
                    new Emp(null, ""),
                    new Emp(new Integer(1), "Robby"),
                    new Emp(new Integer(2), "Pete"),
                    new Emp(new Integer(3), "Jason"),
                    new Emp(new Integer(4), "Dave"),
                };
        }

        return emps;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }

    public void printSelectedEmp() {
        System.out.println("selected emp id: " + id);
    }
}

When you have these two classes, create a Data Control for AutoCompletionModelService. You do this by right clicking on AutoCompletionModelService.java in the Applications navigator. This will create some xml files which describe the classes in your model package.

Now we can start building the GUI. You don’t need much manual coding here. First create a new JPanel for ADF, and then drag and drop a label (from the Swing component palette), and a combobox on the panel. To drop the combobox on the panel you select the id attribute of the AutoCompletionModelSerice data control from the data control palette. Then you specify that you want to drop it as a combo box. Now drag the id attribute on your panel.

Data control palette

Your panel will look something like this (I’ve also dragged the printSelectedEmp method as a button on the panel):

JPanel

Next you need to specify what data to display in the combobox. You do this by selecting the id binding in the panel UIModel, as displayed below.

UIModel

When you edit the binding you see the following binding editor. First specify ‘LOV Mode’ for the List Binding Mode. Next you specify the source of the lov data, i.e., emps, and the destination of the selected data, i.e., AutoCompletionModelServiceDataControl. You also need to specify which attribute provides the value you select (id), and which attribute should be displayed in the lov (name).

List Binding Editor

The only line of coding required is the code to enable autocompletion. Add the following line in jbInit:

AdfAutoCompletion.enable(employeeComboBox,panelBinding,"id");

That’s it. You can now run the panel and the following window will be displayed:

JPanel with autocompletion enabled jcombobox