Sorting a table breaks the TableRowListener?

I have a sortable default table model with ListSelectionListener that listens for a doubleclick and then opens a details view of a particular column. This works fine, however, when I sort a column the listener no longer functions.

JTable foosTable = new JTable(model);
TableRowSorter<TableModel> fooSorter = new TableRowSorter<TableModel>(model);
foosTable.setRowSorter(fooSorter);
ListSelectionModel listMod = foosTable.getSelectionModel();
listMod.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
ListSelectionListener barListener = new ListSelectionListener(foosTable);
listMod.addListSelectionListener(barListener);

Answers


I have never used TableRowListener which seems to only have an itemChanged event. I usually stick to standard swing. Add a mouse listener to the table, grab the location of a click event and then handle it.

 jTable.addMouseListener(new java.awt.event.MouseAdapter() {
     public void mouseClicked(java.awt.event.MouseEvent evt) {
         Point p = new Point(evt.getX(), evt.getY());
         int col = jTable.columnAtPoint(p);
         int row = jTable.rowAtPoint(p);
         if (evt.getButton() == MouseEvent.BUTTON1)
         {
            if (evt.getClickCount() >= 2)
            {
               ...
               ...
            }
        });

Edit Setup a TableRowSorter:

  TableRowSorter<YourJTableModel> sorter =
        new TableRowSorter<YourJTableModel>(yourJTableModel);
   jTable.setRowSorter(sorter);

Because you are changing the row order, you will need to use convertColumnIndexToModel to get the correct model data for the view.

For more complex sorting/filtering needs you may want to try glazed lists.


Need Your Help

PostgreSQL: summing values that share a field

database postgresql sum

For a given schema of Olympians in PostgreSQL 9.3.1

Target last visible div after js action?

javascript jquery css filter jquery-selectors

Is it possible to target the last visible div/container after a js function has worked, in this case mixitup plugin. You click to filter your results, this adds display: none or display: inline-blo...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.