Skip to main content

setRowFilter() in JXTable

3 replies [Last post]
cronid
Offline
Joined: 2010-08-12

Hello everyone,

RowFilter filter = RowFilter.regexFilter("2");
jxtable.setRowFilter(filter);

wont work, because of the given generic types. It has to be "RowFilter filter" always.

The related error:
The method setRowFilter(RowFilter<? super TableModel,? super Integer>) in the type JXTable is not applicable for the arguments (RowFilter)

It is not a real problem: "TableModel" instead of "DefaultTableModel" will work, but with the swing core option it will work with "DefaultTableModel", if you use the normal way via "DefaultRowSorter".

Some time ago, I wrote this to provide an easy way to set a row filter for JTable (just for private projects):

public class MyJTable extends JTable {</p>
<p>    public MyJTable(TableModel dm) {<br />
        super(dm);<br />
    }</p>
<p>//    @SuppressWarnings("unchecked")<br />
    public  void setRowFilter(RowFilter<? super R, ? super Integer> filter) {<br />
        if (getRowSorter() instanceof DefaultRowSorter<?, ?>) {<br />
            // all fine, because R is a TableModel (R extends TableModel)<br />
            DefaultRowSorter sorter = (DefaultRowSorter) getRowSorter();<br />
            sorter.setRowFilter(filter);<br />
        }<br />
    }</p>
<p>    public static void main(String[] args) {<br />
        String[][] rowData = {<br />
            { "Japan", "245" }, { "USA", "240" }, { "Italien", "220" },<br />
            { "Spanien", "217" }, {"Türkei", "215"} ,{ "England", "214" },<br />
            { "Frankreich", "190" }, {"Griechenland", "185" },<br />
            { "Deutschland", "180" }, {"Portugal", "170" }<br />
        };<br />
        String[] columnNames = {<br />
                "Land", "Durchschnittliche Fernsehdauer pro Tag in Minuten"<br />
        };<br />
        MyModel model = new MyModel(rowData, columnNames);<br />
        MyJTable table = new MyJTable(model);<br />
        table.setAutoCreateRowSorter(true);</p>
<p>        JFrame f = new JFrame();<br />
        f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );<br />
        f.add(new JScrollPane(table));<br />
        f.pack();<br />
        f.setVisible(true);</p>
<p>        RowFilter filter = RowFilter.regexFilter("240", 1);<br />
        table.setRowFilter(filter);<br />
        RowFilter filter2 = RowFilter.regexFilter("245", 1);<br />
        table.setRowFilter(filter2);<br />
        RowFilter filter3 = RowFilter.regexFilter("2", 1);<br />
        table.setRowFilter(filter3);</p>
<p>//        ((DefaultRowSorter) table.getRowSorter()).setRowFilter(filter);<br />
        // bad one because of this warning:<br />
        // Type safety: Unchecked cast from RowSorter<br />
        // to DefaultRowSorter<br />
    }</p>
<p>    public static class MyModel extends DefaultTableModel {</p>
<p>        public MyModel(Object[][] data, Object[] columnNames) {<br />
            super(data, columnNames);<br />
        }<br />
    }<br />
}

Based on that I wrote an extension of JXTable:

public class MyJXTable extends JXTable {</p>
<p>    public MyJXTable(TableModel dm) {<br />
        super(dm);<br />
    }</p>
<p>    public  void setFilter(RowFilter<? super R, ? super Integer> filter) {<br />
        if (hasSortController()) {<br />
            // all fine, because R is a TableModel (R extends TableModel)<br />
            SortController sorter = (SortController) getSortController();<br />
            sorter.setRowFilter(filter);<br />
        } else {<br />
            // no sort controller<br />
            if (getRowSorter() instanceof DefaultRowSorter<?, ?>) {<br />
                // all fine, because R is a TableModel (R extends TableModel)<br />
                DefaultRowSorter sorter = (DefaultRowSorter) getRowSorter();<br />
                sorter.setRowFilter(filter);<br />
            }<br />
        }<br />
    }</p>
<p>    public static void main(String[] args) {<br />
        String[][] rowData = {<br />
            { "Japan", "245" }, { "USA", "240" }, { "Italien", "220" },<br />
            { "Spanien", "217" }, {"Türkei", "215"} ,{ "England", "214" },<br />
            { "Frankreich", "190" }, {"Griechenland", "185" },<br />
            { "Deutschland", "180" }, {"Portugal", "170" }<br />
        };<br />
        String[] columnNames = {<br />
                "Land", "Durchschnittliche Fernsehdauer pro Tag in Minuten"<br />
        };<br />
        MyModel model = new MyModel(rowData, columnNames);<br />
        MyJXTable table = new MyJXTable(model);<br />
        table.setAutoCreateRowSorter(true);</p>
<p>        JFrame f = new JFrame();<br />
        f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );<br />
        f.add(new JScrollPane(table));<br />
        f.pack();<br />
        f.setVisible(true);</p>
<p>        RowFilter filter = RowFilter.regexFilter("240", 1);<br />
        table.setFilter(filter);<br />
        RowFilter filter2 = RowFilter.regexFilter("245", 1);<br />
        table.setFilter(filter2);<br />
        RowFilter filter3 = RowFilter.regexFilter("2", 1);<br />
        table.setFilter(filter3);</p>
<p>//        table.setRowFilter(filter);<br />
        // bad one because of this error:<br />
        // The method setRowFilter(RowFilter<? super TableModel,? super Integer>) in the<br />
        // type JXTable is not applicable for the arguments (RowFilter)<br />
    }</p>
<p>    public static class MyModel extends DefaultTableModel {</p>
<p>        public MyModel(Object[][] data, Object[] columnNames) {<br />
            super(data, columnNames);<br />
        }<br />
    }<br />
}

Well, it works.

Message was edited by: cronid

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kleopatra
Offline
Joined: 2003-06-11

good catch :-) and yet another proof that generics and me never will become friends

Could you please raise an issue in the SwingX issue tracker so we dont forget to look into it?

@Karl: you'r my generic hero - could you give it a look, please?

Thanks everybody
Jeanette

cronid
Offline
Joined: 2010-08-12
kleopatra
Offline
Joined: 2003-06-11

applied your patch to JXList/JXTable.

Just one minor change, though: does nothing if hasSortController returns false. That's mainly for consistency, the views guarantee to not touch the sorter's properties in that case.

Thanks
Jeanette