Champika Samarasinghe
Champika Samarasinghe

Reputation: 171

Search jTable using beans binding

I have a jTable which is populated by data from a database using Beans Binding. I want to implement a search function for the table. I came across following tutorial which achieves this using beans binding:

https://weblogs.java.net/blog/pkeegan/archive/2008/06/using_beans_bin.html

I am unable to get it to work. When i run the sample application i created, i see an unexpected value such as "javax.swing.table.TableRowSorter@16994fe8" appearing on the search text field. This is actually coming from convertForward() method in the converter class which converts the TableRowSorter object to a string. Nothing happens when i type something on the text field.

I have set autoCreateRowSorter property of my jTable to true. I am not using a custom table model since the table is populated using beans binding using the default table model.

Noted that getTable(), setTable() or convertReverse() methods of the converter class are not getting invoked. I am wondering whether i missed anything in the process.

Given below is the code i am using to bind search text field with the jTable:

BindingGroup bindingGroup = new BindingGroup();
RowSorterToStringConverter bindingConverter = new RowSorterToStringConverter();
Binding searchBinding = Bindings.createAutoBinding(
    UpdateStrategy.READ_WRITE, 
    employeeTable, 
    ELProperty.create("${rowSorter}"), 
    txtSearch,
    BeanProperty.create("text"));
searchBinding.setConverter(bindingConverter);
bindingGroup.addBinding(searchBinding);
bindingGroup.bind();

Any help in this regard is greatly appreciated.

Upvotes: 1

Views: 966

Answers (2)

chiragchavda.ks
chiragchavda.ks

Reputation: 530

this code will search from whole table and it is case insensitive. and thanks to kleopatra your answer is very help full

    BindingGroup context = new BindingGroup();
    AutoBinding binding = Bindings.createAutoBinding(
            UpdateStrategy.READ_WRITE, txtfldSearch,
            BeanProperty.create("text"),

            tableSupplier, BeanProperty.create("rowFilter"));

    Converter<String, RowFilter> converter = new Converter<String, RowFilter>()                     {
        @Override
        public RowFilter convertForward(String value) {
            if (value == null || value.trim().length() == 0)
                return null;
            return RowFilters.regexFilter("(?i).*" + value + ".*");
        }

        @Override
        public String convertReverse(RowFilter value) {
            throw new UnsupportedOperationException(
                    "don't expect reverse conversion here");
        }
    };
    binding.setConverter(converter);
    context.addBinding(binding);
    context.bind();

Upvotes: 0

kleopatra
kleopatra

Reputation: 51535

Just so happened that I needed something similar recently, and it turned out to be rather straightforward: assuming you want

  • to map the text typed into a textfield to a regex rowFilter
  • use beansbinding to filter the table rows on typing

So the direction of the conversion is from the field (source) to the table (target), and all you need is

  • a suitable converter from String to Rowfilter, the reverse doesn't make much sense (to me, at least :-)
  • a binding from the field's text property to the table's rowFilter property (assuming JXTable, for a plain table you'll need a path down to the sorter's filter)
  • attach the converter to the binding

Something like:

final JXTable table = createTable(surveys);
JTextField field = new JTextField(20);
BindingGroup context = new BindingGroup();
AutoBinding binding = Bindings.createAutoBinding(READ, 
        field, BeanProperty.create("text"), 
        // JXTable which has delegating
        // api to set the rowFilter
        table, BeanProperty.create("rowFilter"));
        // plain table, use path the sorter's filer property
        //table, BeanProperty.create("rowSorter.rowFilter"));
Converter<String, RowFilter> converter = new Converter<String, RowFilter>() {
    @Override
    public RowFilter convertForward(String value) {
        if (value == null || value.trim().length() == 0) return null;
        return RowFilters.regexFilter(value, 0);
    }

    @Override
    public String convertReverse(RowFilter value) {
        throw new UnsupportedOperationException("don't expect reverse conversion here");
    }

};
binding.setConverter(converter);
context.addBinding(binding);
context.bind();

Upvotes: 2

Related Questions